forked from tangled.org/core
this repo has no description

appview: markdown: introduce syntax highlighting to code blocks

Signed-off-by: BrookJeynes <jeynesbrook@gmail.com>

brookjeynes.dev 599740ec 096eb308

verified
Changed files
+27 -3
appview
pages
markup
+20 -1
appview/pages/markup/markdown.go
··· 5 5 "bytes" 6 6 "fmt" 7 7 "io" 8 + "log" 8 9 "net/url" 9 10 "path" 10 11 "strings" 11 12 13 + "github.com/alecthomas/chroma/v2" 14 + chromahtml "github.com/alecthomas/chroma/v2/formatters/html" 15 + "github.com/alecthomas/chroma/v2/styles" 12 16 "github.com/microcosm-cc/bluemonday" 13 17 "github.com/yuin/goldmark" 18 + highlighting "github.com/yuin/goldmark-highlighting/v2" 14 19 "github.com/yuin/goldmark/ast" 15 20 "github.com/yuin/goldmark/extension" 16 21 "github.com/yuin/goldmark/parser" ··· 43 48 } 44 49 45 50 func (rctx *RenderContext) RenderMarkdown(source string) string { 51 + var style *chroma.Style = styles.Get("catppuccin-latte") 46 52 md := goldmark.New( 47 - goldmark.WithExtensions(extension.GFM), 48 53 goldmark.WithParserOptions( 49 54 parser.WithAutoHeadingID(), 50 55 ), 51 56 goldmark.WithRendererOptions(html.WithUnsafe()), 57 + goldmark.WithExtensions( 58 + extension.GFM, 59 + highlighting.NewHighlighting( 60 + highlighting.WithFormatOptions( 61 + chromahtml.Standalone(false), 62 + chromahtml.WithClasses(true), 63 + ), 64 + highlighting.WithCustomStyle(style), 65 + ), 66 + ), 52 67 ) 53 68 54 69 if rctx != nil { ··· 63 78 64 79 var buf bytes.Buffer 65 80 if err := md.Convert([]byte(source), &buf); err != nil { 81 + log.Println("failed to convert markdown:", err) 66 82 return source 67 83 } 68 84 ··· 173 189 "margin-top", 174 190 "margin-bottom", 175 191 ) 192 + 193 + policy.AllowAttrs("class").OnElements("code", "pre", "span") 194 + 176 195 return policy.Sanitize(html) 177 196 } 178 197
+2 -1
go.mod
··· 36 36 github.com/stretchr/testify v1.10.0 37 37 github.com/urfave/cli/v3 v3.3.3 38 38 github.com/whyrusleeping/cbor-gen v0.3.1 39 - github.com/yuin/goldmark v1.4.13 39 + github.com/yuin/goldmark v1.4.15 40 + github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc 40 41 golang.org/x/crypto v0.38.0 41 42 golang.org/x/net v0.40.0 42 43 golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
+5 -1
go.sum
··· 77 77 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= 78 78 github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= 79 79 github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= 80 + github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= 80 81 github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ= 81 82 github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= 82 83 github.com/docker/docker v28.2.2+incompatible h1:CjwRSksz8Yo4+RmQ339Dp/D2tGO5JxwYeqtMOEe0LDw= ··· 405 406 github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= 406 407 github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= 407 408 github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= 408 - github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= 409 409 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 410 + github.com/yuin/goldmark v1.4.15 h1:CFa84T0goNn/UIXYS+dmjjVxMyTAvpOmzld40N/nfK0= 411 + github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 412 + github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ= 413 + github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I= 410 414 gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b h1:CzigHMRySiX3drau9C6Q5CAbNIApmLdat5jPMqChvDA= 411 415 gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b/go.mod h1:/y/V339mxv2sZmYYR64O07VuCpdNZqCTwO8ZcouTMI8= 412 416 gitlab.com/yawning/tuplehash v0.0.0-20230713102510-df83abbf9a02 h1:qwDnMxjkyLmAFgcfgTnfJrmYKWhHnci3GjDqcZp1M3Q=