-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (99 loc) · 3.56 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const startRegex = /{{%\s+(notice).+%}}/;
const endRegex = /{{%\s+(\/notice).+%}}/;
const classMap = {
"note": "notice-note",
"warn": "notice-warn",
"info": "notice-info",
"error": "notice-error"
}
// TODO expend collapse support
module.exports = shortcodes;
function shortcodes(options) {
// const startBlock = (options || {}).startBlock || "[[";
// const endBlock = (options || {}).endBlock || "]]";
// const inlineMode = (options || {}).inlineMode || false;
return (node) => {
// console.log(node)
const nodeCopy = node
nodeCopy.children.forEach((item, index) => {
const children = item.children
if (item.type === "paragraph") {
children.forEach((child, childIndex) => {
if (startCodeBlock(child.value)) {
if (isShortcodeBlock(child.value)) {
// block without html inside
const block = child.value.split("\n")
const start = block[0]
const end = block[block.length - 1]
const startHtml = '<div class="' +
classMap[start.replace("{{%", "")
.replace("%}}", "").replace("notice", "").trim()] +
'">' + '<p class="shortcode-block">'
const endHtml = '</p></div>'
const result = {
type: 'html',
value: child.value.replace(start, startHtml).replace(end, endHtml)
}
node.children = [
...node.children.slice(0, index),
result,
...node.children.slice(index + 1)
]
} else {
// block contains html for markdown
const startIndex = childIndex
// console.log("start index: "+ startIndex)
let endIndex
for (let i=startIndex;i < nodeCopy.children[index].children.length;i++) {
if (endCodeBlock(nodeCopy.children[index].children[i].value)) {
endIndex = i
break
}
}
const oldStart = nodeCopy.children[index].children[startIndex].value
const mapTag = parseTag(oldStart)
const startHtml = '<div class="' +
classMap[mapTag.tag]
+ '">' + '<p class="shortcode-block">' + mapTag.text
// console.log(startHtml)
const endTag = nodeCopy.children[index].children[endIndex].value.replace("{{% /notice %}}","")
const endHtml = endTag + '</p></div>'
const start = {
type: 'html',
value: startHtml
}
const end = {
type: 'html',
value: endHtml
}
node.children[index].children = [
...node.children[index].children.slice(0, startIndex),
start,
...node.children[index].children.slice(startIndex+1,endIndex),
end,
...node.children[index].children.slice(endIndex+1)
]
}
}
})
}
})
}
function startCodeBlock(value) {
return startRegex.test(value)
}
function endCodeBlock(value) {
return endRegex.test(value)
}
// to test if is shortcode block
function isShortcodeBlock(value) {
return startRegex.test(value) && endRegex.test(value);
}
function parseTag(value) {
let va = value.split("%}}")[0]
return {
tag: va.replace("\n", "").replace("{{%", "").replace("notice", "").trim(),
text: value.split("%}}")[1]
}
}
}