@@ -1,67 +0,0 @@ |
| 1 | | -// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | | - |
| 3 | | -// Package markdown wraps Goldmark + bluemonday for safe README |
| 4 | | -// rendering. S25 will broaden this with auto-mention, issue-ref |
| 5 | | -// linking, and cross-repo extensions; S17 ships only what's needed |
| 6 | | -// for tree-page README rendering. |
| 7 | | -package markdown |
| 8 | | - |
| 9 | | -import ( |
| 10 | | - "bytes" |
| 11 | | - |
| 12 | | - "github.com/microcosm-cc/bluemonday" |
| 13 | | - "github.com/yuin/goldmark" |
| 14 | | - "github.com/yuin/goldmark/extension" |
| 15 | | - "github.com/yuin/goldmark/parser" |
| 16 | | - "github.com/yuin/goldmark/renderer/html" |
| 17 | | -) |
| 18 | | - |
| 19 | | -// gm is the shared Goldmark instance. CommonMark + GFM (tables, |
| 20 | | -// strikethrough, autolinks, task-list) + auto-heading-id for in-page |
| 21 | | -// anchors. We deliberately do NOT enable HTML passthrough; raw HTML |
| 22 | | -// in user content is escaped. |
| 23 | | -var gm = goldmark.New( |
| 24 | | - goldmark.WithExtensions( |
| 25 | | - extension.GFM, |
| 26 | | - extension.Footnote, |
| 27 | | - ), |
| 28 | | - goldmark.WithParserOptions(parser.WithAutoHeadingID()), |
| 29 | | - goldmark.WithRendererOptions( |
| 30 | | - html.WithHardWraps(), |
| 31 | | - html.WithXHTML(), |
| 32 | | - ), |
| 33 | | -) |
| 34 | | - |
| 35 | | -// sanitizer is bluemonday's UGC policy with two adjustments: |
| 36 | | -// - allow class attributes on `<code>` (Goldmark emits language-foo) |
| 37 | | -// - allow `id` on headings so anchor links work |
| 38 | | -// |
| 39 | | -// Anything Goldmark emits passes through; anything user-injected via |
| 40 | | -// raw HTML in markdown gets stripped because Goldmark didn't enable |
| 41 | | -// HTML rendering in the first place. Defense in depth. |
| 42 | | -var sanitizer = func() *bluemonday.Policy { |
| 43 | | - p := bluemonday.UGCPolicy() |
| 44 | | - p.AllowAttrs("class").Matching(bluemonday.SpaceSeparatedTokens).OnElements("code", "pre", "span") |
| 45 | | - p.AllowAttrs("id").OnElements("h1", "h2", "h3", "h4", "h5", "h6") |
| 46 | | - // Disallow remote images outright; readme images normally live in |
| 47 | | - // the same repo and resolve to /raw/ which we control. Users who |
| 48 | | - // want external images can paste links instead. |
| 49 | | - p.AllowImages() |
| 50 | | - return p |
| 51 | | -}() |
| 52 | | - |
| 53 | | -// RenderHTML returns sanitized HTML for the given markdown bytes. |
| 54 | | -// Empty input returns an empty string. The output is suitable for |
| 55 | | -// inserting into a template via `{{ . | safeHTML }}` — every byte has |
| 56 | | -// passed bluemonday. |
| 57 | | -func RenderHTML(src []byte) (string, error) { |
| 58 | | - if len(src) == 0 { |
| 59 | | - return "", nil |
| 60 | | - } |
| 61 | | - var buf bytes.Buffer |
| 62 | | - if err := gm.Convert(src, &buf); err != nil { |
| 63 | | - return "", err |
| 64 | | - } |
| 65 | | - clean := sanitizer.SanitizeBytes(buf.Bytes()) |
| 66 | | - return string(clean), nil |
| 67 | | -} |