-
Notifications
You must be signed in to change notification settings - Fork 85
/
markdoc.tsx
170 lines (160 loc) · 3.35 KB
/
markdoc.tsx
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
import {
type RenderableTreeNode,
parse as markdocParse,
transform as markdocTransform,
Tag,
} from '@markdoc/markdoc';
import { type Menu } from '~/components';
function generateID(
children: RenderableTreeNode[],
attributes: Record<string, any>,
) {
if (attributes.id && typeof attributes.id === 'string') {
return attributes.id;
}
return children
.flatMap((child) => {
if (child instanceof Tag) {
return child.children;
}
return [child];
})
.filter((child) => typeof child === 'string')
.join(' ')
.replace(/[?]/g, '')
.replace(/\s+/g, '-')
.toLowerCase();
}
export function parse(markdown: string) {
const content = markdown
.replace(
/<details>\n?\s*<summary>(.+?)<\/summary>(.*?)<\/details>/gs,
'{% details summary="$1" %}$2{% /details %}',
)
.replace(/<!-- (\/?(aside|sandbox)( \w+=".+")*) -->/g, '{% $1 %}');
const ast = markdocParse(content);
const node = markdocTransform(ast, {
nodes: {
fence: {
render: 'Fence',
attributes: {
language: { type: String },
},
},
heading: {
attributes: {
id: { type: String, required: false },
level: { type: Number },
},
transform(node, config) {
const attributes = node.transformAttributes(config);
const children = node.transformChildren(config);
return new Tag(
`Heading`,
{
...attributes,
id:
attributes.level === 2
? generateID(children, attributes)
: undefined,
},
children,
);
},
},
link: {
render: 'Link',
attributes: {
href: { type: String },
title: { type: String },
},
},
paragraph: {
render: 'Paragraph',
},
list: {
render: 'List',
attributes: {
ordered: { type: Boolean },
},
},
item: {
render: 'Item',
},
code: {
transform(node) {
const { content, ...attributes } = node.attributes;
return new Tag('Code', attributes, [content]);
},
},
strong: {
render: 'Strong',
},
},
tags: {
aside: {
render: 'Aside',
description: 'Side notes',
},
sandbox: {
render: 'Sandbox',
description: 'To display an embedded sandbox',
attributes: {
title: {
type: String,
description: 'Title of the sandbox',
},
src: {
type: String,
required: true,
description: 'Path to the source of the sandbox',
},
},
},
details: {
render: 'Details',
description: 'Native Details tag',
attributes: {
summary: {
type: String,
description: 'Summary of the block',
},
},
},
},
});
return node;
}
export function collectHeadings(
node: RenderableTreeNode,
level = 1,
menu: Menu = { title: '', links: [] },
): Menu {
if (node instanceof Tag) {
if (node.name === 'Heading') {
const title = Array.isArray(node.children) ? node.children[0] : null;
if (typeof title === 'string') {
switch (node.attributes.level) {
case 1:
if (menu.title) {
throw new Error('There are multiple h1 headings in the document');
}
menu.title = title;
break;
case 2:
menu.links.push({
title,
to: `#${node.attributes.id ?? ''}`,
});
break;
}
}
}
if (level > 0 && node.children) {
for (const child of node.children) {
collectHeadings(child, level - 1, menu);
}
}
}
return menu;
}