-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmarkdown-loader.js
47 lines (39 loc) · 1.23 KB
/
markdown-loader.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
const md = require('markdown-it')
const { html5Media } = require('markdown-it-html5-media')
const fm = require('front-matter')
const loaderUtils = require('loader-utils')
module.exports = function (source) {
if (this.cacheable) {
this.cacheable()
}
let options
try {
options = loaderUtils.parseQuery(this.query)
} catch (e) {
options = {}
}
const frontMatterContext = fm(source)
const obj = frontMatterContext.attributes
const parser = md({
html: true
}).use(html5Media, {
videoAttrs:
'autoPlay=true muted=true loop=true playsInline=true preload="auto"'
})
const defaultRender =
parser.renderer.rules.link_open ||
((tokens, idx, options, env, self) =>
self.renderToken(tokens, idx, options))
parser.renderer.rules.link_open = function (tokens, idx, options, env, self) {
tokens[idx].attrPush(['native', ''])
tokens[idx].attrPush(['rel', 'nofollow'])
return defaultRender(tokens, idx, options, env, self)
}
obj.body = parser.render(frontMatterContext.body, options)
const matcher = /<img[^>]+src="([^">]+)"/
const match = obj.body.match(matcher)
if (match && match.length >= 2) {
obj.image = match[1]
}
return 'module.exports = ' + JSON.stringify(obj)
}