A vibe coded tangled fork which supports pijul.

appview/pages/markup: add Dashes extension and export NewMarkdownWith

Signed-off-by: Anirudh Oppiliappan <anirudh@tangled.org>

authored by

Anirudh Oppiliappan and committed by tangled.org 260976e4 31a71987

+93 -22
+64
appview/pages/markup/extension/dashes.go
··· 1 + package extension 2 + 3 + import ( 4 + "github.com/yuin/goldmark" 5 + gast "github.com/yuin/goldmark/ast" 6 + "github.com/yuin/goldmark/parser" 7 + "github.com/yuin/goldmark/text" 8 + "github.com/yuin/goldmark/util" 9 + ) 10 + 11 + type dashParser struct{} 12 + 13 + func (p *dashParser) Trigger() []byte { 14 + return []byte{'-'} 15 + } 16 + 17 + func (p *dashParser) Parse(parent gast.Node, block text.Reader, pc parser.Context) gast.Node { 18 + line, _ := block.PeekLine() 19 + if len(line) < 2 || line[0] != '-' || line[1] != '-' { 20 + return nil 21 + } 22 + node := gast.NewString([]byte("\u2014")) 23 + node.SetCode(true) 24 + block.Advance(2) 25 + return node 26 + } 27 + 28 + type digitDashParser struct{} 29 + 30 + func (p *digitDashParser) Trigger() []byte { 31 + return []byte{'-'} 32 + } 33 + 34 + func (p *digitDashParser) Parse(parent gast.Node, block text.Reader, pc parser.Context) gast.Node { 35 + line, _ := block.PeekLine() 36 + if len(line) < 2 { 37 + return nil 38 + } 39 + before := block.PrecendingCharacter() 40 + if before < '0' || before > '9' { 41 + return nil 42 + } 43 + if line[1] < '0' || line[1] > '9' { 44 + return nil 45 + } 46 + node := gast.NewString([]byte("\u2013")) 47 + node.SetCode(true) 48 + block.Advance(1) 49 + return node 50 + } 51 + 52 + type dashExt struct{} 53 + 54 + // Dashes replaces "--" with an em-dash (—) and a hyphen between two digits 55 + // with an en-dash (–). Implemented as an inline parser so it operates on the 56 + // raw byte stream, unaffected by hard-wrapped source lines. 57 + var Dashes goldmark.Extender = &dashExt{} 58 + 59 + func (e *dashExt) Extend(m goldmark.Markdown) { 60 + m.Parser().AddOptions(parser.WithInlineParsers( 61 + util.Prioritized(&dashParser{}, 9990), 62 + util.Prioritized(&digitDashParser{}, 9991), 63 + )) 64 + }
+29 -22
appview/pages/markup/markdown.go
··· 13 13 chromahtml "github.com/alecthomas/chroma/v2/formatters/html" 14 14 "github.com/alecthomas/chroma/v2/styles" 15 15 "github.com/yuin/goldmark" 16 - "github.com/yuin/goldmark-emoji" 16 + emoji "github.com/yuin/goldmark-emoji" 17 17 highlighting "github.com/yuin/goldmark-highlighting/v2" 18 18 "github.com/yuin/goldmark/ast" 19 19 "github.com/yuin/goldmark/extension" ··· 53 53 Files fs.FS 54 54 } 55 55 56 - func NewMarkdown(hostname string) goldmark.Markdown { 57 - md := goldmark.New( 58 - goldmark.WithExtensions( 59 - extension.GFM, 60 - &mermaid.Extender{ 61 - RenderMode: mermaid.RenderModeClient, 62 - NoScript: true, 63 - }, 64 - highlighting.NewHighlighting( 65 - highlighting.WithFormatOptions( 66 - chromahtml.Standalone(false), 67 - chromahtml.WithClasses(true), 68 - ), 69 - highlighting.WithCustomStyle(styles.Get("catppuccin-latte")), 56 + func NewMarkdown(hostname string, extra ...goldmark.Extender) goldmark.Markdown { 57 + exts := []goldmark.Extender{ 58 + extension.GFM, 59 + &mermaid.Extender{ 60 + RenderMode: mermaid.RenderModeClient, 61 + NoScript: true, 62 + }, 63 + highlighting.NewHighlighting( 64 + highlighting.WithFormatOptions( 65 + chromahtml.Standalone(false), 66 + chromahtml.WithClasses(true), 70 67 ), 71 - extension.NewFootnote( 72 - extension.WithFootnoteIDPrefix([]byte("footnote")), 73 - ), 74 - callout.CalloutExtention, 75 - textension.AtExt, 76 - textension.NewTangledLinkExt(hostname), 77 - emoji.Emoji, 68 + highlighting.WithCustomStyle(styles.Get("catppuccin-latte")), 78 69 ), 70 + extension.NewFootnote( 71 + extension.WithFootnoteIDPrefix([]byte("footnote")), 72 + ), 73 + callout.CalloutExtention, 74 + textension.AtExt, 75 + textension.NewTangledLinkExt(hostname), 76 + emoji.Emoji, 77 + } 78 + exts = append(exts, extra...) 79 + md := goldmark.New( 80 + goldmark.WithExtensions(exts...), 79 81 goldmark.WithParserOptions( 80 82 parser.WithAutoHeadingID(), 81 83 ), 82 84 goldmark.WithRendererOptions(html.WithUnsafe()), 83 85 ) 84 86 return md 87 + } 88 + 89 + // NewMarkdownWith is an alias for NewMarkdown with extra extensions. 90 + func NewMarkdownWith(hostname string, extra ...goldmark.Extender) goldmark.Markdown { 91 + return NewMarkdown(hostname, extra...) 85 92 } 86 93 87 94 func (rctx *RenderContext) RenderMarkdown(source string) string {