-
-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathindex.js
More file actions
335 lines (294 loc) Β· 8.72 KB
/
index.js
File metadata and controls
335 lines (294 loc) Β· 8.72 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { marked } from 'marked';
import dedent from 'dedent';
import Prism from 'prismjs';
import loadLanguages from 'prismjs/components/';
import { parse } from 'node-html-parser';
import { replace } from './gh-emoji/index.js';
import { textToBase64 } from '../../src/components/controllers/repl/query-encode.js';
import { parseFrontmatter } from '../../src/lib/frontmatter.js';
// Prism will always load `markup`, `css`, `clike` and `javascript` by default.
// Any additional languages we need should be loaded here
loadLanguages(['typescript', 'tsx', 'json', 'bash', 'diff']);
/**
* @param {string} content
* @param {string} path
* @returns {Promise<string>}
*/
export async function precompileMarkdown(content, path) {
const parsed = parseContent(content, path);
const emojified = applyEmojiToContent(parsed);
const htmlified = await markdownToHTML(emojified);
const result = highlightCodeBlocks(htmlified);
// client only needs `.html` and `.meta` fields
delete result.content;
return JSON.stringify(result);
}
/**
* Parse Markdown with YAML FrontMatter into a data structure that can be reasoned about:
* {
* content: "<html here>",
* meta: { toc: [], title: "" }
* }
*
* @param {string} content
* @param {string} path
*/
function parseContent(content, path) {
const { body, meta } = parseFrontmatter(content, path);
let processedContent = body;
if (path.includes('/guide/')) {
meta.toc = generateToc(processedContent);
}
// extract tutorial setup, initial and final code blocks
if (/tutorial\/\d/.test(path)) {
const { markdown, tutorial } = extractTutorialCodeBlocks(processedContent);
processedContent = markdown;
meta.tutorial = tutorial;
}
return {
content: processedContent,
meta
};
}
marked.use({
renderer: {
heading({ text, depth }) {
// No need to add links for page titles
if (depth === 1) return `<h${depth}>${text}</h${depth}>`;
const id = generateHeadingId(text);
return `
<h${depth} id="${id}">
<a class="fragment-link" href="#${id}">
<svg width="16" height="16" viewBox="0 0 24 24" aria-label="Link to: ${text} (#${id})">
<use href="#link" />
</svg>
</a>
<span>${text}</span>
</h${depth}>`;
},
paragraph({ text }) {
if (text == '<toc></toc>' || text == '<branding></branding>') {
// The CommonMark spec states that _HTML Blocks_ must start with specific & known
// tags, which our custom components are not. As such, `marked` treats them as
// _Raw HTML_ which results in them being wrapped in a `<p>` tag.
// https://spec.commonmark.org/0.29/#html-blocks
return text;
}
return false;
},
link({ href, text }) {
if (href.includes('://')) {
return `<a href="${href}" target="_blank" rel="noopener noreferrer">${text}</a>`;
}
return `<a href="${href}">${text}</a>`;
},
image({ href, text }) {
return `<img loading="lazy" decoding="async" src="${href}" alt="${text}" />`;
},
code({ text, lang }) {
const [code, source, runInRepl] = processRepl(text.trim());
if (Prism.languages[lang] == null) {
throw new Error(
`No Prism highlighter for language: ${lang}\n\ncode:\n${code}\n`
);
}
text = Prism.highlight(code, Prism.languages[lang], lang);
const runInReplLink = runInRepl
? `<a class="repl-link" href="/repl?code=${encodeURIComponent(
textToBase64(source)
)}">Run in REPL</a>`
: '';
return `
<div class="highlight-container">
<pre class="highlight"><code class="language-${lang}">${text}</code></pre>
${runInReplLink}
</div>
`;
}
}
});
/**
* @param {{ content: string, html: string }} data
* @returns {Promise<{ html: string, content: string }>}
*/
async function markdownToHTML(data) {
data.html = await marked(data.content);
return data;
}
function applyEmojiToContent(data) {
data.content = applyEmoji(data.content);
return data;
}
/**
* @param {string} content
* @returns {string}
*/
function applyEmoji(content) {
if (!content.match(/([^\\]):[a-z0-9_]+:/gi)) {
return content;
}
return replace(content);
}
/* generate ToC from markdown */
function generateToc(markdown) {
const MARKDOWN_TITLE = /(?:^|\n\n)\s*(#{1,6})\s+(.+)/g;
const toc = [];
let token;
MARKDOWN_TITLE.lastIndex = 0;
while ((token = MARKDOWN_TITLE.exec(markdown))) {
const level = token[1].length;
const text = token[2];
const id = generateHeadingId(text);
toc.push({ text, id, level });
}
return toc;
}
/**
* @param {string} text
* @returns {string}
*/
function generateHeadingId(text) {
// Note: character range in regex is roughly "word characters including accented" (eg: bublΓ©)
return text
.toLowerCase()
.replace(/[\s-!<>`",]+/g, '-')
.replace(/^-|-$|[/&.()[\]']/g, '');
}
/**
* Split out tutorial code blocks that will be fed into the REPL
*
* @param {string} markdown
*/
function extractTutorialCodeBlocks(markdown) {
const SETUP_CODE_REG = /```js:setup(.+?)```/s;
const INTITAL_CODE_REG = /```jsx:repl-initial(.+?)```/s;
const FINAL_CODE_REG = /```jsx:repl-final(.+?)```/s;
const tutorial = {};
[
['setup', SETUP_CODE_REG],
['initial', INTITAL_CODE_REG],
['final', FINAL_CODE_REG]
].forEach(([key, regex]) => {
const match = markdown.match(regex);
if (match) {
tutorial[key] = match[1].trim();
markdown = markdown.replace(regex, '');
}
});
return { markdown, tutorial };
}
/**
* This is only for highlighting HTML code blocks in markdown as
* `marked` will ignore them
*
* @param {{ html: string, content: string }} data
* @returns {{ html: string, content: string }}
*/
function highlightCodeBlocks(data) {
const doc = parse(data.html, { blockTextElements: { code: true } });
// Only get the pre blocks that haven't already been highlighted
const codeBlocks = doc.querySelectorAll(
'pre:not([class="highlight"]):has(> code[class])'
);
for (const block of codeBlocks) {
const child = block.childNodes[0];
/**
* Slight hack to facilitate blank lines in code blocks in HTML in markdown, i.e.,
*
* <pre repl="false"><code class="language-jsx">
* import TodoList from './todo-list';<br>
* render(<TodoList />, document.body);
* </code></pre>
*
* Blank lines are an end condition to the code block so instead we must use `<br>`
* and switch it back to `\n` for the code content after marked is through with it.
* We only do this on the home/index page at the moment.
*/
const rawCodeBlockText = unescapeHTML(
dedent(child.innerText)
.trim()
.replaceAll('<br>', '\n')
);
const [code, source, runInRepl] = processRepl(rawCodeBlockText);
const lang = child.getAttribute('class').replace('language-', '');
if (Prism.languages[lang] == null) {
throw new Error(
`No Prism highlighter for language: ${lang}\n\ncode:\n${code}\n`
);
}
child.innerHTML = Prism.highlight(code, Prism.languages[lang], lang);
block.insertAdjacentHTML(
'beforebegin',
'<div class="highlight-container">'
);
const container = block.previousSibling;
container.appendChild(block);
block.setAttribute('class', 'highlight');
if (runInRepl) {
block.insertAdjacentHTML(
'afterend',
`<a class="repl-link" href="/repl?code=${encodeURIComponent(
textToBase64(source)
)}">
Run in REPL
</a>`
);
}
}
data.html = doc.toString();
return data;
}
/**
* Marked escapes HTML entities, which is normally great,
* but we want to feed the raw code into Prism for highlighting.
*
* @param {string} str
* @returns {string}
*/
function unescapeHTML(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'");
}
/**
* @param {string} code
* @returns {[string, string, boolean]}
*/
function processRepl(code) {
let source = code,
runInRepl = false;
if (code.startsWith('// --repl')) {
runInRepl = true;
const idx = code.indexOf('\n');
if (idx > -1) {
code = code.slice(idx + 1);
source = source.slice(idx + 1);
}
const beforeMarker = '// --repl-before';
const beforeIdx = code.indexOf(beforeMarker);
if (beforeIdx > -1) {
const pos = beforeIdx + beforeMarker.length + 1;
code = code.slice(pos);
// Only replace comment line with newline in source
source = source.slice(0, beforeIdx) + '\n' + source.slice(pos);
}
const afterMarker = '// --repl-after';
const afterIdx = code.indexOf(afterMarker);
if (afterIdx > -1) {
code = code.slice(0, afterIdx);
// Only replace comment line with newline in source
// ATTENTION: We cannot reuse the index from `code`
// as the content and thereby offsets are different
const sourceAfterIdx = source.indexOf(afterMarker);
source =
source.slice(0, sourceAfterIdx) +
'\n' +
source.slice(sourceAfterIdx + afterMarker.length + 1) +
'\n';
}
}
return [code, source, runInRepl];
}