refactor: use existing PreventSurroundingPre

This commit is contained in:
httpjamesm 2022-12-29 12:22:59 -05:00
parent 10e7c89e96
commit c0df07abad
6 changed files with 26 additions and 15 deletions

View file

@ -7,13 +7,11 @@ import (
"regexp"
"strings"
"github.com/alecthomas/chroma/formatters"
html_formatter "github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
)
var plainFormattedCodeRegex = regexp.MustCompile(`(?s)<pre tabindex="0" class="chroma"><code>(.+?)</code></pre>`)
func HighlightSyntaxViaContent(content string) (htmlOut string) {
content = html.UnescapeString(content)
@ -32,10 +30,7 @@ func HighlightSyntaxViaContent(content string) (htmlOut string) {
style = styles.Fallback
}
formatter := formatters.Get("html")
if formatter == nil {
formatter = formatters.Fallback
}
formatter := html_formatter.New(html_formatter.PreventSurroundingPre(true), html_formatter.WithClasses(true))
iterator, err := lexer.Tokenise(nil, content)
if err != nil {
@ -52,14 +47,7 @@ func HighlightSyntaxViaContent(content string) (htmlOut string) {
return
}
// parse only the <pre><code>...</code></pre> part
htmlOut = b.String()
htmlOut = plainFormattedCodeRegex.FindString(htmlOut)
htmlOut = StripBlockTags(htmlOut)
// remove <pre tabindex="0" class="chroma">
htmlOut = strings.Replace(htmlOut, "<pre tabindex=\"0\" class=\"chroma\">", "", -1)
return
}