|
| 1 | +// Imports |
| 2 | +import { Renderer } from "../renderer.ts" |
| 3 | +import gfm from "../plugins/gfm.ts" |
| 4 | +import { create as sanitize } from "../plugins/sanitize.ts" |
| 5 | +import highlighting from "../plugins/highlighting.ts" |
| 6 | + |
| 7 | +/** Renderer instance. */ |
| 8 | +const renderer = new Renderer({ |
| 9 | + plugins: [ |
| 10 | + gfm, |
| 11 | + sanitize({ |
| 12 | + tagNames: [ |
| 13 | + // Headers |
| 14 | + "h1", |
| 15 | + "h2", |
| 16 | + "h3", |
| 17 | + "h4", |
| 18 | + "h5", |
| 19 | + "h6", |
| 20 | + // Text |
| 21 | + "p", |
| 22 | + "strong", |
| 23 | + "em", |
| 24 | + "del", |
| 25 | + "sup", |
| 26 | + "sub", |
| 27 | + // Blockquotes |
| 28 | + "blockquote", |
| 29 | + // Code |
| 30 | + "pre", |
| 31 | + "code", |
| 32 | + "kbd", |
| 33 | + // Links |
| 34 | + "a", |
| 35 | + // Images |
| 36 | + "img", |
| 37 | + // Tables |
| 38 | + "table", |
| 39 | + "thead", |
| 40 | + "tbody", |
| 41 | + "tfoot", |
| 42 | + "tr", |
| 43 | + "th", |
| 44 | + "td", |
| 45 | + // Lists |
| 46 | + "ul", |
| 47 | + "ol", |
| 48 | + "li", |
| 49 | + "input", |
| 50 | + // Horizontal rules |
| 51 | + "hr", |
| 52 | + // Line breaks |
| 53 | + "br", |
| 54 | + ], |
| 55 | + strip: ["script"], |
| 56 | + required: { |
| 57 | + input: { disabled: true, type: "checkbox" }, |
| 58 | + }, |
| 59 | + ancestors: { |
| 60 | + tbody: ["table"], |
| 61 | + td: ["table"], |
| 62 | + th: ["table"], |
| 63 | + thead: ["table"], |
| 64 | + tfoot: ["table"], |
| 65 | + tr: ["table"], |
| 66 | + }, |
| 67 | + attributes: { |
| 68 | + a: ["href"], |
| 69 | + code: [["className", /^language-./]], |
| 70 | + img: ["src", "alt"], |
| 71 | + input: [["disabled", true], ["type", "checkbox"], "checked"], |
| 72 | + li: [["className", "task-list-item"]], |
| 73 | + ol: [["className", "contains-task-list"]], |
| 74 | + ul: [["className", "contains-task-list"]], |
| 75 | + "*": ["align", "alt", "height", "width", "title", "width"], |
| 76 | + }, |
| 77 | + protocols: { |
| 78 | + href: ["http", "https"], |
| 79 | + src: ["http", "https", "data"], |
| 80 | + }, |
| 81 | + }), |
| 82 | + highlighting, |
| 83 | + ], |
| 84 | +}) |
| 85 | + |
| 86 | +/** |
| 87 | + * Renders a markdown expression suitable for SVG (sanitized with a subset of tags and attributes). |
| 88 | + */ |
| 89 | +export function markdown(text: string): Promise<string> { |
| 90 | + return renderer.render(text) |
| 91 | +} |
0 commit comments