-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
77 lines (69 loc) · 1.73 KB
/
node.js
File metadata and controls
77 lines (69 loc) · 1.73 KB
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
/**
* @file: node
* @author: Cuttle Cong
* @date: 2018/2/13
* @description:
*/
function walk(node, cond, process, parent) {
parent = parent || null
if (!Array.isArray(node)) {
node = [node]
}
node.forEach(function (node, i, all) {
if (cond(node, i, parent)) {
process(node, i, parent)
}
node.children && walk(node.children, cond, process, node)
})
}
exports.remarkTransformer = function (options) {
return function (node) {
walk(node, function (node) {
return node.type === 'text' && node.value.indexOf('==') !== -1
}, function (node, index, parent) {
var value = node.value
var matched = false
var firstM = false, endM = false
var start = -1, values = value.split('')
for (var i = 0; i < value.length; i++) {
var char = value[i]
var nextChar = value[i + 1]
if (!firstM) {
if (char === '=' && nextChar === '=') {
start = i + 2
firstM = true
i++
}
}
else {
if (char === '=' && nextChar === '=') {
// content is empty
if (start === i) {
start = -1
firstM = false
i++
}
else {
firstM = false
values[start - 1] = '<mark>'
values[start - 2] = ''
values[i] = '</mark>'
values[i + 1] = ''
matched = true
start = -1
i++
}
}
else if (char === '\\' && nextChar === '=') {
values[i] = ''
i++
}
}
}
if (matched) {
node.type = 'html'
node.value = values.join('')
}
})
}
}