From 23c3f3fb4d72445af37c807218d247c37be4b1fb Mon Sep 17 00:00:00 2001 From: noClaps Date: Wed, 27 Aug 2025 00:14:27 +0100 Subject: [PATCH] appview: add support for latex in markdown --- appview/pages/markup/markdown.go | 2 ++ appview/pages/markup/sanitizer.go | 16 ++++++++++++++++ go.mod | 4 +++- go.sum | 7 ++++++- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/appview/pages/markup/markdown.go b/appview/pages/markup/markdown.go index 15d02da..7951f7b 100644 --- a/appview/pages/markup/markdown.go +++ b/appview/pages/markup/markdown.go @@ -11,6 +11,7 @@ import ( chromahtml "github.com/alecthomas/chroma/v2/formatters/html" "github.com/alecthomas/chroma/v2/styles" + treeblood "github.com/wyatt915/goldmark-treeblood" "github.com/yuin/goldmark" highlighting "github.com/yuin/goldmark-highlighting/v2" "github.com/yuin/goldmark/ast" @@ -59,6 +60,7 @@ func (rctx *RenderContext) RenderMarkdown(source string) string { extension.NewFootnote( extension.WithFootnoteIDPrefix([]byte("footnote")), ), + treeblood.MathML(), ), goldmark.WithParserOptions( parser.WithAutoHeadingID(), diff --git a/appview/pages/markup/sanitizer.go b/appview/pages/markup/sanitizer.go index c77af66..e5825b6 100644 --- a/appview/pages/markup/sanitizer.go +++ b/appview/pages/markup/sanitizer.go @@ -97,6 +97,22 @@ func defaultPolicy() *bluemonday.Policy { "margin-bottom", ) + // math + mathAttrs := []string{ + "accent", "columnalign", "columnlines", "columnspan", "dir", "display", + "displaystyle", "encoding", "fence", "form", "largeop", "linebreak", + "linethickness", "lspace", "mathcolor", "mathsize", "mathvariant", "minsize", + "movablelimits", "notation", "rowalign", "rspace", "rowspacing", "rowspan", + "scriptlevel", "stretchy", "symmetric", "title", "voffset", "width", + } + mathElements := []string{ + "annotation", "math", "menclose", "merror", "mfrac", "mi", "mmultiscripts", + "mn", "mo", "mover", "mpadded", "mprescripts", "mroot", "mrow", "mspace", + "msqrt", "mstyle", "msub", "msubsup", "msup", "mtable", "mtd", "mtext", + "mtr", "munder", "munderover", "semantics", + } + policy.AllowAttrs(mathAttrs...).OnElements(mathElements...) + return policy } diff --git a/go.mod b/go.mod index 03793b3..b59ada7 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,8 @@ require ( github.com/stretchr/testify v1.10.0 github.com/urfave/cli/v3 v3.3.3 github.com/whyrusleeping/cbor-gen v0.3.1 - github.com/yuin/goldmark v1.4.15 + github.com/wyatt915/goldmark-treeblood v0.0.0-20250825231212-5dcbdb2f4b57 + github.com/yuin/goldmark v1.7.12 github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc golang.org/x/crypto v0.40.0 golang.org/x/net v0.42.0 @@ -154,6 +155,7 @@ require ( github.com/vmihailenco/go-tinylfu v0.2.2 // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect + github.com/wyatt915/treeblood v0.1.15 // indirect gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b // indirect gitlab.com/yawning/tuplehash v0.0.0-20230713102510-df83abbf9a02 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect diff --git a/go.sum b/go.sum index c1ffdc2..09687f3 100644 --- a/go.sum +++ b/go.sum @@ -426,13 +426,18 @@ github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSD github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/whyrusleeping/cbor-gen v0.3.1 h1:82ioxmhEYut7LBVGhGq8xoRkXPLElVuh5mV67AFfdv0= github.com/whyrusleeping/cbor-gen v0.3.1/go.mod h1:pM99HXyEbSQHcosHc0iW7YFmwnscr+t9Te4ibko05so= +github.com/wyatt915/goldmark-treeblood v0.0.0-20250825231212-5dcbdb2f4b57 h1:UqtQdzLXnvdBdqn/go53qGyncw1wJ7Mq5SQdieM1/Ew= +github.com/wyatt915/goldmark-treeblood v0.0.0-20250825231212-5dcbdb2f4b57/go.mod h1:BxSCWByWSRSuembL3cDG1IBUbkBoO/oW/6tF19aA4hs= +github.com/wyatt915/treeblood v0.1.15 h1:3KZ3o2LpcKZAzOLqMoW9qeUzKEaKArKpbcPpTkNfQC8= +github.com/wyatt915/treeblood v0.1.15/go.mod h1:i7+yhhmzdDP17/97pIsOSffw74EK/xk+qJ0029cSXUY= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/yuin/goldmark v1.4.15 h1:CFa84T0goNn/UIXYS+dmjjVxMyTAvpOmzld40N/nfK0= github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/goldmark v1.7.12 h1:YwGP/rrea2/CnCtUHgjuolG/PnMxdQtPMO5PvaE2/nY= +github.com/yuin/goldmark v1.7.12/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg= github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ= github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I= gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b h1:CzigHMRySiX3drau9C6Q5CAbNIApmLdat5jPMqChvDA= -- 2.43.0