forked from denoland/deno-gfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
239 lines (224 loc) · 6.55 KB
/
mod.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import {
emojify,
htmlEscape,
katex,
Marked,
Prism,
sanitizeHtml,
} from "./deps.ts";
import { CSS, KATEX_CLASSES, KATEX_CSS } from "./style.js";
export { CSS, KATEX_CSS };
class Renderer extends Marked.Renderer {
heading(
text: string,
level: 1 | 2 | 3 | 4 | 5 | 6,
raw: string,
slugger: Marked.Slugger,
): string {
const slug = slugger.slug(raw);
return `<h${level} id="${slug}"><a class="anchor" aria-hidden="true" tabindex="-1" href="#${slug}"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>${text}</h${level}>`;
}
image(src: string, title: string | null, alt: string | null) {
return `<img src="${src}" alt="${alt ?? ""}" title="${title ?? ""}" />`;
}
code(code: string, language?: string) {
// a language of `ts, ignore` should really be `ts`
// and it should be lowercase to ensure it has parity with regular github markdown
language = language?.split(",")?.[0].toLocaleLowerCase();
// transform math code blocks into HTML+MathML
// https://github.blog/changelog/2022-06-28-fenced-block-syntax-for-mathematical-expressions/
if (language === "math") {
return katex.renderToString(code, { displayMode: true });
}
const grammar =
language && Object.hasOwnProperty.call(Prism.languages, language)
? Prism.languages[language]
: undefined;
if (grammar === undefined) {
return `<pre><code class="notranslate">${htmlEscape(code)}</code></pre>`;
}
const html = Prism.highlight(code, grammar, language!);
return `<div class="highlight highlight-source-${language} notranslate"><pre>${html}</pre></div>`;
}
link(href: string, title: string | null, text: string) {
const titleAttr = title ? ` title="${title}"` : "";
if (href.startsWith("#")) {
return `<a href="${href}"${titleAttr}>${text}</a>`;
}
if (this.options.baseUrl) {
href = new URL(href, this.options.baseUrl).href;
}
return `<a href="${href}"${titleAttr} rel="noopener noreferrer">${text}</a>`;
}
}
/** Convert inline and block math to katex */
function mathify(markdown: string) {
// Deal with block math
const blockMath = /\$\$\s(.+?)\s\$\$/s;
let match;
while ((match = blockMath.exec(markdown)) !== null) {
markdown = markdown.replace(
blockMath,
katex.renderToString(match[1].trim(), { displayMode: true }),
);
}
// Deal with inline math
const inlineMath = /\s\$((?=\S).*?(?=\S))\$/;
while ((match = inlineMath.exec(markdown)) !== null) {
markdown = markdown.replace(
inlineMath,
" " + katex.renderToString(match[1], { displayMode: false }),
);
}
return markdown;
}
export interface RenderOptions {
baseUrl?: string;
mediaBaseUrl?: string;
inline?: boolean;
allowIframes?: boolean;
allowMath?: boolean;
disableHtmlSanitization?: boolean;
}
export function render(markdown: string, opts: RenderOptions = {}): string {
opts.mediaBaseUrl ??= opts.baseUrl;
markdown = emojify(markdown);
markdown = mathify(markdown);
const marked_opts = {
baseUrl: opts.baseUrl,
gfm: true,
renderer: new Renderer(),
};
const html = opts.inline
? Marked.marked.parseInline(markdown, marked_opts)
: Marked.marked.parse(markdown, marked_opts);
if (opts.disableHtmlSanitization) {
return html;
}
let allowedTags = sanitizeHtml.defaults.allowedTags.concat([
"img",
"video",
"svg",
"path",
"circle",
"figure",
"figcaption",
"del",
"details",
"summary",
]);
if (opts.allowIframes) {
allowedTags.push("iframe");
}
if (opts.allowMath) {
allowedTags = allowedTags.concat([
"math",
"maction",
"annotation",
"annotation-xml",
"menclose",
"merror",
"mfenced",
"mfrac",
"mi",
"mmultiscripts",
"mn",
"mo",
"mover",
"mpadded",
"mphantom",
"mprescripts",
"mroot",
"mrow",
"ms",
"semantics",
"mspace",
"msqrt",
"mstyle",
"msub",
"msup",
"msubsup",
"mtable",
"mtd",
"mtext",
"mtr",
]);
}
function transformMedia(tagName: string, attribs: sanitizeHtml.Attributes) {
if (opts.mediaBaseUrl && attribs.src) {
try {
attribs.src = new URL(attribs.src, opts.mediaBaseUrl).href;
} catch {
delete attribs.src;
}
}
return { tagName, attribs };
}
return sanitizeHtml(html, {
transformTags: {
img: transformMedia,
video: transformMedia,
},
allowedTags,
allowedAttributes: {
...sanitizeHtml.defaults.allowedAttributes,
img: ["src", "alt", "height", "width", "align"],
video: [
"src",
"alt",
"height",
"width",
"autoplay",
"muted",
"loop",
"playsinline",
"poster",
],
a: ["id", "aria-hidden", "href", "tabindex", "rel", "target"],
svg: ["viewbox", "width", "height", "aria-hidden", "background"],
path: ["fill-rule", "d"],
circle: ["cx", "cy", "r", "stroke", "stroke-width", "fill", "alpha"],
span: opts.allowMath ? ["aria-hidden", "style"] : [],
h1: ["id"],
h2: ["id"],
h3: ["id"],
h4: ["id"],
h5: ["id"],
h6: ["id"],
iframe: ["src", "width", "height"], // Only used when iframe tags are allowed in the first place.
math: ["xmlns"], // Only enabled when math is enabled
annotation: ["encoding"], // Only enabled when math is enabled
},
allowedClasses: {
div: ["highlight", "highlight-source-*", "notranslate"],
span: [
"token",
"keyword",
"operator",
"number",
"boolean",
"function",
"string",
"comment",
"class-name",
"regex",
"regex-delimiter",
"tag",
"attr-name",
"punctuation",
"script-punctuation",
"script",
"plain-text",
"property",
"prefix",
"line",
"deleted",
"inserted",
...(opts.allowMath ? KATEX_CLASSES : []),
],
a: ["anchor"],
svg: ["octicon", "octicon-link"],
},
allowProtocolRelative: false,
});
}