-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown.js
255 lines (220 loc) · 7.8 KB
/
markdown.js
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
// base
import { unified } from 'unified'
import parse from 'remark-parse'
import remark2rehype from 'remark-rehype'
import format from 'rehype-format'
import html from 'rehype-stringify'
import { h } from 'hastscript'
import path from 'path'
import { visit } from 'unist-util-visit'
import { toHtml } from 'hast-util-to-html'
import { toHast } from 'mdast-util-to-hast'
// markdown plugins
import directive from 'remark-directive'
import frontmatter from 'remark-frontmatter'
import parseFrontmatter from 'remark-parse-yaml'
import supersub from 'remark-supersub'
import { toc } from 'mdast-util-toc'
import remarkTextr from 'remark-textr'
import lint from './markdown-lint.js'
// html plugins
import slug from 'rehype-slug'
import urls from 'rehype-urls'
import autolink from 'rehype-autolink-headings'
// textr plugins
// does basic typography substitution, " -> “ for example.
import typographicBase from 'typographic-base'
// Remove the typographicCurrency plugin because it does weird stuff like yer=>﷼
var currencyIndex = typographicBase.mws.findIndex(func => func.name === 'typographicCurrency')
if (currencyIndex > 0) {
typographicBase.mws.splice(currencyIndex, 1)
}
const markdown =
unified()
.use(parse)
// markdown plugins
.use(supersub)
.use(directive)
.use(defaultDirective)
.use(writeDirectives)
.use(frontmatter, 'yaml')
.use(parseFrontmatter)
.use(copyFrontmatter)
.use(tableOfContents)
.use(remarkTextr, { locale: 'en-us', plugins: [typographicBase] })
.use(lint)
// html plugins
.use(remark2rehype)
.use(slug)
.use(autolink)
.use(urls, fixupLinks)
// post process
.use(format)
.use(html)
function fixupLinks(url) {
if (url.pathname && path.extname(url.pathname) === '.md') {
// if the link is internal to an .md file, change it to an .html file
return url.pathname.replace('.md', '.html')
}
return url.href
}
function copyFrontmatter() {
return function (ast, file) {
visit(ast, 'yaml', item => {
// copy parsed frontmatter to the file data
Object.assign(file.data, { metadata: item.data.parsedValue })
})
}
}
// a basic transform for directive nodes. It turns a directive name into an html tag, and adds any attributes that are declared.
function defaultDirective() {
return transform
function transform(tree) {
visit(tree, ['textDirective', 'leafDirective', 'containerDirective'], ondirective)
}
function ondirective(node) {
let data = node.data || (node.data = {})
let hast = h(node.name, node.attributes)
data.hName = hast.tagName
data.hProperties = hast.properties
}
}
function writeDirectives() {
return transform
function transform(tree) {
visit(tree, ['textDirective', 'leafDirective', 'containerDirective'], ondirective)
}
function ondirective(node) {
let data = node.data || (node.data = {})
switch (node.name) {
case 'sidebar':
sidebarDirective(node, data)
break
case 'callout':
calloutDirective(node, data)
break
case 'columns':
columnsDirective(node, data)
break
case 'fate':
fateDirective(node, data)
break
case 'aspect':
aspectDirective(node, data)
break
case 'quote':
quoteDirective(node, data)
break
case 'figure-table':
figureTableDirective(node, data)
break
default:
break;
}
function sidebarDirective(node, data) {
let hast = h('aside', node.attributes)
data.hName = hast.tagName
data.hProperties = hast.properties
}
function calloutDirective(node, data) {
let hast = h('article', node.attributes)
data.hName = hast.tagName
data.hProperties = hast.properties
}
function columnsDirective(node, data) {
let hast = h('div', { class: 'columns' })
data.hName = hast.tagName
data.hProperties = hast.properties
}
function fateDirective(node, data) {
let hast = h('span', { class: 'fate-icon' })
data.hName = hast.tagName
data.hProperties = hast.properties
}
function aspectDirective(node, data) {
let hast = h('span', { class: 'aspect' })
data.hName = hast.tagName
data.hProperties = hast.properties
}
function quoteDirective(node, data) {
let aside = h('aside', node.attributes || { class: '' })
data.hName = aside.tagName
// add a classname of 'quoted' to the start
aside.properties.className.unshift('quoted')
data.hProperties = aside.properties
// find the attribution, it'll be a child with directiveLabel = true
// move it to the end and tell rehype to make it a footer
let attribution = node.children.find(el => el.data.directiveLabel === true)
if (attribution) {
let index = node.children.indexOf(attribution)
node.children.push(node.children.splice(index, 1)[0])
attribution.data.hName = 'footer'
}
}
function figureTableDirective(node, data) {
let aside = h('aside', node.attributes || { class: '' })
data.hName = aside.tagName
// add a classname of 'quoted' to the start
aside.properties.className.unshift('figure-table')
data.hProperties = aside.properties
// find the caption, it'll be a child with directiveLabel = true
// move it to the end and tell rehype to make it a ficaption
let caption = node.children.find(el => el.data.directiveLabel === true)
if (caption) {
let index = node.children.indexOf(caption)
node.children.push(node.children.splice(index, 1)[0])
caption.data.hName = 'figcaption'
}
}
}
}
function tableOfContents() {
return transform
function transform(tree, file) {
let table = toc(tree)
let list = []
// skip files without headers
if (table.map != null) {
// we'll have to reach into children : text nodes to find the values to use for the title of the link
// we care about type='listItem' nodes
// type='list'
// type='listItem'
// type='paragraph'
// type='link' url='#anchor'
// type='text' value='title'
// type='list'
// type='listItem'
// type='listItem'
// type='listItem'
// type='listItem' // these are the list items we need to work on.
let recurse = function (node, curr, list) {
if (node.type === 'list') {
// this is when we know there will be children for current node
// which means the current node's child list becomes the new context
// The top level is a special case
if (curr != null) {
list = curr.children
}
}
if (node.type === 'listItem') {
// Start a new child
curr = { url: '', title: '', children: [] }
}
if (node.type === 'link') {
curr.url = node.url
let hast = toHast(node)
curr.title = toHtml(hast.children)
list.push(curr)
}
// mdast nodes may not have a children property
for (const child of node?.children ?? []) {
recurse(child, curr, list)
}
}
recurse(table.map, null, list)
}
// console.log(JSON.stringify(list, null, ' '))
Object.assign(file.data, { toc: list })
}
}
export default markdown