feat: syntax highlighting for question codeblocks

This commit is contained in:
httpjamesm 2022-12-28 19:10:14 -05:00
parent 14b11b7f14
commit 96d4f768d9
2 changed files with 19 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import (
)
var codeBlockRegex = regexp.MustCompile(`(?s)<pre><code>(.+?)<\/code><\/pre>`)
var questionCodeBlockRegex = regexp.MustCompile(`(?s)<pre class=".+"><code( class=".+")?>(.+?)</code></pre>`)
func ViewQuestion(c *gin.Context) {
client := resty.New()
@ -48,6 +49,20 @@ func ViewQuestion(c *gin.Context) {
panic(err)
}
// parse any code blocks and highlight them
answerCodeBlocks := questionCodeBlockRegex.FindAllString(questionBodyParentHTML, -1)
for _, codeBlock := range answerCodeBlocks {
codeBlock = utils.StripBlockTags(codeBlock)
fmt.Println(codeBlock)
// syntax highlight
highlightedCodeBlock := utils.HighlightSyntaxViaContent(codeBlock)
fmt.Println(highlightedCodeBlock)
// replace the code block with the highlighted code block
questionBodyParentHTML = strings.Replace(questionBodyParentHTML, codeBlock, highlightedCodeBlock, 1)
}
questionCard := doc.Find("div.postcell")
questionMetadata := questionCard.Find("div.user-info")