/
MarkdownAsset.js
70 lines (62 loc) · 1.73 KB
/
MarkdownAsset.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
const { Asset } = require('parcel-bundler')
const MarkdownIt = require('markdown-it')
const Meta = require('markdown-it-meta')
const serializeObject = require('parcel-bundler/src/utils/serializeObject')
class MarkdownAsset extends Asset {
constructor (name, pkg, options) {
super(name, pkg, options)
this.type = 'js'
var md = new MarkdownIt('default', {
html: true,
linkify: true,
typographer: true
}).use(Meta)
// try loading 'markdown-it-highlightjs' if available
try {
md = md.use(require('markdown-it-highlightjs'), {})
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e
}
}
this.md = md
}
collectDependencies () {
this.collectImgs(this.ast.parsed)
}
collectImgs (tokens) {
for (const token of tokens) {
if (token.type === 'inline' && token.children !== null && token.children.length > 0) {
this.collectImgs(token.children)
} else if (token.type === 'image') {
const attrs = token.attrs.map((attr) => {
const [name, value] = attr
if (name === 'src' && value.startsWith('.')) {
const newSrc = this.addURLDependency(value)
return [name, newSrc]
}
return attr
})
token.attrs = attrs
}
}
}
async parse (markdownString) {
const env = {}
const parsed = this.md.parse(markdownString, env)
return {
meta: this.md.meta,
parsed,
env
}
}
generate () {
const html = this.md.renderer.render(this.ast.parsed, this.md.options, this.ast.env)
this.ast.html = html
return serializeObject(
this.ast,
this.options.minify && !this.options.scopeHoist
)
}
}
module.exports = MarkdownAsset