wikiparser-node 0.7.1-b → 0.8.0-m

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/config/default.json +832 -0
  2. package/config/llwiki.json +630 -0
  3. package/config/minimum.json +142 -0
  4. package/config/moegirl.json +728 -0
  5. package/config/zhwiki.json +1269 -0
  6. package/index.js +79 -0
  7. package/lib/element.js +137 -0
  8. package/lib/node.js +226 -0
  9. package/lib/text.js +123 -0
  10. package/lib/title.js +60 -0
  11. package/mixin/hidden.js +18 -0
  12. package/package.json +9 -11
  13. package/parser/brackets.js +119 -0
  14. package/parser/commentAndExt.js +61 -0
  15. package/parser/converter.js +45 -0
  16. package/parser/externalLinks.js +32 -0
  17. package/parser/hrAndDoubleUnderscore.js +37 -0
  18. package/parser/html.js +41 -0
  19. package/parser/links.js +93 -0
  20. package/parser/list.js +58 -0
  21. package/parser/magicLinks.js +40 -0
  22. package/parser/quotes.js +63 -0
  23. package/parser/table.js +113 -0
  24. package/src/arg.js +89 -0
  25. package/src/atom/hidden.js +11 -0
  26. package/src/atom/index.js +26 -0
  27. package/src/attribute.js +277 -0
  28. package/src/attributes.js +150 -0
  29. package/src/converter.js +70 -0
  30. package/src/converterFlags.js +97 -0
  31. package/src/converterRule.js +75 -0
  32. package/src/extLink.js +60 -0
  33. package/src/gallery.js +101 -0
  34. package/src/hasNowiki/index.js +32 -0
  35. package/src/hasNowiki/pre.js +28 -0
  36. package/src/heading.js +83 -0
  37. package/src/html.js +133 -0
  38. package/src/imageParameter.js +106 -0
  39. package/src/imagemap.js +140 -0
  40. package/src/imagemapLink.js +29 -0
  41. package/src/index.js +407 -0
  42. package/src/link/category.js +13 -0
  43. package/src/link/file.js +125 -0
  44. package/src/link/galleryImage.js +62 -0
  45. package/src/link/index.js +125 -0
  46. package/src/magicLink.js +68 -0
  47. package/src/nested/choose.js +23 -0
  48. package/src/nested/combobox.js +22 -0
  49. package/src/nested/index.js +69 -0
  50. package/src/nested/references.js +22 -0
  51. package/src/nowiki/comment.js +47 -0
  52. package/src/nowiki/dd.js +13 -0
  53. package/src/nowiki/doubleUnderscore.js +26 -0
  54. package/src/nowiki/hr.js +22 -0
  55. package/src/nowiki/index.js +34 -0
  56. package/src/nowiki/list.js +13 -0
  57. package/src/nowiki/noinclude.js +14 -0
  58. package/src/nowiki/quote.js +55 -0
  59. package/src/onlyinclude.js +39 -0
  60. package/src/paramTag/index.js +66 -0
  61. package/src/paramTag/inputbox.js +32 -0
  62. package/src/parameter.js +97 -0
  63. package/src/syntax.js +23 -0
  64. package/src/table/index.js +46 -0
  65. package/src/table/td.js +119 -0
  66. package/src/table/tr.js +74 -0
  67. package/src/tagPair/ext.js +121 -0
  68. package/src/tagPair/include.js +26 -0
  69. package/src/tagPair/index.js +77 -0
  70. package/src/transclude.js +323 -0
  71. package/util/base.js +17 -0
  72. package/util/diff.js +76 -0
  73. package/util/lint.js +54 -0
  74. package/util/string.js +60 -0
  75. package/bundle/bundle.min.js +0 -40
  76. package/extensions/editor.css +0 -60
  77. package/extensions/editor.js +0 -324
  78. package/extensions/ui.css +0 -119
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ const Parser = require('../..'),
4
+ Token = require('..');
5
+
6
+ /**
7
+ * 成对标签
8
+ * @classdesc `{childNodes: [AstText|AttributesToken, AstText|Token]}`
9
+ */
10
+ class TagPairToken extends Token {
11
+ #selfClosing;
12
+ #closed;
13
+ #tags;
14
+
15
+ /** getter */
16
+ get closed() {
17
+ return this.#closed;
18
+ }
19
+
20
+ /**
21
+ * @param {string} name 标签名
22
+ * @param {string|Token} attr 标签属性
23
+ * @param {string|Token} inner 内部wikitext
24
+ * @param {string|undefined} closed 是否封闭;约定`undefined`表示自闭合,`''`表示未闭合
25
+ * @param {accum} accum
26
+ */
27
+ constructor(name, attr, inner, closed, config = Parser.getConfig(), accum = []) {
28
+ super(undefined, config, true);
29
+ this.setAttribute('name', name.toLowerCase());
30
+ this.#tags = [name, closed || name];
31
+ this.#selfClosing = closed === undefined;
32
+ this.#closed = closed !== '';
33
+ this.append(attr, inner);
34
+ let index = accum.indexOf(attr);
35
+ if (index === -1) {
36
+ index = accum.indexOf(inner);
37
+ }
38
+ if (index === -1) {
39
+ index = Infinity;
40
+ }
41
+ accum.splice(index, 0, this);
42
+ }
43
+
44
+ /**
45
+ * @override
46
+ */
47
+ toString(selector) {
48
+ const {firstChild, lastChild} = this,
49
+ [opening, closing] = this.#tags;
50
+ return this.#selfClosing
51
+ ? `<${opening}${String(firstChild)}/>`
52
+ : `<${opening}${String(firstChild)}>${String(lastChild)}${this.#closed ? `</${closing}>` : ''}`;
53
+ }
54
+
55
+ /**
56
+ * @override
57
+ * @returns {string}
58
+ */
59
+ text() {
60
+ const [opening, closing] = this.#tags;
61
+ return this.#selfClosing
62
+ ? `<${opening}${this.firstChild.text()}/>`
63
+ : `<${opening}${super.text('>')}${this.#closed ? `</${closing}>` : ''}`;
64
+ }
65
+
66
+ /** @override */
67
+ getPadding() {
68
+ return this.#tags[0].length + 1;
69
+ }
70
+
71
+ /** @override */
72
+ getGaps() {
73
+ return 1;
74
+ }
75
+ }
76
+
77
+ module.exports = TagPairToken;
@@ -0,0 +1,323 @@
1
+ 'use strict';
2
+
3
+ const {removeComment, text} = require('../util/string'),
4
+ {generateForChild} = require('../util/lint'),
5
+ Parser = require('..'),
6
+ Token = require('.'),
7
+ ParameterToken = require('./parameter'),
8
+ AtomToken = require('./atom'),
9
+ SyntaxToken = require('./syntax');
10
+
11
+ /**
12
+ * 模板或魔术字
13
+ * @classdesc `{childNodes: [AtomToken|SyntaxToken, ...ParameterToken]}`
14
+ */
15
+ class TranscludeToken extends Token {
16
+ type = 'template';
17
+ modifier = '';
18
+ /** @type {Record<string, Set<ParameterToken>>} */ #args = {};
19
+ #fragment = '';
20
+ #valid = true;
21
+
22
+ /**
23
+ * 设置引用修饰符
24
+ * @param {string} modifier 引用修饰符
25
+ * @complexity `n`
26
+ */
27
+ setModifier(modifier = '') {
28
+ if (/\s$/u.test(modifier)) {
29
+ return false;
30
+ }
31
+ const {parserFunction: [,, raw, subst]} = this.getAttribute('config'),
32
+ lcModifier = modifier.trim().toLowerCase(),
33
+ isRaw = raw.includes(lcModifier),
34
+ isSubst = subst.includes(lcModifier);
35
+ if (isRaw || isSubst || modifier === '') {
36
+ this.setAttribute('modifier', modifier);
37
+ return Boolean(modifier);
38
+ }
39
+ return false;
40
+ }
41
+
42
+ /**
43
+ * @param {string} title 模板标题或魔术字
44
+ * @param {[string, string|undefined][]} parts 参数各部分
45
+ * @param {accum} accum
46
+ * @complexity `n`
47
+ * @throws `SyntaxError` 非法的模板名称
48
+ */
49
+ constructor(title, parts, config = Parser.getConfig(), accum = []) {
50
+ super(undefined, config, true, accum, {
51
+ });
52
+ const {parserFunction: [insensitive, sensitive, raw]} = config;
53
+ if (title.includes(':')) {
54
+ const [modifier, ...arg] = title.split(':');
55
+ if (this.setModifier(modifier)) {
56
+ title = arg.join(':');
57
+ }
58
+ }
59
+ if (title.includes(':') || parts.length === 0 && !raw.includes(this.modifier.toLowerCase())) {
60
+ const [magicWord, ...arg] = title.split(':'),
61
+ cleaned = removeComment(magicWord),
62
+ name = cleaned.trim(),
63
+ isSensitive = sensitive.includes(name),
64
+ canonicalCame = insensitive[name.toLowerCase()];
65
+ if (!(arg.length > 0 && /\s$/u.test(cleaned)) && (isSensitive || canonicalCame)) {
66
+ this.setAttribute('name', canonicalCame || name.toLowerCase()).type = 'magic-word';
67
+ const pattern = new RegExp(`^\\s*${name}\\s*$`, isSensitive ? 'u' : 'iu'),
68
+ token = new SyntaxToken(magicWord, pattern, 'magic-word-name', config, accum, {
69
+ });
70
+ this.insertAt(token);
71
+ if (arg.length > 0) {
72
+ parts.unshift([arg.join(':')]);
73
+ }
74
+ if (this.name === 'invoke') {
75
+ for (let i = 0; i < 2; i++) {
76
+ const part = parts.shift();
77
+ if (!part) {
78
+ break;
79
+ }
80
+ const invoke = new AtomToken(part.join('='), `invoke-${
81
+ i ? 'function' : 'module'
82
+ }`, config, accum, {
83
+ });
84
+ this.insertAt(invoke);
85
+ }
86
+ }
87
+ }
88
+ }
89
+ if (this.type === 'template') {
90
+ const name = removeComment(title).split('#')[0].trim();
91
+ if (!name || /\0\d+[eh!+-]\x7F|[<>[\]{}\n]|%[\da-f]{2}/u.test(name)) {
92
+ accum.pop();
93
+ throw new SyntaxError(`非法的模板名称:${name}`);
94
+ }
95
+ const token = new AtomToken(title, 'template-name', config, accum, {
96
+ });
97
+ this.insertAt(token);
98
+ }
99
+ const templateLike = this.isTemplate();
100
+ let i = 1;
101
+ for (let j = 0; j < parts.length; j++) {
102
+ const part = parts[j];
103
+ if (!templateLike && !(this.name === 'switch' && j > 0)) {
104
+ part[0] = part.join('=');
105
+ part.length = 1;
106
+ }
107
+ if (part.length === 1) {
108
+ part.unshift(i);
109
+ i++;
110
+ }
111
+ this.insertAt(new ParameterToken(...part, config, accum));
112
+ }
113
+ }
114
+
115
+ /** @override */
116
+ afterBuild() {
117
+ if (this.isTemplate()) {
118
+ const isTemplate = this.type === 'template',
119
+ titleObj = this.normalizeTitle(this.childNodes[isTemplate ? 0 : 1].text(), isTemplate ? 10 : 828);
120
+ this.#fragment = titleObj.fragment;
121
+ this.#valid = titleObj.valid;
122
+ }
123
+ }
124
+
125
+ /**
126
+ * @override
127
+ */
128
+ toString(selector) {
129
+ const {childNodes, firstChild, modifier} = this;
130
+ return `{{${modifier}${modifier && ':'}${
131
+ this.type === 'magic-word'
132
+ ? `${String(firstChild)}${childNodes.length > 1 ? ':' : ''}${childNodes.slice(1).map(String).join('|')}`
133
+ : super.toString(selector, '|')
134
+ }}}`;
135
+ }
136
+
137
+ /**
138
+ * @override
139
+ * @returns {string}
140
+ * @complexity `n`
141
+ */
142
+ text() {
143
+ const {childNodes, firstChild, modifier} = this;
144
+ return `{{${modifier}${modifier && ':'}${
145
+ this.type === 'magic-word'
146
+ ? `${firstChild.text()}${childNodes.length > 1 ? ':' : ''}${text(childNodes.slice(1), '|')}`
147
+ : super.text('|')
148
+ }}}`;
149
+ }
150
+
151
+ /** @override */
152
+ getPadding() {
153
+ return this.modifier ? this.modifier.length + 3 : 2;
154
+ }
155
+
156
+ /** @override */
157
+ getGaps() {
158
+ return 1;
159
+ }
160
+
161
+ /**
162
+ * @override
163
+ * @param {number} start 起始位置
164
+ */
165
+ lint(start = 0) {
166
+ const errors = super.lint(start),
167
+ {type, childNodes} = this;
168
+ let rect;
169
+ if (!this.isTemplate()) {
170
+ return errors;
171
+ } else if (this.#fragment) {
172
+ rect = {start, ...this.getRootNode().posFromIndex(start)};
173
+ errors.push(generateForChild(childNodes[type === 'template' ? 0 : 1], rect, '多余的fragment'));
174
+ }
175
+ if (!this.#valid) {
176
+ rect = {start, ...this.getRootNode().posFromIndex(start)};
177
+ errors.push(generateForChild(childNodes[1], rect, '非法的模块名称'));
178
+ }
179
+ const duplicatedArgs = this.getDuplicatedArgs();
180
+ if (duplicatedArgs.length > 0) {
181
+ rect ||= {start, ...this.getRootNode().posFromIndex(start)};
182
+ errors.push(...duplicatedArgs.flatMap(([, args]) => args).map(
183
+ arg => generateForChild(arg, rect, '重复参数'),
184
+ ));
185
+ }
186
+ return errors;
187
+ }
188
+
189
+ /** 是否是模板 */
190
+ isTemplate() {
191
+ return this.type === 'template' || this.type === 'magic-word' && this.name === 'invoke';
192
+ }
193
+
194
+ /**
195
+ * 处理匿名参数更改
196
+ * @param {number|ParameterToken} addedToken 新增的参数
197
+ * @complexity `n`
198
+ */
199
+ #handleAnonArgChange(addedToken) {
200
+ const args = this.getAnonArgs(),
201
+ j = args.indexOf(addedToken);
202
+ for (let i = j; i < args.length; i++) {
203
+ const token = args[i],
204
+ {name} = token,
205
+ newName = String(i + 1);
206
+ if (name !== newName) {
207
+ this.getArgs(newName, false, false).add(token.setAttribute('name', newName));
208
+ }
209
+ }
210
+ }
211
+
212
+ /**
213
+ * @override
214
+ * @param {ParameterToken} token 待插入的子节点
215
+ * @param {number} i 插入位置
216
+ * @complexity `n`
217
+ */
218
+ insertAt(token, i = this.length) {
219
+ super.insertAt(token, i);
220
+ if (token.anon) {
221
+ this.#handleAnonArgChange(token);
222
+ } else if (token.name) {
223
+ this.getArgs(token.name, false, false).add(token);
224
+ }
225
+ return token;
226
+ }
227
+
228
+ /**
229
+ * 获取所有参数
230
+ * @returns {ParameterToken[]}
231
+ * @complexity `n`
232
+ */
233
+ getAllArgs() {
234
+ return this.childNodes.filter(child => child instanceof ParameterToken);
235
+ }
236
+
237
+ /**
238
+ * 获取匿名参数
239
+ * @complexity `n`
240
+ */
241
+ getAnonArgs() {
242
+ return this.getAllArgs().filter(({anon}) => anon);
243
+ }
244
+
245
+ /**
246
+ * 获取指定参数
247
+ * @param {string|number} key 参数名
248
+ * @param {boolean} exact 是否匹配匿名性
249
+ * @param {boolean} copy 是否返回一个备份
250
+ * @complexity `n`
251
+ */
252
+ getArgs(key, exact, copy = true) {
253
+ const keyStr = String(key).trim();
254
+ let args;
255
+ if (Object.hasOwn(this.#args, keyStr)) {
256
+ args = this.#args[keyStr];
257
+ } else {
258
+ args = new Set(this.getAllArgs().filter(({name}) => keyStr === name));
259
+ this.#args[keyStr] = args;
260
+ }
261
+ return args;
262
+ }
263
+
264
+ /**
265
+ * 获取重名参数
266
+ * @complexity `n`
267
+ * @returns {[string, ParameterToken[]][]}
268
+ */
269
+ getDuplicatedArgs() {
270
+ if (this.isTemplate()) {
271
+ return Object.entries(this.#args).filter(([, {size}]) => size > 1)
272
+ .map(([key, args]) => [key, [...args]]);
273
+ }
274
+ return [];
275
+ }
276
+
277
+ /**
278
+ * 对特定魔术字获取可能的取值
279
+ * @this {ParameterToken}}
280
+ * @throws `Error` 不是可接受的魔术字
281
+ */
282
+ getPossibleValues() {
283
+ const {type, name, childNodes, constructor: {name: cName}} = this;
284
+ if (type === 'template') {
285
+ throw new Error(`${cName}.getPossibleValues 方法仅供特定魔术字使用!`);
286
+ }
287
+ let start;
288
+ switch (name) {
289
+ case 'if':
290
+ case 'ifexist':
291
+ case 'ifexpr':
292
+ case 'iferror':
293
+ start = 2;
294
+ break;
295
+ case 'ifeq':
296
+ start = 3;
297
+ break;
298
+ default:
299
+ throw new Error(`${cName}.getPossibleValues 方法仅供特定魔术字使用!`);
300
+ }
301
+ const /** @type {Token[]} */ queue = childNodes.slice(start, start + 2).map(({childNodes: [, value]}) => value);
302
+ for (let i = 0; i < queue.length;) {
303
+ /** @type {Token[] & {0: TranscludeToken}} */
304
+ const {length, 0: first} = queue[i].childNodes.filter(child => child.text().trim());
305
+ if (length === 0) {
306
+ queue.splice(i, 1);
307
+ } else if (length > 1 || first.type !== 'magic-word') {
308
+ i++;
309
+ } else {
310
+ try {
311
+ const possibleValues = first.getPossibleValues();
312
+ queue.splice(i, 1, ...possibleValues);
313
+ i += possibleValues.length;
314
+ } catch {
315
+ i++;
316
+ }
317
+ }
318
+ }
319
+ return queue;
320
+ }
321
+ }
322
+
323
+ module.exports = TranscludeToken;
package/util/base.js ADDED
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 是否是普通对象
5
+ * @param {*} obj 对象
6
+ */
7
+ const isPlainObject = obj => Boolean(obj) && Object.getPrototypeOf(obj).constructor === Object;
8
+
9
+ /**
10
+ * 延时
11
+ * @param {number} t 秒数
12
+ */
13
+ const sleep = t => new Promise(resolve => {
14
+ setTimeout(resolve, t * 1000);
15
+ });
16
+
17
+ module.exports = {isPlainObject, sleep};
package/util/diff.js ADDED
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ const {spawn} = require('child_process'),
4
+ fs = require('fs/promises');
5
+
6
+ process.on('unhandledRejection', e => {
7
+ console.error(e);
8
+ });
9
+
10
+ /**
11
+ * 将shell命令转化为Promise对象
12
+ * @param {string} command shell指令
13
+ * @param {string[]} args shell输入参数
14
+ * @returns {Promise<?string>}
15
+ */
16
+ const cmd = (command, args) => new Promise(resolve => {
17
+ let timer, shell;
18
+
19
+ /**
20
+ * 清除进程并返回
21
+ * @param {*} val 返回值
22
+ */
23
+ const r = val => {
24
+ clearTimeout(timer);
25
+ shell.kill('SIGINT');
26
+ resolve(val);
27
+ };
28
+ try {
29
+ shell = spawn(command, args);
30
+ timer = setTimeout(() => {
31
+ shell.kill('SIGINT');
32
+ }, 60 * 1000);
33
+ let buf = '';
34
+ shell.stdout.on('data', data => {
35
+ buf += data.toString();
36
+ });
37
+ shell.stdout.on('end', () => {
38
+ r(buf);
39
+ });
40
+ shell.on('exit', () => {
41
+ r(shell.killed ? undefined : '');
42
+ });
43
+ shell.on('error', () => {
44
+ r(undefined);
45
+ });
46
+ } catch {
47
+ r(undefined);
48
+ }
49
+ });
50
+
51
+ /**
52
+ * 比较两个文件
53
+ * @param {string} oldStr 旧文本
54
+ * @param {string} newStr 新文本
55
+ * @param {string} uid 唯一标识
56
+ */
57
+ const diff = async (oldStr, newStr, uid = '') => {
58
+ if (oldStr === newStr) {
59
+ return;
60
+ }
61
+ const oldFile = `diffOld${uid}`,
62
+ newFile = `diffNew${uid}`;
63
+ await Promise.all([fs.writeFile(oldFile, oldStr), fs.writeFile(newFile, newStr)]);
64
+ const stdout = await cmd('git', [
65
+ 'diff',
66
+ '--color-words=[\xC0-\xFF][\x80-\xBF]+|<?/?\\w+/?>?|[^[:space:]]',
67
+ '-U0',
68
+ '--no-index',
69
+ oldFile,
70
+ newFile,
71
+ ]);
72
+ await Promise.all([fs.unlink(oldFile), fs.unlink(newFile)]);
73
+ console.log(stdout?.split('\n')?.slice(4)?.join('\n'));
74
+ };
75
+
76
+ module.exports = diff;
package/util/lint.js ADDED
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const Token = require('../src');
4
+
5
+ /**
6
+ * 生成对于子节点的LintError对象
7
+ * @param {Token} child 子节点
8
+ * @param {{top: number, left: number, start: number}} boundingRect 父节点的绝对定位
9
+ * @param {string} message 错误信息
10
+ * @param {'error'|'warning'} severity 严重程度
11
+ * @returns {LintError}
12
+ */
13
+ const generateForChild = (child, boundingRect, message, severity = 'error') => {
14
+ const index = child.getRelativeIndex(),
15
+ {offsetHeight, offsetWidth, parentNode, length} = child,
16
+ {top: offsetTop, left: offsetLeft} = parentNode.posFromIndex(index),
17
+ {start} = boundingRect,
18
+ {top, left} = 'top' in boundingRect ? boundingRect : child.getRootNode().posFromIndex(start),
19
+ excerpt = String(child).slice(0, 50),
20
+ startIndex = start + index,
21
+ endIndex = startIndex + length,
22
+ startLine = top + offsetTop,
23
+ endLine = startLine + offsetHeight - 1,
24
+ startCol = offsetTop ? offsetLeft : left + offsetLeft,
25
+ endCol = offsetHeight > 1 ? offsetWidth : startCol + offsetWidth;
26
+ return {message, severity, startIndex, endIndex, startLine, endLine, startCol, endCol, excerpt};
27
+ };
28
+
29
+ /**
30
+ * 生成对于自己的LintError对象
31
+ * @param {Token} token 节点
32
+ * @param {{top: number, left: number, start: number}} boundingRect 绝对定位
33
+ * @param {string} message 错误信息
34
+ * @param {'error'|'warning'} severity 严重程度
35
+ * @returns {LintError}
36
+ */
37
+ const generateForSelf = (token, boundingRect, message, severity = 'error') => {
38
+ const {start} = boundingRect,
39
+ {offsetHeight, offsetWidth, length} = token,
40
+ {top, left} = 'top' in boundingRect ? boundingRect : token.getRootNode().posFromIndex(start);
41
+ return {
42
+ message,
43
+ severity,
44
+ startIndex: start,
45
+ endIndex: start + length,
46
+ startLine: top,
47
+ endLine: top + offsetHeight - 1,
48
+ startCol: left,
49
+ endCol: offsetHeight > 1 ? offsetWidth : left + offsetWidth,
50
+ excerpt: String(token).slice(0, 50),
51
+ };
52
+ };
53
+
54
+ module.exports = {generateForChild, generateForSelf};
package/util/string.js ADDED
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ const extUrlCharFirst = '(?:\\[[\\da-f:.]+\\]|[^[\\]<>"\\0-\\x1F\\x7F\\p{Zs}\\uFFFD])',
4
+ extUrlChar = '(?:[^[\\]<>"\\0-\\x1F\\x7F\\p{Zs}\\uFFFD]|\\0\\d+c\\x7F)*';
5
+
6
+ /**
7
+ * remove half-parsed comment-like tokens
8
+ * @param {string} str 原字符串
9
+ */
10
+ const removeComment = str => str.replace(/\0\d+c\x7F/gu, '');
11
+
12
+ /**
13
+ * escape special chars for RegExp constructor
14
+ * @param {string} str RegExp source
15
+ */
16
+ const escapeRegExp = str => str.replace(/[\\{}()|.?*+^$[\]]/gu, '\\$&');
17
+
18
+ /**
19
+ * a more sophisticated string-explode function
20
+ * @param {string} start start syntax of a nested AST node
21
+ * @param {string} end end syntax of a nested AST node
22
+ * @param {string} separator syntax for explosion
23
+ * @param {string} str string to be exploded
24
+ */
25
+ const explode = (start, end, separator, str) => {
26
+ if (str === undefined) {
27
+ return [];
28
+ }
29
+ const regex = new RegExp(`${[start, end, separator].map(escapeRegExp).join('|')}`, 'gu'),
30
+ /** @type {string[]} */ exploded = [];
31
+ let mt = regex.exec(str),
32
+ depth = 0,
33
+ lastIndex = 0;
34
+ while (mt) {
35
+ const {0: match, index} = mt;
36
+ if (match !== separator) {
37
+ depth += match === start ? 1 : -1;
38
+ } else if (depth === 0) {
39
+ exploded.push(str.slice(lastIndex, index));
40
+ ({lastIndex} = regex);
41
+ }
42
+ mt = regex.exec(str);
43
+ }
44
+ exploded.push(str.slice(lastIndex));
45
+ return exploded;
46
+ };
47
+
48
+ /**
49
+ * extract effective wikitext
50
+ * @param {(string|AstNode)[]} childNodes a Token's contents
51
+ * @param {string} separator delimiter between nodes
52
+ */
53
+ const text = (childNodes, separator = '') => {
54
+ const AstNode = require('../lib/node');
55
+ return childNodes.map(child => typeof child === 'string' ? child : child.text()).join(separator);
56
+ };
57
+
58
+ module.exports = {
59
+ extUrlCharFirst, extUrlChar, removeComment, escapeRegExp, explode, text,
60
+ };