diff --git a/src/routes/question.go b/src/routes/question.go index f2ee434..0374bb8 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -14,6 +14,7 @@ import ( ) var codeBlockRegex = regexp.MustCompile(`(?s)
(.+?)<\/code><\/pre>`)
+var questionCodeBlockRegex = regexp.MustCompile(`(?s)
(.+?)
`) 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") diff --git a/src/utils/syntax.go b/src/utils/syntax.go index e4cdf58..78df5f3 100644 --- a/src/utils/syntax.go +++ b/src/utils/syntax.go @@ -64,6 +64,8 @@ func HighlightSyntaxViaContent(content string) (htmlOut string) { return } +var preClassRegex = regexp.MustCompile(`(?s)
`)
+
 func StripBlockTags(content string) (result string) {
 	// strip all "" tags
 	content = strings.Replace(content, "", "", -1)
@@ -72,6 +74,8 @@ func StripBlockTags(content string) (result string) {
 	content = strings.Replace(content, "
", "", -1)
 	content = strings.Replace(content, "
", "", -1) + content = preClassRegex.ReplaceAllString(content, "") + result = content return