vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Show a model from a document, and redraw the viewer
0be968244db3882594fa65032ff6b4cf59385bb9 · Keenan Nemetz · 11d ago · parent ffbcb4cc
Show a model from a document, and redraw the viewer A README that names a 3D model with an image path was emitting an <img> of a mesh. The raw route serves model/stl, no browser draws that, so the reader saw a broken image where the rack was supposed to be. A model image in a document now becomes the same viewer host the code browser writes, with the image's alt text as its label and the download link inside it for a reader without JavaScript or WebGL. Only a document with a place in a tree does this: an issue body has no raw route to fetch. The sanitiser admits exactly that host's shape, so the one classed element in a rendered document is the one the renderer wrote. The viewer also never redrew after OrbitControls moved the camera: it renders on demand, and only the button handlers called render. A drag or a wheel zoom changed the camera and left the old frame on screen, which is why the viewer looked static. Listen for the controls' change event, add zoom in/out buttons clamped to the wheel's own limits, and give the canvas a grab cursor. Docs now say a Markdown path names a model the same way it names a picture, and that the code browser's size cap cannot apply to a document rendered from its text alone.
server/web/markdown.go +127−6
| @@ -7,6 +7,7 @@ package web | |||
| 7 | 7 | ||
| 8 | 8 | import ( | |
| 9 | 9 | "bytes" | |
| 10 | "html" | ||
| 10 | 11 | "html/template" | |
| 11 | 12 | "net/url" | |
| 12 | 13 | "path" | |
| @@ -17,15 +18,17 @@ import ( | |||
| 17 | 18 | "github.com/yuin/goldmark" | |
| 18 | 19 | "github.com/yuin/goldmark/ast" | |
| 19 | 20 | "github.com/yuin/goldmark/extension" | |
| 21 | "github.com/yuin/goldmark/renderer" | ||
| 20 | 22 | "github.com/yuin/goldmark/text" | |
| 23 | "github.com/yuin/goldmark/util" | ||
| 21 | 24 | ||
| 22 | 25 | "rodent.software/vole/forge/instancerepo" | |
| 23 | 26 | "rodent.software/vole/metadata" | |
| 24 | 27 | ) | |
| 25 | 28 | ||
| 26 | 29 | var ( | |
| 27 | markdown = goldmark.New(goldmark.WithExtensions(extension.GFM, extension.Typographer)) | ||
| 28 | sanitizer = bluemonday.UGCPolicy() | ||
| 30 | markdown = goldmark.New(goldmark.WithExtensions(extension.GFM, extension.Typographer, modelPreviews{})) | ||
| 31 | sanitizer = newSanitizer() | ||
| 29 | 32 | // xrefRE matches the two ways a body names another entity: the durable ULID and | |
| 30 | 33 | // the short alias. The alias is deliberately only digits, so it can never be | |
| 31 | 34 | // confused with a ULID prefix, and a number longer than nine digits is not a | |
| @@ -94,7 +97,7 @@ func (w *Web) renderTreeMarkdown(doc treeDoc, src string) template.HTML { | |||
| 94 | 97 | } | |
| 95 | 98 | source := []byte(src) | |
| 96 | 99 | parsed := markdown.Parser().Parse(text.NewReader(source)) | |
| 97 | rewriteTreeLinks(parsed, doc) | ||
| 100 | rewriteTreeLinks(parsed, doc, source) | ||
| 98 | 101 | if doc.Mentions { | |
| 99 | 102 | w.linkMentions(parsed, source) | |
| 100 | 103 | } | |
| @@ -109,7 +112,11 @@ func (w *Web) renderTreeMarkdown(doc treeDoc, src string) template.HTML { | |||
| 109 | 112 | // file they name. It works on the parsed tree rather than on the markdown text so | |
| 110 | 113 | // that every destination goldmark understands is covered the same way, including | |
| 111 | 114 | // reference-style links and images. | |
| 112 | func rewriteTreeLinks(doc ast.Node, tree treeDoc) { | ||
| 115 | // | ||
| 116 | // An image whose target is a 3D model becomes a viewer host instead of an <img>: | ||
| 117 | // the bytes are a mesh, so a browser told to draw them shows a broken image. | ||
| 118 | func rewriteTreeLinks(doc ast.Node, tree treeDoc, source []byte) { | ||
| 119 | var previews []modelImage | ||
| 113 | 120 | ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { | |
| 114 | 121 | if !entering { | |
| 115 | 122 | return ast.WalkContinue, nil | |
| @@ -120,12 +127,126 @@ func rewriteTreeLinks(doc ast.Node, tree treeDoc) { | |||
| 120 | 127 | v.Destination = []byte(href) | |
| 121 | 128 | } | |
| 122 | 129 | case *ast.Image: | |
| 123 | if href, ok := docHref(tree, string(v.Destination), true); ok { | ||
| 124 | v.Destination = []byte(href) | ||
| 130 | href, ok := docHref(tree, string(v.Destination), true) | ||
| 131 | if !ok { | ||
| 132 | return ast.WalkContinue, nil | ||
| 133 | } | ||
| 134 | v.Destination = []byte(href) | ||
| 135 | // Only a document in a tree resolves to a raw route the viewer can | ||
| 136 | // fetch, so an issue body or the instance introduction keeps its | ||
| 137 | // images as written. | ||
| 138 | format, name := "", "" | ||
| 139 | if tree.Ref != "" && !tree.Instance { | ||
| 140 | format, name = previewModel(href) | ||
| 141 | } | ||
| 142 | if format == "" { | ||
| 143 | return ast.WalkContinue, nil | ||
| 125 | 144 | } | |
| 145 | title := strings.TrimSpace(string(v.Text(source))) | ||
| 146 | if title == "" { | ||
| 147 | title = name | ||
| 148 | } | ||
| 149 | previews = append(previews, modelImage{image: v, preview: &modelPreview{ | ||
| 150 | src: href, format: format, title: title, name: name, | ||
| 151 | }}) | ||
| 126 | 152 | } | |
| 127 | 153 | return ast.WalkContinue, nil | |
| 128 | 154 | }) | |
| 155 | // Replace after the walk: swapping a node the walker is standing on leaves | ||
| 156 | // it visiting a tree that no longer exists. | ||
| 157 | for _, p := range previews { | ||
| 158 | if parent := p.image.Parent(); parent != nil { | ||
| 159 | parent.ReplaceChild(parent, p.image, p.preview) | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | // modelImage is one image on its way to becoming a viewer: the node to replace | ||
| 165 | // and the node that replaces it. | ||
| 166 | type modelImage struct { | ||
| 167 | image *ast.Image | ||
| 168 | preview *modelPreview | ||
| 169 | } | ||
| 170 | |||
| 171 | // previewModel names the browser loader for a resolved href and the file it | ||
| 172 | // downloads, or "" when the file is not a model. Both come from the URL path, so a | ||
| 173 | // query or fragment on the link does not hide the extension or enter the name. | ||
| 174 | func previewModel(href string) (format, name string) { | ||
| 175 | u, err := url.Parse(href) | ||
| 176 | if err != nil { | ||
| 177 | return "", "" | ||
| 178 | } | ||
| 179 | return modelFormat(u.Path), path.Base(u.Path) | ||
| 180 | } | ||
| 181 | |||
| 182 | // modelPreview is the node a markdown image becomes when it names a model. It | ||
| 183 | // renders as the same viewer host the code browser writes, so a README can show | ||
| 184 | // an assembly, and the download link stays for a reader without JavaScript or | ||
| 185 | // WebGL. It is inline rather than a block because an image may sit in the middle | ||
| 186 | // of a sentence; CSS gives the host its own box either way. | ||
| 187 | type modelPreview struct { | ||
| 188 | ast.BaseInline | ||
| 189 | src string | ||
| 190 | format string | ||
| 191 | // title labels the preview; name is what the fallback offers to download. | ||
| 192 | title string | ||
| 193 | name string | ||
| 194 | } | ||
| 195 | |||
| 196 | var modelPreviewKind = ast.NewNodeKind("VoleModelPreview") | ||
| 197 | |||
| 198 | func (n *modelPreview) Kind() ast.NodeKind { return modelPreviewKind } | ||
| 199 | |||
| 200 | func (n *modelPreview) Dump(source []byte, level int) { | ||
| 201 | ast.DumpHelper(n, source, level, map[string]string{ | ||
| 202 | "src": n.src, "format": n.format, "title": n.title, "name": n.name, | ||
| 203 | }, nil) | ||
| 204 | } | ||
| 205 | |||
| 206 | // modelPreviews teaches the renderer the one node this package adds to what | ||
| 207 | // goldmark can write. There is no parser for it: a node is only ever created by | ||
| 208 | // rewriteTreeLinks, after the document has been parsed. | ||
| 209 | type modelPreviews struct{} | ||
| 210 | |||
| 211 | func (modelPreviews) Extend(m goldmark.Markdown) { | ||
| 212 | m.Renderer().AddOptions(renderer.WithNodeRenderers( | ||
| 213 | util.Prioritized(modelPreviewRenderer{}, 500), | ||
| 214 | )) | ||
| 215 | } | ||
| 216 | |||
| 217 | type modelPreviewRenderer struct{} | ||
| 218 | |||
| 219 | func (modelPreviewRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { | ||
| 220 | reg.Register(modelPreviewKind, renderModelPreview) | ||
| 221 | } | ||
| 222 | |||
| 223 | func renderModelPreview(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
| 224 | if !entering { | ||
| 225 | return ast.WalkContinue, nil | ||
| 226 | } | ||
| 227 | preview := node.(*modelPreview) | ||
| 228 | src := html.EscapeString(preview.src) | ||
| 229 | _, _ = w.WriteString(`<span class="model-viewer" data-model="` + src + | ||
| 230 | `" data-format="` + preview.format + `" data-title="` + html.EscapeString(preview.title) + `">`) | ||
| 231 | _, _ = w.WriteString(`<span class="model-fallback"><a href="` + src + `">Download ` + | ||
| 232 | html.EscapeString(preview.name) + `</a> to open it in a 3D application.</span></span>`) | ||
| 233 | return ast.WalkSkipChildren, nil | ||
| 234 | } | ||
| 235 | |||
| 236 | // newSanitizer is the UGC policy plus the one element this package writes itself: | ||
| 237 | // the viewer host a model image becomes. goldmark has already dropped raw HTML by | ||
| 238 | // the time this runs, but the policy still admits by shape rather than by trust — | ||
| 239 | // the viewer class on a span, a same-origin model path, a format the browser has a | ||
| 240 | // loader for — so the one classed element in a rendered document is the one the | ||
| 241 | // renderer wrote, and no class a later extension lets through can style a page. | ||
| 242 | func newSanitizer() *bluemonday.Policy { | ||
| 243 | p := bluemonday.UGCPolicy() | ||
| 244 | p.AllowAttrs("class").Matching(regexp.MustCompile(`^model-viewer$`)).OnElements("span") | ||
| 245 | p.AllowAttrs("class").Matching(regexp.MustCompile(`^model-fallback$`)).OnElements("span") | ||
| 246 | p.AllowAttrs("data-model").Matching(regexp.MustCompile(`^/[^/]`)).OnElements("span") | ||
| 247 | p.AllowAttrs("data-format").Matching(regexp.MustCompile(`^(3mf|glb|gltf|obj|stl)$`)).OnElements("span") | ||
| 248 | p.AllowAttrs("data-title").OnElements("span") | ||
| 249 | return p | ||
| 129 | 250 | } | |
| 130 | 251 | ||
| 131 | 252 | // docHref resolves one destination written in a tree document into the URL that | |