-
Notifications
You must be signed in to change notification settings - Fork 0
/
remark-plugin.js
48 lines (41 loc) · 1.01 KB
/
remark-plugin.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
/**
* @file mdx-md-plugin
* @author imcuttle <moyuyc95@gmail.com>
* @date 2018/10/31
*
*/
const createFactory = require('./loader/createFactory')
const { detectMarkdown } = require('.')
function create() {
let runtimeKey
let runtimeOpts
const loader = createFactory({
keyGetter() {
return runtimeKey
},
optsGetter() {
return runtimeOpts
}
})((oldV, newV, opts = {}) => {
return detectMarkdown(oldV, newV, opts)
})
function transform(input, { key, opts }) {
runtimeKey = key
runtimeOpts = opts
return loader(input)
}
function plugin(options = {}) {
const self = this
return function(node, file = {}) {
const opts = options
const key = options.filepath || file.history[0]
return transform(node, {
key,
opts: Object.assign({ text: false, position: false, wrapType: 'ast' }, opts, { ast: true })
}).ast
}
}
Object.assign(plugin, loader)
return plugin
}
module.exports = Object.assign(create(), { create })