From 8dfa4f75e6f9fda69fdf437bbd05a22ab9cfd23c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=2E=20R=C3=B8dseth?= Date: Tue, 4 Jun 2024 13:42:10 +0200 Subject: [PATCH] Either render MathJax formulas or apply syntax highlighting to code Either render MathJax formulas or apply syntax highlighting to code until the issue in github.com/xyproto/splash has been fixed. Fixes #147 --- engine/rendering.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/engine/rendering.go b/engine/rendering.go index 02192036..dd9e77d6 100644 --- a/engine/rendering.go +++ b/engine/rendering.go @@ -27,6 +27,8 @@ import ( _ "embed" ) +const markdownCodeStyle = "base16-snazzy" // using xyproto/splash + var ( //go:embed embedded/tex-mml-chtml.js mathJaxScript string @@ -66,19 +68,17 @@ func (ac *Config) LoadRenderFunctions(w http.ResponseWriter, _ *http.Request, L // Convert the buffer from Markdown to HTML htmlData := markdown.ToHTML(mdContent, mdParser, nil) - // Add a script for rendering MathJax, but only if at least one mathematical formula is present - if containsFormula(mdContent) { - htmlData = append(htmlData, []byte(``)...) - } - // Apply syntax highlighting - codeStyle := "base16-snazzy" - if highlightedHTML, err := splash.Splash(htmlData, codeStyle); err == nil { // success + if highlightedHTML, err := splash.Splash(htmlData, markdownCodeStyle); err == nil { // success htmlData = highlightedHTML } + // Add a script for rendering MathJax, but only if at least one mathematical formula is present + if containsFormula(mdContent) { + js := append([]byte(` tag + } + w.Write(htmlData) return 0 // number of results })) @@ -451,18 +451,16 @@ func (ac *Config) MarkdownPage(w http.ResponseWriter, req *http.Request, mdConte } } - // Add a script for rendering MathJax, but only if at least one mathematical formula is present - if containsFormula(mdContent) { - head.WriteString(``) - } - // Embed the style and rendered markdown into a simple HTML 5 page htmldata := themes.SimpleHTMLPage(title, h1title, []byte(head.String()), htmlbody) - // Add syntax highlighting to the header, but only if "`), []byte(mathJaxScript)...) + htmldata = InsertScriptTag(htmldata, js) // also adds the closing tag + } else if bytes.Contains(htmlbody, []byte("