From 23b8ed8899cc79a51fc4fb78aa8332921f68aa91 Mon Sep 17 00:00:00 2001 From: Solomon Date: Thu, 28 Mar 2024 23:59:28 +0000 Subject: [PATCH] improvement: use lang-* classes declared in snippet markup (#97) * improvement: use lang-* classes declared in snippet markup * fix: don't require snippet lang classes in code blocks * fix: constrain greedy code block match --- src/utils/syntax.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/utils/syntax.go b/src/utils/syntax.go index 24c5d38..369f114 100644 --- a/src/utils/syntax.go +++ b/src/utils/syntax.go @@ -14,13 +14,15 @@ import ( // highlightSyntaxViaContent uses Chroma to lex code content and apply the appropriate tokenizer engine. // If it can't find one, it defaults to JavaScript syntax highlighting. -func highlightSyntaxViaContent(content string) (htmlOut string) { +func highlightSyntaxViaContent(content string, lang string) (htmlOut string) { content = html.UnescapeString(content) fallbackOut := html.EscapeString(content) - // identify the language - lexer := lexers.Analyse(content) + lexer := lexers.Get(lang) + if lexer == nil { + lexer = lexers.Analyse(content) + } if lexer == nil { lexer = lexers.Get(".js") } @@ -71,19 +73,20 @@ func stripBlockTags(content string) (result string) { return } -var codeBlockRegex = regexp.MustCompile(`(?s)
(.*?)<\/code><\/pre>`)
+var codeBlockRegex = regexp.MustCompile(`(?s)]+?lang-(.+?)[\s"'])?.*?>(.*?)<\/code><\/pre>`)
 
 // HighlightCodeBlocks uses both highlightSyntaxViaContent stripCodeBlocks and returns the newly highlighted code HTML.
 func HighlightCodeBlocks(html string) string {
 	// Replace each code block with the highlighted version
 	highlightedHTML := codeBlockRegex.ReplaceAllStringFunc(html, func(codeBlock string) string {
 		// Extract the code content from the code block
-		codeContent := codeBlockRegex.FindStringSubmatch(codeBlock)[1]
+		matches := codeBlockRegex.FindStringSubmatch(codeBlock)
+		lang, codeContent := matches[1], matches[2]
 
 		codeContent = stripBlockTags(codeContent)
 
 		// Highlight the code content
-		highlightedCode := highlightSyntaxViaContent(codeContent)
+		highlightedCode := highlightSyntaxViaContent(codeContent, lang)
 
 		// Replace the original code block with the highlighted version
 		highlightedCodeBlock := "
" + highlightedCode + "
"