A vibe coded tangled fork which supports pijul.
at sl/spindle-adapters 314 lines 7.7 kB view raw
1// Package markup is an umbrella package for all markups and their renderers. 2package markup 3 4import ( 5 "bytes" 6 "fmt" 7 "io" 8 "io/fs" 9 "net/url" 10 "path" 11 "strings" 12 13 chromahtml "github.com/alecthomas/chroma/v2/formatters/html" 14 "github.com/alecthomas/chroma/v2/styles" 15 "github.com/yuin/goldmark" 16 "github.com/yuin/goldmark-emoji" 17 highlighting "github.com/yuin/goldmark-highlighting/v2" 18 "github.com/yuin/goldmark/ast" 19 "github.com/yuin/goldmark/extension" 20 "github.com/yuin/goldmark/parser" 21 "github.com/yuin/goldmark/renderer/html" 22 "github.com/yuin/goldmark/text" 23 "github.com/yuin/goldmark/util" 24 callout "gitlab.com/staticnoise/goldmark-callout" 25 htmlparse "golang.org/x/net/html" 26 27 "tangled.org/core/api/tangled" 28 textension "tangled.org/core/appview/pages/markup/extension" 29 "tangled.org/core/appview/pages/repoinfo" 30) 31 32// RendererType defines the type of renderer to use based on context 33type RendererType int 34 35const ( 36 // RendererTypeRepoMarkdown is for repository documentation markdown files 37 RendererTypeRepoMarkdown RendererType = iota 38 // RendererTypeDefault is non-repo markdown, like issues/pulls/comments. 39 RendererTypeDefault 40) 41 42// RenderContext holds the contextual data for rendering markdown. 43// It can be initialized empty, and that'll skip any transformations. 44type RenderContext struct { 45 CamoUrl string 46 CamoSecret string 47 repoinfo.RepoInfo 48 IsDev bool 49 Hostname string 50 RendererType RendererType 51 Files fs.FS 52} 53 54func NewMarkdown(hostname string) goldmark.Markdown { 55 md := goldmark.New( 56 goldmark.WithExtensions( 57 extension.GFM, 58 highlighting.NewHighlighting( 59 highlighting.WithFormatOptions( 60 chromahtml.Standalone(false), 61 chromahtml.WithClasses(true), 62 ), 63 highlighting.WithCustomStyle(styles.Get("catppuccin-latte")), 64 ), 65 extension.NewFootnote( 66 extension.WithFootnoteIDPrefix([]byte("footnote")), 67 ), 68 callout.CalloutExtention, 69 textension.AtExt, 70 textension.NewTangledLinkExt(hostname), 71 emoji.Emoji, 72 ), 73 goldmark.WithParserOptions( 74 parser.WithAutoHeadingID(), 75 ), 76 goldmark.WithRendererOptions(html.WithUnsafe()), 77 ) 78 return md 79} 80 81func (rctx *RenderContext) RenderMarkdown(source string) string { 82 return rctx.RenderMarkdownWith(source, NewMarkdown(rctx.Hostname)) 83} 84 85func (rctx *RenderContext) RenderMarkdownWith(source string, md goldmark.Markdown) string { 86 if rctx != nil { 87 var transformers []util.PrioritizedValue 88 89 transformers = append(transformers, util.Prioritized(&MarkdownTransformer{rctx: rctx}, 10000)) 90 91 md.Parser().AddOptions( 92 parser.WithASTTransformers(transformers...), 93 ) 94 } 95 96 var buf bytes.Buffer 97 if err := md.Convert([]byte(source), &buf); err != nil { 98 return source 99 } 100 101 var processed strings.Builder 102 if err := postProcess(rctx, strings.NewReader(buf.String()), &processed); err != nil { 103 return source 104 } 105 106 return processed.String() 107} 108 109func postProcess(ctx *RenderContext, input io.Reader, output io.Writer) error { 110 node, err := htmlparse.Parse(io.MultiReader( 111 strings.NewReader("<html><body>"), 112 input, 113 strings.NewReader("</body></html>"), 114 )) 115 if err != nil { 116 return fmt.Errorf("failed to parse html: %w", err) 117 } 118 119 if node.Type == htmlparse.DocumentNode { 120 node = node.FirstChild 121 } 122 123 visitNode(ctx, node) 124 125 newNodes := make([]*htmlparse.Node, 0, 5) 126 127 if node.Data == "html" { 128 node = node.FirstChild 129 for node != nil && node.Data != "body" { 130 node = node.NextSibling 131 } 132 } 133 if node != nil { 134 if node.Data == "body" { 135 child := node.FirstChild 136 for child != nil { 137 newNodes = append(newNodes, child) 138 child = child.NextSibling 139 } 140 } else { 141 newNodes = append(newNodes, node) 142 } 143 } 144 145 for _, node := range newNodes { 146 if err := htmlparse.Render(output, node); err != nil { 147 return fmt.Errorf("failed to render processed html: %w", err) 148 } 149 } 150 151 return nil 152} 153 154func visitNode(ctx *RenderContext, node *htmlparse.Node) { 155 switch node.Type { 156 case htmlparse.ElementNode: 157 switch node.Data { 158 case "img", "source": 159 for i, attr := range node.Attr { 160 if attr.Key != "src" { 161 continue 162 } 163 164 camoUrl, _ := url.Parse(ctx.CamoUrl) 165 dstUrl, _ := url.Parse(attr.Val) 166 if dstUrl.Host != camoUrl.Host { 167 attr.Val = ctx.imageFromKnotTransformer(attr.Val) 168 attr.Val = ctx.camoImageLinkTransformer(attr.Val) 169 node.Attr[i] = attr 170 } 171 } 172 } 173 174 for n := node.FirstChild; n != nil; n = n.NextSibling { 175 visitNode(ctx, n) 176 } 177 default: 178 } 179} 180 181type MarkdownTransformer struct { 182 rctx *RenderContext 183} 184 185func (a *MarkdownTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { 186 _ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) { 187 if !entering { 188 return ast.WalkContinue, nil 189 } 190 191 switch a.rctx.RendererType { 192 case RendererTypeRepoMarkdown: 193 switch n := n.(type) { 194 case *ast.Heading: 195 a.rctx.anchorHeadingTransformer(n) 196 case *ast.Link: 197 a.rctx.relativeLinkTransformer(n) 198 case *ast.Image: 199 a.rctx.imageFromKnotAstTransformer(n) 200 a.rctx.camoImageLinkAstTransformer(n) 201 } 202 case RendererTypeDefault: 203 switch n := n.(type) { 204 case *ast.Heading: 205 a.rctx.anchorHeadingTransformer(n) 206 case *ast.Image: 207 a.rctx.imageFromKnotAstTransformer(n) 208 a.rctx.camoImageLinkAstTransformer(n) 209 } 210 } 211 212 return ast.WalkContinue, nil 213 }) 214} 215 216func (rctx *RenderContext) relativeLinkTransformer(link *ast.Link) { 217 218 dst := string(link.Destination) 219 220 if isAbsoluteUrl(dst) || isFragment(dst) || isMail(dst) { 221 return 222 } 223 224 actualPath := rctx.actualPath(dst) 225 226 newPath := path.Join("/", rctx.RepoInfo.FullName(), "tree", rctx.RepoInfo.Ref, actualPath) 227 link.Destination = []byte(newPath) 228} 229 230func (rctx *RenderContext) imageFromKnotTransformer(dst string) string { 231 if isAbsoluteUrl(dst) { 232 return dst 233 } 234 235 scheme := "https" 236 if rctx.IsDev { 237 scheme = "http" 238 } 239 240 actualPath := rctx.actualPath(dst) 241 242 repoName := fmt.Sprintf("%s/%s", rctx.RepoInfo.OwnerDid, rctx.RepoInfo.Name) 243 244 query := fmt.Sprintf("repo=%s&ref=%s&path=%s&raw=true", 245 url.QueryEscape(repoName), url.QueryEscape(rctx.RepoInfo.Ref), actualPath) 246 247 parsedURL := &url.URL{ 248 Scheme: scheme, 249 Host: rctx.Knot, 250 Path: path.Join("/xrpc", tangled.RepoBlobNSID), 251 RawQuery: query, 252 } 253 newPath := parsedURL.String() 254 return newPath 255} 256 257func (rctx *RenderContext) imageFromKnotAstTransformer(img *ast.Image) { 258 dst := string(img.Destination) 259 img.Destination = []byte(rctx.imageFromKnotTransformer(dst)) 260} 261 262func (rctx *RenderContext) anchorHeadingTransformer(h *ast.Heading) { 263 idGeneric, exists := h.AttributeString("id") 264 if !exists { 265 return // no id, nothing to do 266 } 267 id, ok := idGeneric.([]byte) 268 if !ok { 269 return 270 } 271 272 // create anchor link 273 anchor := ast.NewLink() 274 anchor.Destination = fmt.Appendf(nil, "#%s", string(id)) 275 anchor.SetAttribute([]byte("class"), []byte("anchor")) 276 277 // create icon text 278 iconText := ast.NewString([]byte("#")) 279 anchor.AppendChild(anchor, iconText) 280 281 // set class on heading 282 h.SetAttribute([]byte("class"), []byte("heading")) 283 284 // append anchor to heading 285 h.AppendChild(h, anchor) 286} 287 288// actualPath decides when to join the file path with the 289// current repository directory (essentially only when the link 290// destination is relative. if it's absolute then we assume the 291// user knows what they're doing.) 292func (rctx *RenderContext) actualPath(dst string) string { 293 if path.IsAbs(dst) { 294 return dst 295 } 296 297 return path.Join(rctx.CurrentDir, dst) 298} 299 300func isAbsoluteUrl(link string) bool { 301 parsed, err := url.Parse(link) 302 if err != nil { 303 return false 304 } 305 return parsed.IsAbs() 306} 307 308func isFragment(link string) bool { 309 return strings.HasPrefix(link, "#") 310} 311 312func isMail(link string) bool { 313 return strings.HasPrefix(link, "mailto:") 314}