@tiaforge-ui/sinter-markdown 0.0.0-stage → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +305 -2
- package/docs/markdown.md +258 -0
- package/docs/recipes/article-with-comments.html +28 -0
- package/llms.txt +44 -0
- package/package.json +65 -4
- package/sinter/components/internal/md-block.html +7 -0
- package/sinter/components/internal/md-blockquote.html +3 -0
- package/sinter/components/internal/md-break.html +3 -0
- package/sinter/components/internal/md-code.html +4 -0
- package/sinter/components/internal/md-delete.html +3 -0
- package/sinter/components/internal/md-details.html +4 -0
- package/sinter/components/internal/md-emphasis.html +3 -0
- package/sinter/components/internal/md-heading.html +4 -0
- package/sinter/components/internal/md-image.html +9 -0
- package/sinter/components/internal/md-inline-code.html +3 -0
- package/sinter/components/internal/md-kbd.html +3 -0
- package/sinter/components/internal/md-link.html +4 -0
- package/sinter/components/internal/md-list-item.html +4 -0
- package/sinter/components/internal/md-list.html +4 -0
- package/sinter/components/internal/md-mark.html +3 -0
- package/sinter/components/internal/md-paragraph.html +3 -0
- package/sinter/components/internal/md-phrasing.html +9 -0
- package/sinter/components/internal/md-strong.html +3 -0
- package/sinter/components/internal/md-sub.html +3 -0
- package/sinter/components/internal/md-sup.html +3 -0
- package/sinter/components/internal/md-table.html +9 -0
- package/sinter/components/internal/md-thematic-break.html +3 -0
- package/sinter/components/md-doc.html +10 -0
- package/sinter/components/md-inline.html +11 -0
- package/sinter/index.ts +5 -0
- package/sinter/parse.ts +270 -0
- package/sinter/parser.d.ts +19 -0
- package/sinter/parser.js +2872 -0
- package/sinter/sinter.helpers.ts +10 -0
- package/sinter/sinter.types.ts +209 -0
- package/sinter/tree.ts +61 -0
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiaforge-ui/sinter-markdown",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Markdown for sinter: a parser to mdast that folds at build time, and md-* components with slots for the nodes an app changes.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Tia Heinrich",
|
|
7
|
+
"homepage": "https://github.com/tiaforge/sinter/tree/main/packages/sinter-markdown#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/tiaforge/sinter.git",
|
|
11
|
+
"directory": "packages/sinter-markdown"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/tiaforge/sinter/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"sinter",
|
|
18
|
+
"markdown",
|
|
19
|
+
"mdast",
|
|
20
|
+
"commonmark",
|
|
21
|
+
"gfm",
|
|
22
|
+
"components"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"sinter": "./sinter",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./sinter/index.ts",
|
|
29
|
+
"./parser": "./sinter/parser.js",
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"sinter",
|
|
34
|
+
"!sinter/sinter.components.d.ts",
|
|
35
|
+
"docs",
|
|
36
|
+
"llms.txt",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build:parser": "node scripts/build-parser.mjs",
|
|
41
|
+
"check": "sinter check",
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"test": "vitest run"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@tiaforge-ui/sinter": ">=0.20.0 <1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@tiaforge-ui/sinter": "file:../..",
|
|
50
|
+
"@types/jsdom": "30.0.0",
|
|
51
|
+
"@types/node": "^26.6.6",
|
|
52
|
+
"character-entities": "2.0.2",
|
|
53
|
+
"jsdom": "30.1.3",
|
|
54
|
+
"markdown-it": "14.1.0",
|
|
55
|
+
"playwright": "1.63.0",
|
|
56
|
+
"rolldown": "1.2.16",
|
|
57
|
+
"typescript": "7.0.2",
|
|
58
|
+
"vite": "8.3.5",
|
|
59
|
+
"vitest": "5.1.0"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
One block, by its type: the only place block content is dispatched. To add a block type, add it to
|
|
3
|
+
Block in sinter.types.ts, write its component in this folder, and add it to this map.
|
|
4
|
+
-->
|
|
5
|
+
<template data-sn-component="{ node: Block }">
|
|
6
|
+
<sn-component use="{ paragraph: 'md-paragraph', heading: 'md-heading', thematicBreak: 'md-thematic-break', blockquote: 'md-blockquote', list: 'md-list', code: 'md-code', table: 'md-table', details: 'md-details' }[node.type]" data-sn-prop="{ node }"></sn-component>
|
|
7
|
+
</template>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<!-- A code block. A fill can highlight it: at build time, with a helper over the constant code. -->
|
|
2
|
+
<template data-sn-component="{ node: Code }">
|
|
3
|
+
<slot name="code" data-sn-prop="{ node }"><pre><code data-sn-attr="{ class: node.lang ? 'language-' + node.lang : null }" data-sn-text="node.value"></code></pre></slot>
|
|
4
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Delete }">
|
|
2
|
+
<del><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></del>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<!-- <details> with its <summary>, kept from raw HTML under md.parse's html: 'allow'. -->
|
|
2
|
+
<template data-sn-component="{ node: Details }">
|
|
3
|
+
<details data-sn-attr="{ open: node.open }"><template data-sn-if="node.summary.length"><summary><template data-sn-for="n in node.summary"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></summary></template><template data-sn-for="b in node.children"><md-block data-sn-prop="{ node: b }"></md-block></template></details>
|
|
4
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Emphasis }">
|
|
2
|
+
<em><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></em>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<!-- A heading: its root is the `heading` slot, whose default is an if chain over the depth. -->
|
|
2
|
+
<template data-sn-component="{ node: Heading }">
|
|
3
|
+
<slot name="heading" data-sn-prop="{ node }"><template data-sn-if="node.depth === 1"><h1 data-sn-attr="{ id: 'md-' + node.slug }"><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></h1></template><template data-sn-else><template data-sn-if="node.depth === 2"><h2 data-sn-attr="{ id: 'md-' + node.slug }"><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></h2></template><template data-sn-else><template data-sn-if="node.depth === 3"><h3 data-sn-attr="{ id: 'md-' + node.slug }"><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></h3></template><template data-sn-else><template data-sn-if="node.depth === 4"><h4 data-sn-attr="{ id: 'md-' + node.slug }"><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></h4></template><template data-sn-else><template data-sn-if="node.depth === 5"><h5 data-sn-attr="{ id: 'md-' + node.slug }"><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></h5></template><template data-sn-else><h6 data-sn-attr="{ id: 'md-' + node.slug }"><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></h6></template></template></template></template></template></slot>
|
|
4
|
+
</template>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
An image: a plain <img>. Over a file: resource it is a constant, and sinter optimizes a local file
|
|
3
|
+
as any image in a page. From an API it stays as written: the bound `srcset: null` tells sinter not to
|
|
4
|
+
send it through the app's resizing service (`sinter({ images: { remote } })`), since user content may
|
|
5
|
+
name any image. An app that wants it resized fills the `image` slot with its own <img>.
|
|
6
|
+
-->
|
|
7
|
+
<template data-sn-component="{ node: Image }">
|
|
8
|
+
<slot name="image" data-sn-prop="{ node }"><img data-sn-attr="{ src: node.url, alt: node.alt, title: node.title, srcset: null }"></slot>
|
|
9
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Kbd }">
|
|
2
|
+
<kbd><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></kbd>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<!-- A link: its URL is an attribute binding, so sinter refuses javascript: and other unsafe schemes. -->
|
|
2
|
+
<template data-sn-component="{ node: Link }">
|
|
3
|
+
<slot name="link" data-sn-prop="{ node }"><a data-sn-attr="{ href: node.url, title: node.title }"><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></a></slot>
|
|
4
|
+
</template>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<!-- An item: a task's checkbox, then its blocks; in a tight list a paragraph is its content alone, without <p>. -->
|
|
2
|
+
<template data-sn-component="{ node: ListItem; tight: boolean }">
|
|
3
|
+
<li data-sn-class="{ 'task-list-item': node.checked !== null }"><template data-sn-if="node.checked !== null"><input type="checkbox" disabled data-sn-attr="{ checked: node.checked }"></template><template data-sn-for="b in node.children"><template data-sn-if="tight && b.type === 'paragraph'"><template data-sn-for="n in b.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></template><template data-sn-else><md-block data-sn-prop="{ node: b }"></md-block></template></template></li>
|
|
4
|
+
</template>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<!-- A list: an if chain over `ordered`. Each item knows whether the list is tight (no <p> in its items). -->
|
|
2
|
+
<template data-sn-component="{ node: List }">
|
|
3
|
+
<template data-sn-if="node.ordered"><ol data-sn-attr="{ start: node.start === 1 ? null : node.start }"><template data-sn-for="i in node.children"><md-list-item data-sn-prop="{ node: i, tight: !node.spread }"></md-list-item></template></ol></template><template data-sn-else><ul><template data-sn-for="i in node.children"><md-list-item data-sn-prop="{ node: i, tight: !node.spread }"></md-list-item></template></ul></template>
|
|
4
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Mark }">
|
|
2
|
+
<mark><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></mark>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Paragraph }">
|
|
2
|
+
<p><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></p>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
One node of phrasing content other than text, by its type: the only place phrasing content is
|
|
3
|
+
dispatched. Text runs are not components (a root cannot be text), so each loop over phrasing content
|
|
4
|
+
writes them itself and passes every other node here. To add an inline type, add it to Inline in
|
|
5
|
+
sinter.types.ts, write its component in this folder, and add it to this map.
|
|
6
|
+
-->
|
|
7
|
+
<template data-sn-component="{ node: Phrasing }">
|
|
8
|
+
<sn-component use="{ emphasis: 'md-emphasis', strong: 'md-strong', delete: 'md-delete', inlineCode: 'md-inline-code', break: 'md-break', link: 'md-link', image: 'md-image', kbd: 'md-kbd', sub: 'md-sub', sup: 'md-sup', mark: 'md-mark' }[node.type]" data-sn-prop="{ node }"></sn-component>
|
|
9
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Strong }">
|
|
2
|
+
<strong><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></strong>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Sub }">
|
|
2
|
+
<sub><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></sub>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<template data-sn-component="{ node: Sup }">
|
|
2
|
+
<sup><template data-sn-for="n in node.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></sup>
|
|
3
|
+
</template>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
A table: the first row is the header, `align` per column as GitHub writes it (an inline style would
|
|
3
|
+
need 'unsafe-inline'). The slot's default is a wrapper around the table: the HTML parser moves a
|
|
4
|
+
<slot> written inside a <table> out of it, so a fill places the whole table (<slot></slot> in a
|
|
5
|
+
<div class="scroll">), or renders its own.
|
|
6
|
+
-->
|
|
7
|
+
<template data-sn-component="{ node: Table }">
|
|
8
|
+
<slot name="table" data-sn-prop="{ node }"><div class="md-table"><table><thead><template data-sn-for="r in node.children.slice(0, 1)"><tr><template data-sn-for="(c, k) in r.children"><th data-sn-attr="{ align: node.align[k] }"><template data-sn-for="n in c.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></th></template></tr></template></thead><template data-sn-if="node.children.length > 1"><tbody><template data-sn-for="r in node.children.slice(1)"><tr><template data-sn-for="(c, k) in r.children"><td data-sn-attr="{ align: node.align[k] }"><template data-sn-for="n in c.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></td></template></tr></template></tbody></template></table></div></slot>
|
|
9
|
+
</template>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
md-doc: a parsed document, block content. Public.
|
|
3
|
+
<md-doc data-sn-prop="{ doc }"></md-doc>, `doc` from md.parse (or an API that sends the tree).
|
|
4
|
+
Its root is a plain element, so the tag may carry an id or a class; with no doc it is empty.
|
|
5
|
+
The slots below are the library's whole contract: a fill changes how that kind of node renders,
|
|
6
|
+
wherever it is in the tree.
|
|
7
|
+
-->
|
|
8
|
+
<template data-sn-component="{ doc?: Root | null; heading: Slot<{ node: Heading }>; link: Slot<{ node: Link }>; image: Slot<{ node: Image }>; code: Slot<{ node: Code }>; inlineCode: Slot<{ node: InlineCode }>; blockquote: Slot<{ node: Blockquote }>; table: Slot<{ node: Table }> }">
|
|
9
|
+
<div class="md"><template data-sn-for="b in doc ? doc.children : []"><md-block data-sn-prop="{ node: b }"></md-block></template></div>
|
|
10
|
+
</template>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
md-inline: a parsed line, phrasing content. Public.
|
|
3
|
+
<h1><md-inline data-sn-prop="{ doc: md.parse(post.title) }"></md-inline></h1>: the content of the
|
|
4
|
+
document's paragraphs and headings, without their elements, one space between two of them; other
|
|
5
|
+
blocks (a list, a code block) are left out. Its root is a <span> with only phrasing content inside,
|
|
6
|
+
so it is valid in a heading, a label, a link or a paragraph. It places links, images and inline code, so those
|
|
7
|
+
are the slots it declares.
|
|
8
|
+
-->
|
|
9
|
+
<template data-sn-component="{ doc?: Root | null; link: Slot<{ node: Link }>; image: Slot<{ node: Image }>; inlineCode: Slot<{ node: InlineCode }> }">
|
|
10
|
+
<span class="md"><template data-sn-for="(b, i) in doc ? doc.children.filter((x) => x.type === 'paragraph' || x.type === 'heading') : []"><template data-sn-if="i > 0"><template data-sn-text="' '"></template></template><template data-sn-for="n in b.children"><template data-sn-if="n.type === 'text'"><template data-sn-text="n.value"></template></template><template data-sn-else><md-phrasing data-sn-prop="{ node: n }"></md-phrasing></template></template></template></span>
|
|
11
|
+
</template>
|
package/sinter/index.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The package's entry: the helpers, which an app re-exports as a namespace
|
|
2
|
+
// (`export * as md from '@tiaforge-ui/sinter-markdown'`), and the tree's types
|
|
3
|
+
// (`export type { Root } from '@tiaforge-ui/sinter-markdown'` in its src/sinter.types.ts).
|
|
4
|
+
export * from './sinter.helpers.js';
|
|
5
|
+
export type * from './sinter.types.js';
|
package/sinter/parse.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// `md.parse(source, options)`: markdown to the tree of sinter.types.ts. The parsing itself is the
|
|
2
|
+
// prebundled parser (parser.js, markdown-it); this module builds the tree from its tokens in one pass
|
|
3
|
+
// (no intermediate tree) and applies the options: what a document may contain is decided here, once per
|
|
4
|
+
// use, not by components that render nothing.
|
|
5
|
+
//
|
|
6
|
+
// Pure: the same source and options give the same tree, with no globals beyond the language's and
|
|
7
|
+
// `URL`. So sinter runs it at build time over a `file:` resource. In the browser a let or a prop is
|
|
8
|
+
// evaluated at every read, and md-doc's components read their `doc` at every node, so md.parse keeps
|
|
9
|
+
// its trees itself, with sinter's memo(): one parse per source and options, the tree frozen and shared
|
|
10
|
+
// (README.md, "md.parse keeps its trees").
|
|
11
|
+
// The parser by the package's own name, not `./parser.js`: an app makes it load on demand by listing
|
|
12
|
+
// this package in `sinter({ lazy })`, and lazy packages are matched by import specifier.
|
|
13
|
+
import { tokenize, type Token } from '@tiaforge-ui/sinter-markdown/parser';
|
|
14
|
+
import { memo } from '@tiaforge-ui/sinter/memo';
|
|
15
|
+
import { slug, text } from './tree.js';
|
|
16
|
+
import type { Block, Details, Heading, Inline, List, ListItem, ParseOptions, Root, Table, TableRow } from './sinter.types.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The tree of `source` (CommonMark, and GFM tables, task lists, strikethrough and autolinks), under
|
|
20
|
+
* `options`. The same source and options give the same tree, frozen: change a copy.
|
|
21
|
+
*/
|
|
22
|
+
export function parse(source: string | null | undefined, options?: ParseOptions): Root {
|
|
23
|
+
// the options normalized to a plain object of primitives, so equal policies share one tree
|
|
24
|
+
return parseTree(typeof source === 'string' ? source : '', optionsOf(options));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** the parse itself, kept by its arguments (the least recently used trees go first, past 8 MiB) */
|
|
28
|
+
const parseTree = memo((source: string, o: Options): Root => ({ type: 'root', children: blocks(tokenize(source), { o, ids: new Set() }) }), { maxSize: 8 * 1024 * 1024 });
|
|
29
|
+
|
|
30
|
+
interface Options {
|
|
31
|
+
base: string | null;
|
|
32
|
+
headings: 'keep' | 'strong';
|
|
33
|
+
images: 'keep' | 'link';
|
|
34
|
+
html: 'drop' | 'text' | 'allow';
|
|
35
|
+
}
|
|
36
|
+
interface Context {
|
|
37
|
+
o: Options;
|
|
38
|
+
/** heading ids so far */
|
|
39
|
+
ids: Set<string>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const ONE_OF = { headings: ['keep', 'strong'], images: ['keep', 'link'], html: ['drop', 'text', 'allow'] } as const;
|
|
43
|
+
function optionsOf(options: ParseOptions | undefined): Options {
|
|
44
|
+
const o = options ?? {};
|
|
45
|
+
for (const [k, allowed] of Object.entries(ONE_OF)) {
|
|
46
|
+
const v = (o as Record<string, unknown>)[k];
|
|
47
|
+
if (v !== undefined && !(allowed as readonly unknown[]).includes(v)) throw new TypeError(`md.parse: \`${k}\` is ${allowed.map((a) => `'${a}'`).join(', ')}, not ${JSON.stringify(v)}`);
|
|
48
|
+
}
|
|
49
|
+
let base: string | null = null;
|
|
50
|
+
if (o.base != null) {
|
|
51
|
+
if (typeof o.base !== 'string' || !o.base.startsWith('/') || o.base.startsWith('//')) throw new TypeError(`md.parse: \`base\` is a folder from the project root, such as '/content/posts/', not ${JSON.stringify(o.base)}`);
|
|
52
|
+
base = o.base.endsWith('/') ? o.base : o.base + '/';
|
|
53
|
+
}
|
|
54
|
+
return { base, headings: o.headings ?? 'keep', images: o.images ?? 'keep', html: o.html ?? 'text' };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** a relative URL under `base`; absolute URLs, fragments, queries and links to markdown files as written */
|
|
58
|
+
function url(value: string, cx: Context, link: boolean): string {
|
|
59
|
+
const base = cx.o.base;
|
|
60
|
+
if (!base || !value || /^[a-z][a-z\d+.-]*:/i.test(value) || /^[/#?]/.test(value)) return value;
|
|
61
|
+
if (link && /\.(md|markdown)(?:[?#]|$)/i.test(value)) return value;
|
|
62
|
+
const u = new URL(value, 'http://root' + base);
|
|
63
|
+
return u.pathname + u.search + u.hash;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Block content -------------------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Where block content goes: the document, a blockquote or a list item. `open`: the `<details>` opened in it under
|
|
70
|
+
* html: 'allow' and not closed yet (a details spans sibling blocks: an opening html block, blocks, a closing one);
|
|
71
|
+
* a list item's `list`, and `first` until its first block (a task item's marker is in its first paragraph)
|
|
72
|
+
*/
|
|
73
|
+
interface Box { out: Block[]; open: Array<{ node: Details; out: Block[]; raw: string }>; list?: List; item?: ListItem; first?: boolean }
|
|
74
|
+
|
|
75
|
+
/** a GFM task list item: its first paragraph starts with `[ ]`, `[x]` or `[X]` and whitespace */
|
|
76
|
+
const TASK = /^\[([ xX])\][ \t]+/;
|
|
77
|
+
|
|
78
|
+
function blocks(tokens: readonly Token[], cx: Context): Block[] {
|
|
79
|
+
const root: Box = { out: [], open: [] }, boxes = [root], lists: List[] = [];
|
|
80
|
+
let box = root, list: List | null = null, table: Table | null = null, row: TableRow | null = null;
|
|
81
|
+
const into = () => (box.open.length ? box.open[box.open.length - 1]!.node.children : box.out);
|
|
82
|
+
const add = (b: Block) => { into().push(b); box.first = false; };
|
|
83
|
+
const enter = (out: Block[], more?: Partial<Box>) => { boxes.push((box = { out, open: [], ...more })); };
|
|
84
|
+
// a `<details>` left open is shown as written, its content after it
|
|
85
|
+
const flush = () => { while (box.open.length) { const top = box.open.pop()!; top.out.push(rawBlock(top.raw), ...top.node.children); } };
|
|
86
|
+
const leave = () => { flush(); boxes.pop(); box = boxes[boxes.length - 1]!; };
|
|
87
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
88
|
+
const t = tokens[i]!;
|
|
89
|
+
switch (t.type) {
|
|
90
|
+
case 'paragraph_open': {
|
|
91
|
+
// markdown-it hides the paragraphs of a tight list's items: a visible one makes its list loose (as
|
|
92
|
+
// mdast-util-to-hast renders it, every item's paragraphs are then <p>)
|
|
93
|
+
if (box.list && !t.hidden) box.list.spread = true;
|
|
94
|
+
let skip = 0;
|
|
95
|
+
if (box.first && box.list) {
|
|
96
|
+
// the marker: in the text the paragraph starts with
|
|
97
|
+
const kids = tokens[i + 1]!.children ?? [];
|
|
98
|
+
let lead = '';
|
|
99
|
+
for (const k of kids) { const s = textOf(k); if (s == null) break; lead += s; }
|
|
100
|
+
const m = TASK.exec(lead);
|
|
101
|
+
if (m) { box.item!.checked = m[1] !== ' '; skip = m[0].length; }
|
|
102
|
+
}
|
|
103
|
+
const children = inlines(tokens[i + 1]!.children ?? [], cx, skip);
|
|
104
|
+
if (children.length) add({ type: 'paragraph', children }); else box.first = false;
|
|
105
|
+
i += 2;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case 'heading_open': {
|
|
109
|
+
const children = inlines(tokens[i + 1]!.children ?? [], cx);
|
|
110
|
+
add(cx.o.headings === 'strong' ? { type: 'paragraph', children: [{ type: 'strong', children }] }
|
|
111
|
+
: { type: 'heading', depth: +t.tag.slice(1) as Heading['depth'], slug: slug(text(children), cx.ids), children });
|
|
112
|
+
i += 2;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
case 'blockquote_open': { const node: Block = { type: 'blockquote', children: [] }; add(node); enter(node.children); break; }
|
|
116
|
+
case 'blockquote_close': case 'list_item_close': leave(); break;
|
|
117
|
+
case 'bullet_list_open': case 'ordered_list_open': {
|
|
118
|
+
const s = t.attrGet('start'), ordered = t.type === 'ordered_list_open';
|
|
119
|
+
const node: List = { type: 'list', ordered, start: ordered ? (s == null ? 1 : +s) : null, spread: false, children: [] };
|
|
120
|
+
add(node);
|
|
121
|
+
lists.push((list = node));
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case 'bullet_list_close': case 'ordered_list_close': lists.pop(); list = lists[lists.length - 1] ?? null; break;
|
|
125
|
+
case 'list_item_open': {
|
|
126
|
+
const item: ListItem = { type: 'listItem', checked: null, children: [] };
|
|
127
|
+
list!.children.push(item);
|
|
128
|
+
enter(item.children, { list: list!, item, first: true });
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case 'table_open': add((table = { type: 'table', align: [], children: [] })); break;
|
|
132
|
+
case 'tr_open': table!.children.push((row = { type: 'tableRow', children: [] })); break;
|
|
133
|
+
case 'th_open': case 'td_open': {
|
|
134
|
+
if (t.type === 'th_open') { const style = t.attrGet('style'); table!.align.push(style ? (/text-align:(\w+)/.exec(style)![1] as Table['align'][number]) : null); }
|
|
135
|
+
row!.children.push({ type: 'tableCell', children: inlines(tokens[i + 1]!.children ?? [], cx) });
|
|
136
|
+
i += 2;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case 'hr': add({ type: 'thematicBreak' }); break;
|
|
140
|
+
case 'code_block': add({ type: 'code', lang: null, meta: null, value: t.content.replace(/\n$/, '') }); break;
|
|
141
|
+
case 'fence': {
|
|
142
|
+
const info = t.info.trim(), sp = info.search(/\s/);
|
|
143
|
+
add({ type: 'code', lang: info ? (sp < 0 ? info : info.slice(0, sp)) : null, meta: sp < 0 ? null : info.slice(sp).trim() || null, value: t.content.replace(/\n$/, '') });
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case 'html_block': {
|
|
147
|
+
const value = t.content.replace(/\n+$/, '');
|
|
148
|
+
box.first = false;
|
|
149
|
+
if (cx.o.html === 'drop') break;
|
|
150
|
+
if (cx.o.html === 'allow') {
|
|
151
|
+
const d = DETAILS_OPEN.exec(value);
|
|
152
|
+
if (d) {
|
|
153
|
+
const node: Details = { type: 'details', open: d[1] != null, summary: d[2] != null ? summaryOf(d[2], cx) : [], children: [] };
|
|
154
|
+
box.open.push({ node, out: into(), raw: value });
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
const s = SUMMARY.exec(value), top = box.open[box.open.length - 1];
|
|
158
|
+
if (s && top && !top.node.summary.length && !top.node.children.length) { top.node.summary = summaryOf(s[1]!, cx); break; }
|
|
159
|
+
if (DETAILS_CLOSE.test(value) && top) { box.open.pop(); top.out.push(top.node); break; }
|
|
160
|
+
}
|
|
161
|
+
into().push(rawBlock(value));
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
// definitions are resolved into their references by the parser; anything else has no component
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
while (boxes.length > 1) leave();
|
|
168
|
+
flush();
|
|
169
|
+
return root.out;
|
|
170
|
+
}
|
|
171
|
+
const DETAILS_OPEN = /^<details(\s+open(?:\s*=\s*(?:""|''|"open"|'open'|open))?)?\s*>\s*(?:<summary>([\s\S]*?)<\/summary>)?\s*$/i;
|
|
172
|
+
const SUMMARY = /^<summary>([\s\S]*?)<\/summary>\s*$/i;
|
|
173
|
+
const DETAILS_CLOSE = /^<\/details\s*>\s*$/i;
|
|
174
|
+
|
|
175
|
+
/** raw HTML shown as written: a paragraph of its text */
|
|
176
|
+
const rawBlock = (value: string): Block => ({ type: 'paragraph', children: [{ type: 'text', value }] });
|
|
177
|
+
|
|
178
|
+
/** a summary's content, parsed as markdown and taken from its first paragraph */
|
|
179
|
+
function summaryOf(source: string, cx: Context): Inline[] {
|
|
180
|
+
const t = tokenize(source);
|
|
181
|
+
return t[0]?.type === 'paragraph_open' ? inlines(t[1]!.children ?? [], cx) : source.trim() ? [{ type: 'text', value: source.trim() }] : [];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Phrasing content ----------------------------------------------------------------------------------
|
|
185
|
+
|
|
186
|
+
const ELEMENTS = ['kbd', 'sub', 'sup', 'mark'] as const;
|
|
187
|
+
type Element = (typeof ELEMENTS)[number];
|
|
188
|
+
const OPEN = /^<(kbd|sub|sup|mark)(?:\s[^<>]*)?>$/i;
|
|
189
|
+
const CLOSE = /^<\/(kbd|sub|sup|mark)\s*>$/i;
|
|
190
|
+
|
|
191
|
+
/** what a text-like token adds to its text run: a text, an entity, a soft break; null for any other */
|
|
192
|
+
const textOf = (t: Token): string | null => (t.type === 'text' || t.type === 'text_special' ? t.content : t.type === 'softbreak' ? '\n' : null);
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Phrasing content from an `inline` token's tokens, `skip` characters of its leading text left out (a task
|
|
196
|
+
* item's marker). Adjacent text is one node, and empty text none: one text node per run in the page. Each
|
|
197
|
+
* element (emphasis, a link) holds frames: under html: 'allow' an opening tag starts one that its closing
|
|
198
|
+
* tag ends, and one left open is shown as written.
|
|
199
|
+
*/
|
|
200
|
+
function inlines(tokens: readonly Token[], cx: Context, skip = 0): Inline[] {
|
|
201
|
+
type Frame = { tag: Element | null; raw: string; out: Inline[] };
|
|
202
|
+
const out: Inline[] = [], stack: Frame[][] = [[{ tag: null, raw: '', out }]];
|
|
203
|
+
let frames = stack[0]!, cur = out;
|
|
204
|
+
const text = (v: string) => {
|
|
205
|
+
if (!v) return;
|
|
206
|
+
const last = cur[cur.length - 1];
|
|
207
|
+
if (last?.type === 'text') last.value += v; else cur.push({ type: 'text', value: v });
|
|
208
|
+
};
|
|
209
|
+
const put = (n: Inline) => (n.type === 'text' ? text(n.value) : cur.push(n));
|
|
210
|
+
const at = () => { cur = frames[frames.length - 1]!.out; };
|
|
211
|
+
// a frame closed by its tag's end, or left open: its tag as written and its content, into the frame before
|
|
212
|
+
const drop = () => { const f = frames.pop()!; at(); text(f.raw); for (const n of f.out) put(n); };
|
|
213
|
+
const open = (n: Inline & { children: Inline[] }) => { cur.push(n); stack.push((frames = [{ tag: null, raw: '', out: n.children }])); cur = n.children; };
|
|
214
|
+
const close = () => { while (frames.length > 1) drop(); stack.pop(); frames = stack[stack.length - 1]!; at(); };
|
|
215
|
+
for (const t of tokens) {
|
|
216
|
+
const s = textOf(t);
|
|
217
|
+
if (s != null) {
|
|
218
|
+
if (skip) { if (s.length <= skip) { skip -= s.length; continue; } text(s.slice(skip)); skip = 0; } else text(s);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
skip = 0;
|
|
222
|
+
switch (t.type) {
|
|
223
|
+
case 'hardbreak': cur.push({ type: 'break' }); break;
|
|
224
|
+
case 'em_open': open({ type: 'emphasis', children: [] }); break;
|
|
225
|
+
case 'strong_open': open({ type: 'strong', children: [] }); break;
|
|
226
|
+
case 's_open': open({ type: 'delete', children: [] }); break;
|
|
227
|
+
case 'link_open': open({ type: 'link', url: url(t.attrGet('href') ?? '', cx, true), title: t.attrGet('title') ?? null, children: [] }); break;
|
|
228
|
+
case 'em_close': case 'strong_close': case 's_close': case 'link_close': close(); break;
|
|
229
|
+
case 'code_inline': cur.push({ type: 'inlineCode', value: t.content }); break;
|
|
230
|
+
case 'image': {
|
|
231
|
+
const src = url(t.attrGet('src') ?? '', cx, false), title = t.attrGet('title') ?? null, alt = plain(t.children);
|
|
232
|
+
cur.push(cx.o.images === 'link' ? { type: 'link', url: src, title, children: [{ type: 'text', value: alt || src }] } : { type: 'image', url: src, title, alt });
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
case 'html_inline': {
|
|
236
|
+
const value = t.content;
|
|
237
|
+
if (cx.o.html === 'drop') break;
|
|
238
|
+
if (cx.o.html === 'allow') {
|
|
239
|
+
const o = OPEN.exec(value);
|
|
240
|
+
if (o) { frames.push({ tag: o[1]!.toLowerCase() as Element, raw: value, out: [] }); at(); break; }
|
|
241
|
+
const c = CLOSE.exec(value);
|
|
242
|
+
const k = c ? frames.findLastIndex((f) => f.tag === c[1]!.toLowerCase()) : -1;
|
|
243
|
+
if (k > 0) {
|
|
244
|
+
// frames opened inside it and never closed are shown as written
|
|
245
|
+
while (frames.length - 1 > k) drop();
|
|
246
|
+
const f = frames.pop()!;
|
|
247
|
+
at();
|
|
248
|
+
cur.push({ type: f.tag!, children: f.out });
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
text(value);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
while (frames.length > 1) drop();
|
|
258
|
+
return out;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** an image's alt: the text of its description, without markup */
|
|
262
|
+
function plain(tokens: readonly Token[] | null): string {
|
|
263
|
+
let s = '';
|
|
264
|
+
for (const t of tokens ?? []) {
|
|
265
|
+
if (t.type === 'text' || t.type === 'text_special' || t.type === 'code_inline') s += t.content;
|
|
266
|
+
else if (t.type === 'softbreak') s += '\n';
|
|
267
|
+
else if (t.type === 'image') s += plain(t.children);
|
|
268
|
+
}
|
|
269
|
+
return s;
|
|
270
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The prebundled parser (sinter/parser.js, built by scripts/build-parser.mjs).
|
|
2
|
+
|
|
3
|
+
/** a markdown-it token, for the parts md.parse reads */
|
|
4
|
+
export interface Token {
|
|
5
|
+
type: string;
|
|
6
|
+
/** the element: `h2` for a level-2 heading's tokens */
|
|
7
|
+
tag: string;
|
|
8
|
+
content: string;
|
|
9
|
+
/** a fence's info string, unescaped */
|
|
10
|
+
info: string;
|
|
11
|
+
/** a paragraph of a tight list's item */
|
|
12
|
+
hidden: boolean;
|
|
13
|
+
/** an `inline` token's tokens; an image's description */
|
|
14
|
+
children: Token[] | null;
|
|
15
|
+
attrGet(name: string): string | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The block tokens of `source` (CommonMark, and GFM tables, strikethrough and autolinks), as markdown-it gives them. */
|
|
19
|
+
export function tokenize(source: string): Token[];
|