mirror of
https://github.com/httpjamesm/AnonymousOverflow.git
synced 2025-04-21 06:19:14 +00:00
improvement: use lang-* classes declared in snippet markup
This commit is contained in:
parent
0d7355bd46
commit
0b073c0d3c
1 changed files with 9 additions and 6 deletions
|
@ -14,13 +14,15 @@ import (
|
||||||
|
|
||||||
// highlightSyntaxViaContent uses Chroma to lex code content and apply the appropriate tokenizer engine.
|
// 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.
|
// 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)
|
content = html.UnescapeString(content)
|
||||||
|
|
||||||
fallbackOut := html.EscapeString(content)
|
fallbackOut := html.EscapeString(content)
|
||||||
|
|
||||||
// identify the language
|
lexer := lexers.Get(lang)
|
||||||
lexer := lexers.Analyse(content)
|
if lexer == nil {
|
||||||
|
lexer = lexers.Analyse(content)
|
||||||
|
}
|
||||||
if lexer == nil {
|
if lexer == nil {
|
||||||
lexer = lexers.Get(".js")
|
lexer = lexers.Get(".js")
|
||||||
}
|
}
|
||||||
|
@ -71,19 +73,20 @@ func stripBlockTags(content string) (result string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var codeBlockRegex = regexp.MustCompile(`(?s)<pre><code>(.*?)<\/code><\/pre>`)
|
var codeBlockRegex = regexp.MustCompile(`(?s)<pre.*?lang-(.*?)[\s"'].*?><code>(.*?)<\/code><\/pre>`)
|
||||||
|
|
||||||
// HighlightCodeBlocks uses both highlightSyntaxViaContent stripCodeBlocks and returns the newly highlighted code HTML.
|
// HighlightCodeBlocks uses both highlightSyntaxViaContent stripCodeBlocks and returns the newly highlighted code HTML.
|
||||||
func HighlightCodeBlocks(html string) string {
|
func HighlightCodeBlocks(html string) string {
|
||||||
// Replace each code block with the highlighted version
|
// Replace each code block with the highlighted version
|
||||||
highlightedHTML := codeBlockRegex.ReplaceAllStringFunc(html, func(codeBlock string) string {
|
highlightedHTML := codeBlockRegex.ReplaceAllStringFunc(html, func(codeBlock string) string {
|
||||||
// Extract the code content from the code block
|
// 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)
|
codeContent = stripBlockTags(codeContent)
|
||||||
|
|
||||||
// Highlight the code content
|
// Highlight the code content
|
||||||
highlightedCode := highlightSyntaxViaContent(codeContent)
|
highlightedCode := highlightSyntaxViaContent(codeContent, lang)
|
||||||
|
|
||||||
// Replace the original code block with the highlighted version
|
// Replace the original code block with the highlighted version
|
||||||
highlightedCodeBlock := "<pre>" + highlightedCode + "</pre>"
|
highlightedCodeBlock := "<pre>" + highlightedCode + "</pre>"
|
||||||
|
|
Loading…
Add table
Reference in a new issue