-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathmarkdown.ts
More file actions
350 lines (329 loc) · 11 KB
/
Copy pathmarkdown.ts
File metadata and controls
350 lines (329 loc) · 11 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* eslint-disable import/no-named-as-default-member */
import {createHash} from "node:crypto";
import matter from "gray-matter";
import he from "he";
import MarkdownIt from "markdown-it";
import type {RuleCore} from "markdown-it/lib/parser_core.js";
import type {RuleInline} from "markdown-it/lib/parser_inline.js";
import type {RenderRule} from "markdown-it/lib/renderer.js";
import MarkdownItAnchor from "markdown-it-anchor";
import type {Config} from "./config.js";
import {mergeStyle} from "./config.js";
import {parseInfo} from "./info.js";
import type {JavaScriptNode} from "./javascript/parse.js";
import {parseJavaScript} from "./javascript/parse.js";
import {relativePath} from "./path.js";
import {transpileSql} from "./sql.js";
import {transpileTag} from "./tag.js";
import {InvalidThemeError} from "./theme.js";
import {red} from "./tty.js";
export interface MarkdownCode {
id: string;
node: JavaScriptNode;
}
export interface MarkdownPage {
title: string | null;
html: string;
data: {[key: string]: any} | null;
style: string | null;
code: MarkdownCode[];
}
export interface ParseContext {
code: MarkdownCode[];
startLine: number;
currentLine: number;
path: string;
}
function uniqueCodeId(context: ParseContext, content: string): string {
const hash = createHash("sha256").update(content).digest("hex").slice(0, 8);
let id = hash;
let count = 1;
while (context.code.some((code) => code.id === id)) id = `${hash}-${count++}`;
return id;
}
function isFalse(attribute: string | undefined): boolean {
return attribute?.toLowerCase() === "false";
}
function getLiveSource(content: string, tag: string, attributes: Record<string, string>): string | undefined {
return tag === "js"
? content
: tag === "tex"
? transpileTag(content, "tex.block", true)
: tag === "html"
? transpileTag(content, "html.fragment", true)
: tag === "sql"
? transpileSql(content, attributes)
: tag === "svg"
? transpileTag(content, "svg.fragment", true)
: tag === "dot"
? transpileTag(content, "dot", false)
: tag === "mermaid"
? transpileTag(content, "await mermaid", false)
: undefined;
}
// TODO sourceLine and remap syntax error position; consider showing a code
// snippet along with the error. Also, consider whether we want to show the
// file name here.
//
// const message = error.message;
// if (verbose) {
// let warning = error.message;
// const match = /^(.+)\s\((\d+):(\d+)\)$/.exec(message);
// if (match) {
// const line = +match[2] + (options?.sourceLine ?? 0);
// const column = +match[3] + 1;
// warning = `${match[1]} at line ${line}, column ${column}`;
// } else if (options?.sourceLine) {
// warning = `${message} at line ${options.sourceLine + 1}`;
// }
// console.error(red(`${error.name}: ${warning}`));
// }
function makeFenceRenderer(baseRenderer: RenderRule): RenderRule {
return (tokens, idx, options, context: ParseContext, self) => {
const {path} = context;
const token = tokens[idx];
const {tag, attributes} = parseInfo(token.info);
token.info = tag;
let html = "";
let source: string | undefined;
try {
source = isFalse(attributes.run) ? undefined : getLiveSource(token.content, tag, attributes);
if (source != null) {
const id = uniqueCodeId(context, source);
// TODO const sourceLine = context.startLine + context.currentLine;
const node = parseJavaScript(source, {path});
context.code.push({id, node});
html += `<div id="cell-${id}" class="observablehq observablehq--block${
node.expression ? " observablehq--loading" : ""
}"></div>\n`;
}
} catch (error) {
if (!(error instanceof SyntaxError)) throw error;
html += `<div class="observablehq observablehq--block">
<div class="observablehq--inspect observablehq--error">SyntaxError: ${he.escape(error.message)}</div>
</div>\n`;
}
if (attributes.echo == null ? source == null : !isFalse(attributes.echo)) {
html += baseRenderer(tokens, idx, options, context, self);
}
return html;
};
}
const CODE_DOLLAR = 36;
const CODE_BRACEL = 123;
const CODE_BRACER = 125;
const CODE_BACKSLASH = 92;
const CODE_QUOTE = 34;
const CODE_SINGLE_QUOTE = 39;
const CODE_BACKTICK = 96;
function parsePlaceholder(content: string, replacer: (i: number, j: number) => void) {
let afterDollar = false;
for (let j = 0, n = content.length; j < n; ++j) {
const cj = content.charCodeAt(j);
if (cj === CODE_BACKSLASH) {
++j; // skip next character
continue;
}
if (cj === CODE_DOLLAR) {
afterDollar = true;
continue;
}
if (afterDollar) {
if (cj === CODE_BRACEL) {
let quote = 0; // TODO detect comments, too
let braces = 0;
let k = j + 1;
inner: for (; k < n; ++k) {
const ck = content.charCodeAt(k);
if (ck === CODE_BACKSLASH) {
++k;
continue;
}
if (quote) {
if (ck === quote) quote = 0;
continue;
}
switch (ck) {
case CODE_QUOTE:
case CODE_SINGLE_QUOTE:
case CODE_BACKTICK:
quote = ck;
break;
case CODE_BRACEL:
++braces;
break;
case CODE_BRACER:
if (--braces < 0) {
replacer(j - 1, k + 1);
break inner;
}
break;
}
}
j = k;
}
afterDollar = false;
}
}
}
function transformPlaceholderBlock(token) {
const input = token.content;
if (/^\s*<script[\s>]/.test(input)) return [token]; // ignore <script> elements
const output: any[] = [];
let i = 0;
parsePlaceholder(input, (j, k) => {
output.push({...token, level: i > 0 ? token.level + 1 : token.level, content: input.slice(i, j)});
output.push({type: "placeholder", level: token.level + 1, content: input.slice(j + 2, k - 1)});
i = k;
});
if (i === 0) return [token];
else if (i < input.length) output.push({...token, content: input.slice(i), nesting: -1});
return output;
}
const transformPlaceholderInline: RuleInline = (state, silent) => {
if (silent || state.pos + 2 > state.posMax) return false;
const marker1 = state.src.charCodeAt(state.pos);
const marker2 = state.src.charCodeAt(state.pos + 1);
if (!(marker1 === CODE_DOLLAR && marker2 === CODE_BRACEL)) return false;
let quote = 0;
let braces = 0;
for (let pos = state.pos + 2; pos < state.posMax; ++pos) {
const code = state.src.charCodeAt(pos);
if (code === CODE_BACKSLASH) {
++pos; // skip next character
continue;
}
if (quote) {
if (code === quote) quote = 0;
continue;
}
switch (code) {
case CODE_QUOTE:
case CODE_SINGLE_QUOTE:
case CODE_BACKTICK:
quote = code;
break;
case CODE_BRACEL:
++braces;
break;
case CODE_BRACER:
if (--braces < 0) {
const token = state.push("placeholder", "", 0);
token.content = state.src.slice(state.pos + 2, pos);
state.pos = pos + 1;
return true;
}
break;
}
}
return false;
};
const transformPlaceholderCore: RuleCore = (state) => {
const input = state.tokens;
const output: any[] = [];
for (const token of input) {
switch (token.type) {
case "html_block":
output.push(...transformPlaceholderBlock(token));
break;
default:
output.push(token);
break;
}
}
state.tokens = output;
};
function makePlaceholderRenderer(): RenderRule {
return (tokens, idx, options, context: ParseContext) => {
const {path} = context;
const token = tokens[idx];
const id = uniqueCodeId(context, token.content);
try {
// TODO sourceLine: context.startLine + context.currentLine
const node = parseJavaScript(token.content, {path, inline: true});
context.code.push({id, node});
return `<span id="cell-${id}" class="observablehq--loading"></span>`;
} catch (error) {
if (!(error instanceof SyntaxError)) throw error;
return `<span id="cell-${id}">
<span class="observablehq--inspect observablehq--error" style="display: block;">SyntaxError: ${he.escape(
error.message
)}</span>
</span>`;
}
};
}
function makeSoftbreakRenderer(baseRenderer: RenderRule): RenderRule {
return (tokens, idx, options, context: ParseContext, self) => {
context.currentLine++;
return baseRenderer(tokens, idx, options, context, self);
};
}
export interface ParseOptions {
root: string;
path: string;
style?: Config["style"];
md: MarkdownIt;
}
export function createMarkdownIt({markdownIt}: {markdownIt?: (md: MarkdownIt) => MarkdownIt} = {}): MarkdownIt {
const md = MarkdownIt({html: true, linkify: true});
md.linkify.set({fuzzyLink: false, fuzzyEmail: false});
md.use(MarkdownItAnchor, {permalink: MarkdownItAnchor.permalink.headerLink({class: "observablehq-header-anchor"})});
md.inline.ruler.push("placeholder", transformPlaceholderInline);
md.core.ruler.before("linkify", "placeholder", transformPlaceholderCore);
md.renderer.rules.placeholder = makePlaceholderRenderer();
md.renderer.rules.fence = makeFenceRenderer(md.renderer.rules.fence!);
md.renderer.rules.softbreak = makeSoftbreakRenderer(md.renderer.rules.softbreak!);
return markdownIt === undefined ? md : markdownIt(md);
}
export function parseMarkdown(input: string, {path, style: configStyle, md}: ParseOptions): MarkdownPage {
const parts = matter(input, {});
const code: MarkdownCode[] = [];
const context: ParseContext = {code, startLine: 0, currentLine: 0, path};
const tokens = md.parse(parts.content, context);
const html = md.renderer.render(tokens, md.options, context); // Note: mutates code, assets!
const style = getStylesheet(path, parts.data, configStyle);
return {
html,
data: isEmpty(parts.data) ? null : parts.data,
title: parts.data?.title ?? findTitle(tokens) ?? null,
style,
code
};
}
function getStylesheet(path: string, data: MarkdownPage["data"], style: Config["style"] = null): string | null {
try {
style = mergeStyle(path, data?.style, data?.theme, style);
} catch (error) {
if (!(error instanceof InvalidThemeError)) throw error;
console.error(red(String(error))); // TODO error during build
style = {theme: []};
}
return !style
? null
: "path" in style
? relativePath(path, style.path)
: `observablehq:theme-${style.theme.join(",")}.css`;
}
// TODO Use gray-matter’s parts.isEmpty, but only when it’s accurate.
function isEmpty(object) {
for (const key in object) return false;
return true;
}
// TODO Make this smarter.
function findTitle(tokens: ReturnType<MarkdownIt["parse"]>): string | undefined {
for (const [i, token] of tokens.entries()) {
if (token.type === "heading_open" && token.tag === "h1") {
const next = tokens[i + 1];
if (next?.type === "inline") {
const text = next.children
?.filter((t) => t.type === "text")
.map((t) => t.content)
.join("");
if (text) {
return text;
}
}
}
}
}