forked from pixelandtonic/vuepress-theme-craftdocs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
markup.js
257 lines (215 loc) · 7.61 KB
/
markup.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const Token = require('markdown-it/lib/token');
const container = require('markdown-it-container')
function findPrev(tokens, idx, check) {
for (let i = idx - 1; i >= 0; i--) {
if (check(tokens[i])) {
return i;
}
}
return false;
}
function findNext(tokens, idx, check) {
for (let i = idx + 1; i < tokens.length; i++) {
if (check(tokens[i])) {
return i;
}
}
return false;
}
function increment(tokens) {
tokens.forEach(t => t.level++)
}
function vPres(tokens) {
for (let i = 0; i < tokens.length; i++) {
if (!hasInlineCode(tokens[i])) {
continue;
}
let openIdx = findPrev(tokens, i, t => t.level === 0)
let closeIdx = findNext(tokens, i, t => t.level === 0)
if (
openIdx === false ||
closeIdx === false ||
['paragraph_open', 'blockquote_open', 'bullet_list_open', 'ordered_list_open', 'table_open'].indexOf(tokens[openIdx].type) === -1 ||
['paragraph_close', 'blockquote_close', 'bullet_list_close', 'ordered_list_close', 'table_close'].indexOf(tokens[closeIdx].type) === -1
) {
continue;
}
let vPreOpen = new Token('container_v-pre_open', 'div', 1);
vPreOpen.markup = ':::';
vPreOpen.info = 'v-pre';
let vPreClose = new Token('container_v-pre_close', 'div', -1);
vPreClose.markup = ':::';
let innerTokens = tokens.slice(openIdx, closeIdx + 1);
increment(innerTokens)
let replaceTokens = [
vPreOpen,
...innerTokens,
vPreClose
]
tokens.splice(openIdx, closeIdx - openIdx + 1, ...replaceTokens)
// skip ahead
i = openIdx + replaceTokens.length - 1
}
}
function hasInlineCode(token) {
if (token.type === 'inline') {
for (let i = 0; i < token.children.length; i++) {
if (token.children[i].type === 'code_inline') {
return true;
}
}
}
return false;
}
function tables(tokens) {
for (let i = 0; i < tokens.length; i++) {
let t = tokens[i]
if (t.type === 'table_open' && (i === 0 || tokens[i - 1].content !== `<div class="table">\n`)) {
for (let j = i + 1; j < tokens.length; j++) {
let t2 = tokens[j];
if (t2.type === 'table_close') {
let replaceTokens = [
openBlock('table', t.level),
...tokens.slice(i, j + 1),
closeBlock(t.level)
];
tokens.splice(i, (j - i), ...replaceTokens);
// skip ahead
i += replaceTokens.length - 1;
break;
}
}
}
}
}
function split(tokens) {
for (let i = 0; i < tokens.length; i++) {
let t = tokens[i];
if (t.type === 'hr') {
let leftContentStart = 0,
rightContentEnd = tokens.length - 1;
// see if there's a previous h2/h3
for (let j = i - 1; j >= 0; j--) {
if (isHeading(tokens[j], 'heading_close')) {
leftContentStart = j + 1;
break;
}
}
// see if there's another h2/h3 afterwards
for (let j = i + 1; j < rightContentEnd; j++) {
if (isHeading(tokens[j], 'heading_open')) {
rightContentEnd = j - 1;
break;
}
}
let leftTokens = tokens.slice(leftContentStart, i);
let rightTokens = tokens.slice(i + 1, rightContentEnd + 1);
let replaceTokens = [
openBlock('split'),
openBlock('left'),
...leftTokens,
closeBlock(),
openBlock('right'),
...rightTokens,
closeBlock(),
closeBlock()
];
tokens.splice(leftContentStart, (rightContentEnd - leftContentStart) + 1, ...replaceTokens);
// skip ahead
i = leftContentStart + replaceTokens.length;
}
}
}
function codeToggles(tokens) {
for (let i = 0; i < tokens.length; i++) {
let t = tokens[i]
if (t.type === 'container_code_open') {
// find the close tag
for (let j = i + 1; j < tokens.length; j++) {
if (tokens[j].type === 'container_code_close') {
let innerTokens = tokens.slice(i + 1, j)
let slotNames = [];
let labels = {};
codeBlocks(innerTokens, (t, i) => {
let slotName;
// does the slot have a custom label?
let labelMatch = t.info.match(/([^ ]) +(.*)/);
if (labelMatch) {
// give the slot a random slot name
slotName = 'slot' + i;
labels[slotName] = labelMatch[2];
// remove the label from the code info
t.info = t.info.replace(labelMatch[0], labelMatch[1]);
} else {
// set the slot name to the language (w/out line numbers)
slotName = t.info.replace(/\{.*\}/, '').trim();
}
slotNames.push(slotName);
return [
block(`<template slot="${slotName}">`, t.level),
t,
block('</template>', t.level)
]
});
let openBlock = block(`<code-toggle :languages='${JSON.stringify(slotNames)}' :labels='${JSON.stringify(labels)}'>`, tokens[i].level);
let closeBlock = block('</code-toggle>', tokens[j].level);
openBlock.nesting = tokens[i].nesting;
closeBlock.nesting = tokens[j].nesting;
let replaceTokens = [
openBlock,
...innerTokens,
closeBlock
]
tokens.splice(i, j - i, ...replaceTokens);
// skip ahead
i += (replaceTokens.length - 1)
break;
}
}
}
}
}
function codeBlocks(tokens, replace) {
for (let i = 0; i < tokens.length; i++) {
let t = tokens[i]
if (t.type === 'fence' && t.info) {
let replaceTokens = replace(t, i)
tokens.splice(i, 1, ...replaceTokens)
// skip ahead
i += (replaceTokens.length - 1)
}
}
}
function isHeading(t, type) {
return t.type === type && (t.tag === 'h1' || t.tag === 'h2' || t.tag === 'h3' || t.tag === 'h4');
}
function block(tag, level) {
var t = new Token('html_block', '', 0);
t.content = `${tag}\n`;
t.block = true;
t.level = level || 0;
return t;
}
function openBlock(klass, level) {
return block(`<div class="${klass}">`, level);
}
function closeBlock(level) {
return block('</div>', level);
}
module.exports = (md) => {
// override parse()
const parse = md.parse
md.parse = (...args) => {
const tokens = parse.call(md, ...args)
vPres(tokens);
tables(tokens);
codeToggles(tokens);
split(tokens);
return tokens;
}
md.use(container, 'code', {
render(tokens, idx) {
return '';
}
})
}