All files / markdown / presets / svg.ts

100.00% Branches 0/0
100.00% Lines 73/73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
x1
x1
x1
x1
 
 
x1
x1
x1
x1
x1
 
x1
x1
x1
x1
x1
x1
 
x1
x1
x1
x1
x1
x1
 
x1
 
x1
x1
x1
 
x1
 
x1
 
x1
x1
x1
x1
x1
x1
x1
 
x1
x1
x1
x1
 
x1
 
x1
x1
x3
x1
x4
x1
x1
x3
x3
x3
x3
x3
x2
x1
x1
x3
x5
x4
x11
x5
x5
x5
x7
x1
x1
x4
x4
x1
x1
x1
 
x1
 
 
 
 
x1
x4
x4
























































































// Imports
import { Renderer } from "../renderer.ts"
import gfm from "../plugins/gfm.ts"
import { create as sanitize } from "../plugins/sanitize.ts"
import highlighting from "../plugins/highlighting.ts"

/** Renderer instance. */
const renderer = new Renderer({
  plugins: [
    gfm,
    sanitize({
      tagNames: [
        // Headers
        "h1",
        "h2",
        "h3",
        "h4",
        "h5",
        "h6",
        // Text
        "p",
        "strong",
        "em",
        "del",
        "sup",
        "sub",
        // Blockquotes
        "blockquote",
        // Code
        "pre",
        "code",
        "kbd",
        // Links
        "a",
        // Images
        "img",
        // Tables
        "table",
        "thead",
        "tbody",
        "tfoot",
        "tr",
        "th",
        "td",
        // Lists
        "ul",
        "ol",
        "li",
        "input",
        // Horizontal rules
        "hr",
        // Line breaks
        "br",
      ],
      strip: ["script"],
      required: {
        input: { disabled: true, type: "checkbox" },
      },
      ancestors: {
        tbody: ["table"],
        td: ["table"],
        th: ["table"],
        thead: ["table"],
        tfoot: ["table"],
        tr: ["table"],
      },
      attributes: {
        a: ["href"],
        code: [["className", /^language-./]],
        img: ["src", "alt"],
        input: [["disabled", true], ["type", "checkbox"], "checked"],
        li: [["className", "task-list-item"]],
        ol: [["className", "contains-task-list"]],
        ul: [["className", "contains-task-list"]],
        "*": ["align", "alt", "height", "width", "title", "width"],
      },
      protocols: {
        href: ["http", "https"],
        src: ["http", "https", "data"],
      },
    }),
    highlighting,
  ],
})

/**
 * Renders a markdown expression suitable for SVG (sanitized with a subset of tags and attributes).
 */
export function markdown(text: string): Promise<string> {
  return renderer.render(text)
}