-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (80 loc) · 2.63 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* critic markup HTML LaTeX
{--[text]--} <del>[text]</del> \st{[text]}
{++[text]++} <ins>[text]</ins> \underline{[text]}
{~~[text1]~>[text2]~~} <del>[text1]</del><ins>[text2]</ins> \st{[text1]}\underline{[text2]}
{==[text]==} <mark>[text]</mark> \hl{[text]}
{>>[text]<<} <aside>[text]</aside> \marginpar{[text]}
*/
/**
* CriticMarkup rule
*/
module.exports = function critcmarkup_plugin(md) {
md.inline.ruler.before('strikethrough', 'critic-markup', (state, silent)=> {
const src = state.src,
pos = state.pos
if (src[pos] === '{' && (
(src[pos + 1] === '-' && src[pos + 2] === '-') ||
(src[pos + 1] === '+' && src[pos + 2] === '+') ||
(src[pos + 1] === '~' && src[pos + 2] === '~') ||
(src[pos + 1] === '=' && src[pos + 2] === '=') ||
(src[pos + 1] === '>' && src[pos + 2] === '>')
)) {
let tag = src.slice(pos+1, pos+3)
let closeTag = tag
if (closeTag[0] === '>')
closeTag = '<<}'
else
closeTag = closeTag + '}'
let i = pos + 3,
end = -1,
content = null
while(i < src.length) {
if (src.startsWith(closeTag, i)) {
end = i
break
}
i += 1
}
if (end >= 0)
content = src.slice(pos + 3, end)
else
return false
if (content && !silent) {
const token = state.push('critic-markup')
token.content = content
token.tag = tag
state.pos = end + closeTag.length
return true
} else {
return false
}
} else {
return false
}
})
/**
* CriticMarkup renderer
*/
md.renderer.rules['critic-markup'] = (tokens, idx)=> {
const token = tokens[idx],
tag = token['tag'],
content = token['content']
if (tag === '--') {
return `<del>${content}</del>`
} else if (tag === '++') {
return `<ins>${content}</ins>`
} else if (tag === '==') {
return `<mark>${content}</mark>`
} else if (tag === '>>') {
return `<span style="display:none">${content}</span>`
} else { // {~~[text1]~>[text2]~~}
const arr = content.split('~>')
if (arr.length === 2) {
return `<del>${arr[0]}</del><ins>${arr[1]}</ins>`
} else {
return `<code>Error: ~> not found.</code>`
}
}
}
};