forked from zWingz/remark-container
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (60 loc) · 1.62 KB
/
index.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
const defaultOptions = {
className: 'remark-container',
}
function plugin(opt = {}) {
const { className } = Object.assign(defaultOptions, opt)
const parser = this.Parser
const { blockTokenizers, blockMethods, interruptParagraph } = parser.prototype
const paragraph = 'paragraph'
function tokenizer(eat, value, silent) {
const reg = /^\s*:::\s*(\w+)(.*?)[\n\r]([\s\S]+?)\s*:::\s*?/
const match = value.match(reg)
if (!match) {
/* eslint-disable-next-line */
return false
}
/* istanbul ignore if - never used (yet) */
if (silent) {
return true
}
// const [input, type, title, content] = match
const input = match[0]
const type = match[1]
const title = match[2]
const content = match[3]
const start = eat.now()
const add = eat(input)
const end = eat.now()
let children = [
{
type: paragraph,
children: [
{
type: 'text',
value: (title || type).trim(),
},
],
data: { hProperties: { className: [`${className}-title`] } },
},
]
children = children.concat(this.tokenizeBlock(content.trim(), {}))
return add({
type: 'container',
children,
data: {
hName: 'div',
hProperties: { className: [className, type.toLowerCase()] },
},
position: {
start,
end,
},
})
}
tokenizer.notInList = true
tokenizer.notInLink = true
blockTokenizers.container = tokenizer
blockMethods.splice(blockMethods.indexOf('newline') + 1, 0, 'container')
interruptParagraph.unshift(['container'])
}
module.exports = plugin