mirror of
https://github.com/httpjamesm/AnonymousOverflow.git
synced 2025-06-10 16:39:41 +00:00
refactor: use existing PreventSurroundingPre
This commit is contained in:
parent
10e7c89e96
commit
c0df07abad
6 changed files with 26 additions and 15 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue