-
-
Notifications
You must be signed in to change notification settings - Fork 673
/
codeTabs.ts
294 lines (232 loc) · 8.36 KB
/
codeTabs.ts
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import type { PluginSimple } from "markdown-it";
import type { RuleBlock } from "markdown-it/lib/parser_block";
export const codeTabs: PluginSimple = (md) => {
const CODETAB_MARKER = `@tab`;
const codeTabsRule: RuleBlock = (state, startLine, endLine, silent) => {
let start = state.bMarks[startLine] + state.tShift[startLine];
let max = state.eMarks[startLine];
// Check out the first character quickly,
// this should filter out most of non-containers
if (state.src[start] !== ":") return false;
let pos = start + 1;
// Check out the rest of the marker string
while (pos <= max) {
if (state.src[pos] !== ":") break;
pos += 1;
}
const markerCount = pos - start;
if (markerCount < 3) return false;
const markup = state.src.slice(start, pos);
const params = state.src.slice(pos, max);
const [name, id = ""] = params.split("#", 2);
if (name.trim() !== "code-tabs") return false;
// Since start is found, we can report success here in validation mode
if (silent) return true;
// Search for the end of the block
let nextLine = startLine;
let autoClosed = false;
// Search for the end of the block
while (
// unclosed block should be autoclosed by end of document.
// also block seems to be autoclosed by end of parent
nextLine < endLine
) {
nextLine += 1;
start = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (start < max && state.sCount[nextLine] < state.blkIndent)
// non-empty line with negative indent should stop the list:
// - ```
// test
break;
if (
// match start
state.src[start] === ":" &&
// closing fence should be indented less than 4 spaces
state.sCount[nextLine] - state.blkIndent < 4
) {
// check rest of marker
for (pos = start + 1; pos <= max; pos++)
if (state.src[pos] !== ":") break;
// closing code fence must be at least as long as the opening one
if (pos - start >= markerCount) {
// make sure tail has spaces only
pos = state.skipSpaces(pos);
if (pos >= max) {
// found!
autoClosed = true;
break;
}
}
}
}
const oldParent = state.parentType;
const oldLineMax = state.lineMax;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
state.parentType = "code_tabs";
// this will prevent lazy continuations from ever going past our end marker
state.lineMax = nextLine - (autoClosed ? 1 : 0);
const openToken = state.push(`code_tabs_open`, "CodeGroup", 1);
openToken.markup = markup;
openToken.block = true;
openToken.info = name;
openToken.meta = { id: id.trim() };
openToken.map = [startLine, nextLine - (autoClosed ? 1 : 0)];
state.md.block.tokenize(
state,
startLine + 1,
nextLine - (autoClosed ? 1 : 0)
);
const closeToken = state.push(`code_tabs_close`, "CodeGroup", -1);
closeToken.markup = state.src.slice(start, pos);
closeToken.block = true;
state.parentType = oldParent;
state.lineMax = oldLineMax;
state.line = nextLine + (autoClosed ? 1 : 0);
return true;
};
const codeTabRule: RuleBlock = (state, startLine, endLine, silent) => {
let start = state.bMarks[startLine] + state.tShift[startLine];
let max = state.eMarks[startLine];
/*
* Check out the first character quickly,
* this should filter out most of non-uml blocks
*/
if (state.src.charAt(start) !== "@") return false;
let index;
// Check out the rest of the marker string
for (index = 0; index < CODETAB_MARKER.length; index++)
if (CODETAB_MARKER[index] !== state.src[start + index]) return false;
const markup = state.src.slice(start, start + index);
const info = state.src.slice(start + index, max);
// Since start is found, we can report success here in validation mode
if (silent) return true;
let nextLine = startLine;
let autoClosed = false;
// Search for the end of the block
while (
// unclosed block should be autoclosed by end of document.
// also block seems to be autoclosed by end of parent
nextLine < endLine
) {
nextLine += 1;
start = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (start < max && state.sCount[nextLine] < state.blkIndent)
// non-empty line with negative indent should stop the list:
// - ```
// test
break;
if (
// match start
state.src[start] === "@" &&
// marker should not be indented with respect of opening fence
state.sCount[nextLine] <= state.sCount[startLine]
) {
let openMakerMatched = true;
for (index = 0; index < CODETAB_MARKER.length; index++)
if (CODETAB_MARKER[index] !== state.src[start + index]) {
openMakerMatched = false;
break;
}
if (openMakerMatched) {
// found!
autoClosed = true;
nextLine -= 1;
break;
}
}
}
const oldParent = state.parentType;
const oldLineMax = state.lineMax;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
state.parentType = "code_tab";
// this will prevent lazy continuations from ever going past our end marker
state.lineMax = nextLine;
const openToken = state.push(`code_tab_open`, "CodeTab", 1);
openToken.block = true;
openToken.hidden = true;
openToken.markup = markup;
openToken.info = info.replace(/^:active/, "").trim();
openToken.meta = {
active: info.includes(":active"),
};
openToken.map = [startLine, nextLine];
state.md.block.tokenize(state, startLine + 1, nextLine);
const closeToken = state.push(`code_tab_close`, "CodeTab", -1);
closeToken.block = true;
closeToken.hidden = true;
closeToken.markup = "";
state.parentType = oldParent;
state.lineMax = oldLineMax;
state.line = nextLine + (autoClosed ? 1 : 0);
return true;
};
md.block.ruler.before("fence", `code_tabs`, codeTabsRule, {
alt: ["paragraph", "reference", "blockquote", "list"],
});
md.block.ruler.before("fence", "code_tab", codeTabRule, {
alt: ["paragraph", "reference", "blockquote", "list"],
});
md.renderer.rules.code_tabs_open = (
tokens,
index,
options,
env,
self
): string => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { meta } = tokens[index];
let inCodeTab = false;
let foundFence = false;
let codeTabIndex = -1;
let activeIndex = -1;
let title = "";
const codeTabsData: { code: string; title: string }[] = [];
for (let i = index; i < tokens.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { block, meta, type, info } = tokens[i];
if (block) {
if (type === "code_tab_close") {
inCodeTab = false;
continue;
}
if (type === "code_tab_open") {
// found a code tab
inCodeTab = true;
foundFence = false;
codeTabIndex += 1;
// code tab is active
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (meta.active) activeIndex = codeTabIndex;
title = info;
continue;
}
if (type === "code_tabs_close") {
break;
}
if (inCodeTab && type === "fence" && !foundFence) {
foundFence = true;
codeTabsData.push({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
code: self.rules.fence!(tokens, i, options, env, self),
title,
});
}
// hide tokens
tokens[i].type = "code_tabs_empty";
tokens[i].hidden = true;
}
}
return `<CodeTabs :data='${
// single quote will break @vue/compiler-sfc
JSON.stringify(codeTabsData).replace(/'/g, "'")
}'${activeIndex !== -1 ? ` :active="${activeIndex}"` : ""}${
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
meta.id ? ` code-id="${meta.id as string}"` : ""
}>\n`;
};
md.renderer.rules.code_tabs_close = (): string => "</CodeTabs>\n";
};