wikiparser-node 0.0.3 → 0.2.1
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.
- package/README.md +29 -1931
- package/config/default.json +12 -1
- package/config/llwiki.json +12 -1
- package/config/moegirl.json +9 -1
- package/errors/2022-07-04T22:30:41.785Z +1 -0
- package/errors/2022-07-04T22:30:41.785Z.err +11 -0
- package/errors/2022-07-04T22:30:41.785Z.json +5 -0
- package/index.js +43 -0
- package/lib/element.js +2 -1
- package/mixin/sol.js +42 -0
- package/package.json +2 -2
- package/parser/converter.js +44 -0
- package/parser/list.js +58 -0
- package/src/arg.js +6 -6
- package/src/attribute.js +27 -17
- package/src/converter.js +135 -0
- package/src/converterFlags.js +214 -0
- package/src/converterRule.js +209 -0
- package/src/extLink.js +21 -9
- package/src/heading.js +13 -17
- package/src/imageParameter.js +1 -1
- package/src/index.js +33 -18
- package/src/link/file.js +2 -2
- package/src/link/index.js +7 -7
- package/src/nowiki/dd.js +49 -0
- package/src/nowiki/hr.js +3 -2
- package/src/nowiki/list.js +16 -0
- package/src/parameter.js +2 -2
- package/src/syntax.js +3 -1
- package/src/table/index.js +9 -9
- package/src/table/td.js +1 -0
- package/src/table/tr.js +4 -6
- package/src/transclude.js +14 -14
- package/typings/index.d.ts +2 -0
- package/typings/node.d.ts +3 -3
- package/typings/token.d.ts +1 -0
- package/util/string.js +13 -1
- package/src/listToken.js +0 -47
package/src/index.js
CHANGED
|
@@ -37,6 +37,8 @@
|
|
|
37
37
|
* l: LinkToken
|
|
38
38
|
* q: QuoteToken
|
|
39
39
|
* w: ExtLinkToken
|
|
40
|
+
* d: ListToken
|
|
41
|
+
* v: ConverterToken
|
|
40
42
|
*/
|
|
41
43
|
|
|
42
44
|
const {externalUse} = require('../util/debug'),
|
|
@@ -201,9 +203,13 @@ class Token extends AstElement {
|
|
|
201
203
|
* @complexity `n`
|
|
202
204
|
*/
|
|
203
205
|
removeAt(i) {
|
|
206
|
+
if (typeof i !== 'number') {
|
|
207
|
+
this.typeError('removeAt', 'Number');
|
|
208
|
+
}
|
|
209
|
+
const iPos = i < 0 ? i + this.childNodes.length : i;
|
|
204
210
|
if (!Parser.running) {
|
|
205
211
|
const protectedIndices = this.#protectedChildren.applyTo(this.childNodes);
|
|
206
|
-
if (protectedIndices.includes(
|
|
212
|
+
if (protectedIndices.includes(iPos)) {
|
|
207
213
|
throw new Error(`${this.constructor.name} 的第 ${i} 个子节点不可移除!`);
|
|
208
214
|
} else if (this.#acceptable) {
|
|
209
215
|
const acceptableIndices = Object.fromEntries(
|
|
@@ -436,11 +442,7 @@ class Token extends AstElement {
|
|
|
436
442
|
this.#parseLinks();
|
|
437
443
|
break;
|
|
438
444
|
case 6: {
|
|
439
|
-
|
|
440
|
-
for (let i = 0; i < lines.length; i++) {
|
|
441
|
-
lines[i] = this.#parseQuotes(lines[i]);
|
|
442
|
-
}
|
|
443
|
-
this.setText(lines.join('\n'));
|
|
445
|
+
this.#parseQuotes();
|
|
444
446
|
break;
|
|
445
447
|
}
|
|
446
448
|
case 7:
|
|
@@ -450,8 +452,10 @@ class Token extends AstElement {
|
|
|
450
452
|
this.#parseMagicLinks();
|
|
451
453
|
break;
|
|
452
454
|
case 9:
|
|
455
|
+
this.#parseList();
|
|
453
456
|
break;
|
|
454
457
|
case 10:
|
|
458
|
+
this.#parseConverter();
|
|
455
459
|
// no default
|
|
456
460
|
}
|
|
457
461
|
if (this.type === 'root') {
|
|
@@ -530,25 +534,21 @@ class Token extends AstElement {
|
|
|
530
534
|
return n ? this.build().afterBuild() : this;
|
|
531
535
|
}
|
|
532
536
|
|
|
533
|
-
/** @this {Token & {firstChild: string}} */
|
|
534
537
|
#parseCommentAndExt(includeOnly = false) {
|
|
535
538
|
const parseCommentAndExt = require('../parser/commentAndExt');
|
|
536
539
|
this.setText(parseCommentAndExt(this.firstChild, this.#config, this.#accum, includeOnly));
|
|
537
540
|
}
|
|
538
541
|
|
|
539
|
-
/** @this {Token & {firstChild: string}} */
|
|
540
542
|
#parseBrackets() {
|
|
541
543
|
const parseBrackets = require('../parser/brackets');
|
|
542
544
|
this.setText(parseBrackets(this.firstChild, this.#config, this.#accum));
|
|
543
545
|
}
|
|
544
546
|
|
|
545
|
-
/** @this {Token & {firstChild: string}} */
|
|
546
547
|
#parseHtml() {
|
|
547
548
|
const parseHtml = require('../parser/html');
|
|
548
549
|
this.setText(parseHtml(this.firstChild, this.#config, this.#accum));
|
|
549
550
|
}
|
|
550
551
|
|
|
551
|
-
/** @this {Token & {firstChild: string}} */
|
|
552
552
|
#parseTable() {
|
|
553
553
|
const parseTable = require('../parser/table'),
|
|
554
554
|
TableToken = require('./table');
|
|
@@ -567,35 +567,50 @@ class Token extends AstElement {
|
|
|
567
567
|
}
|
|
568
568
|
}
|
|
569
569
|
|
|
570
|
-
/** @this {Token & {firstChild: string}} */
|
|
571
570
|
#parseHrAndDoubleUndescore() {
|
|
572
571
|
const parseHrAndDoubleUnderscore = require('../parser/hrAndDoubleUnderscore');
|
|
573
572
|
this.setText(parseHrAndDoubleUnderscore(this.firstChild, this.#config, this.#accum));
|
|
574
573
|
}
|
|
575
574
|
|
|
576
|
-
/** @this {Token & {firstChild: string}} */
|
|
577
575
|
#parseLinks() {
|
|
578
576
|
const parseLinks = require('../parser/links');
|
|
579
577
|
this.setText(parseLinks(this.firstChild, this.#config, this.#accum));
|
|
580
578
|
}
|
|
581
579
|
|
|
582
|
-
/** @
|
|
583
|
-
#parseQuotes(
|
|
584
|
-
const parseQuotes = require('../parser/quotes')
|
|
585
|
-
|
|
580
|
+
/** @this {Token & {firstChild: string}} */
|
|
581
|
+
#parseQuotes() {
|
|
582
|
+
const parseQuotes = require('../parser/quotes'),
|
|
583
|
+
lines = this.firstChild.split('\n');
|
|
584
|
+
for (let i = 0; i < lines.length; i++) {
|
|
585
|
+
lines[i] = parseQuotes(lines[i], this.#config, this.#accum);
|
|
586
|
+
}
|
|
587
|
+
this.setText(lines.join('\n'));
|
|
586
588
|
}
|
|
587
589
|
|
|
588
|
-
/** @this {Token & {firstChild: string}} */
|
|
589
590
|
#parseExternalLinks() {
|
|
590
591
|
const parseExternalLinks = require('../parser/externalLinks');
|
|
591
592
|
this.setText(parseExternalLinks(this.firstChild, this.#config, this.#accum));
|
|
592
593
|
}
|
|
593
594
|
|
|
594
|
-
/** @this {Token & {firstChild: string}} */
|
|
595
595
|
#parseMagicLinks() {
|
|
596
596
|
const parseMagicLinks = require('../parser/magicLinks');
|
|
597
597
|
this.setText(parseMagicLinks(this.firstChild, this.#config, this.#accum));
|
|
598
598
|
}
|
|
599
|
+
|
|
600
|
+
/** @this {Token & {firstChild: string}} */
|
|
601
|
+
#parseList() {
|
|
602
|
+
const parseList = require('../parser/list'),
|
|
603
|
+
lines = this.firstChild.split('\n');
|
|
604
|
+
for (let i = this.type === 'root' ? 0 : 1; i < lines.length; i++) {
|
|
605
|
+
lines[i] = parseList(lines[i], this.#config, this.#accum);
|
|
606
|
+
}
|
|
607
|
+
this.setText(lines.join('\n'));
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
#parseConverter() {
|
|
611
|
+
const parseConverter = require('../parser/converter');
|
|
612
|
+
this.setText(parseConverter(this.firstChild, this.#config, this.#accum));
|
|
613
|
+
}
|
|
599
614
|
}
|
|
600
615
|
|
|
601
616
|
Parser.classes.Token = __filename;
|
package/src/link/file.js
CHANGED
|
@@ -84,7 +84,7 @@ class FileToken extends LinkToken {
|
|
|
84
84
|
* @param {ImageParameterToken} token
|
|
85
85
|
* @complexity `n`
|
|
86
86
|
*/
|
|
87
|
-
insertAt(token, i = this.
|
|
87
|
+
insertAt(token, i = this.childNodes.length) {
|
|
88
88
|
if (!Parser.running) {
|
|
89
89
|
this.getArgs(token.name, false, false).add(token);
|
|
90
90
|
this.#keys.add(token.name);
|
|
@@ -221,7 +221,7 @@ class FileToken extends LinkToken {
|
|
|
221
221
|
root = Parser.parse(wikitext, this.getAttribute('include'), 6, config),
|
|
222
222
|
{childNodes: {length}, firstElementChild} = root;
|
|
223
223
|
if (length !== 1 || !firstElementChild?.matches('file#File:F')
|
|
224
|
-
|| firstElementChild.
|
|
224
|
+
|| firstElementChild.childNodes.length !== 2 || firstElementChild.lastElementChild.name !== key
|
|
225
225
|
) {
|
|
226
226
|
throw new SyntaxError(`非法的 ${key} 参数:${noWrap(value)}`);
|
|
227
227
|
}
|
package/src/link/index.js
CHANGED
|
@@ -29,9 +29,9 @@ class LinkToken extends Token {
|
|
|
29
29
|
'Stage-2': ':', '!ExtToken': '', '!HeadingToken': '',
|
|
30
30
|
}));
|
|
31
31
|
if (linkText !== undefined) {
|
|
32
|
-
const inner = new Token(linkText, config, true, accum);
|
|
32
|
+
const inner = new Token(linkText, config, true, accum, {'Stage-5': ':', ConverterToken: ':'});
|
|
33
33
|
inner.type = 'link-text';
|
|
34
|
-
this.appendChild(inner.setAttribute('stage',
|
|
34
|
+
this.appendChild(inner.setAttribute('stage', Parser.MAX_STAGE - 1));
|
|
35
35
|
}
|
|
36
36
|
this.selfLink = !title.title;
|
|
37
37
|
this.fragment = title.fragment;
|
|
@@ -107,7 +107,7 @@ class LinkToken extends Token {
|
|
|
107
107
|
|
|
108
108
|
/** @returns {[number, string][]} */
|
|
109
109
|
plain() {
|
|
110
|
-
return this.
|
|
110
|
+
return this.childNodes.length === 1 ? [] : this.lastElementChild.plain();
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
/** @param {string} link */
|
|
@@ -118,7 +118,7 @@ class LinkToken extends Token {
|
|
|
118
118
|
}
|
|
119
119
|
const root = Parser.parse(`[[${link}]]`, this.getAttribute('include'), 6, this.getAttribute('config')),
|
|
120
120
|
{childNodes: {length}, firstElementChild} = root;
|
|
121
|
-
if (length !== 1 || firstElementChild?.type !== this.type || firstElementChild.
|
|
121
|
+
if (length !== 1 || firstElementChild?.type !== this.type || firstElementChild.childNodes.length !== 1) {
|
|
122
122
|
const msgs = {link: '内链', file: '文件链接', category: '分类'};
|
|
123
123
|
throw new SyntaxError(`非法的${msgs[this.type]}目标:${link}`);
|
|
124
124
|
}
|
|
@@ -135,7 +135,7 @@ class LinkToken extends Token {
|
|
|
135
135
|
config = this.getAttribute('config'),
|
|
136
136
|
root = Parser.parse(`[[${page ? `:${this.name}` : ''}#${fragment}]]`, include, 6, config),
|
|
137
137
|
{childNodes: {length}, firstElementChild} = root;
|
|
138
|
-
if (length !== 1 || firstElementChild?.type !== 'link' || firstElementChild.
|
|
138
|
+
if (length !== 1 || firstElementChild?.type !== 'link' || firstElementChild.childNodes.length !== 1) {
|
|
139
139
|
throw new SyntaxError(`非法的 fragment:${fragment}`);
|
|
140
140
|
}
|
|
141
141
|
if (page) {
|
|
@@ -169,7 +169,7 @@ class LinkToken extends Token {
|
|
|
169
169
|
this.type === 'category' ? 'Category:' : ''
|
|
170
170
|
}L|${linkText}]]`, this.getAttribute('include'), 6, config),
|
|
171
171
|
{childNodes: {length}, firstElementChild} = root;
|
|
172
|
-
if (length !== 1 || firstElementChild?.type !== this.type || firstElementChild.
|
|
172
|
+
if (length !== 1 || firstElementChild?.type !== this.type || firstElementChild.childNodes.length !== 2) {
|
|
173
173
|
throw new SyntaxError(`非法的${this.type === 'link' ? '内链文字' : '分类关键字'}:${noWrap(linkText)}`);
|
|
174
174
|
}
|
|
175
175
|
({lastElementChild} = firstElementChild);
|
|
@@ -177,7 +177,7 @@ class LinkToken extends Token {
|
|
|
177
177
|
lastElementChild = Parser.run(() => new Token('', config));
|
|
178
178
|
lastElementChild.setAttribute('stage', 7).type = 'link-text';
|
|
179
179
|
}
|
|
180
|
-
if (this.
|
|
180
|
+
if (this.childNodes.length === 1) {
|
|
181
181
|
this.appendChild(lastElementChild);
|
|
182
182
|
} else {
|
|
183
183
|
this.lastElementChild.safeReplaceWith(lastElementChild);
|
package/src/nowiki/dd.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const /** @type {Parser} */ Parser = require('../..'),
|
|
4
|
+
NowikiToken = require('.');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* :
|
|
8
|
+
* @classdesc `{childNodes: [string]}`
|
|
9
|
+
*/
|
|
10
|
+
class DdToken extends NowikiToken {
|
|
11
|
+
type = 'dd';
|
|
12
|
+
dt;
|
|
13
|
+
ul;
|
|
14
|
+
ol;
|
|
15
|
+
indent;
|
|
16
|
+
|
|
17
|
+
/** @param {string} str */
|
|
18
|
+
#update(str) {
|
|
19
|
+
this.setAttribute('ul', str.includes('*')).setAttribute('ol', str.includes('#'))
|
|
20
|
+
.setAttribute('dt', str.includes(';')).setAttribute('indent', str.split(':').length - 1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {string} str
|
|
25
|
+
* @param {accum} accum
|
|
26
|
+
*/
|
|
27
|
+
constructor(str, config = Parser.getConfig(), accum = []) {
|
|
28
|
+
super(str, config, accum);
|
|
29
|
+
this.seal(['dt', 'ul', 'ol', 'indent']).#update(str);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @returns {[number, string][]} */
|
|
33
|
+
plain() {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @param {string} str */
|
|
38
|
+
setText(str) {
|
|
39
|
+
const src = this.type === 'dd' ? ':' : ';:*#';
|
|
40
|
+
if (new RegExp(`[^${src}]`).test(str)) {
|
|
41
|
+
throw new RangeError(`${this.constructor.name} 仅能包含${src.split('').map(c => `"${c}"`).join('、')}!`);
|
|
42
|
+
}
|
|
43
|
+
this.#update(str);
|
|
44
|
+
return super.setText(str);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Parser.classes.DdToken = __filename;
|
|
49
|
+
module.exports = DdToken;
|
package/src/nowiki/hr.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const sol = require('../../mixin/sol'),
|
|
4
|
+
/** @type {Parser} */ Parser = require('../..'),
|
|
4
5
|
NowikiToken = require('.');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* `<hr>`
|
|
8
9
|
* @classdesc `{childNodes: [string]}`
|
|
9
10
|
*/
|
|
10
|
-
class HrToken extends NowikiToken {
|
|
11
|
+
class HrToken extends sol(NowikiToken) {
|
|
11
12
|
type = 'hr';
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const sol = require('../../mixin/sol'),
|
|
4
|
+
/** @type {Parser} */ Parser = require('../..'),
|
|
5
|
+
DdToken = require('./dd');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ;:*#
|
|
9
|
+
* @classdesc `{childNodes: [string]}`
|
|
10
|
+
*/
|
|
11
|
+
class ListToken extends sol(DdToken) {
|
|
12
|
+
type = 'list';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
Parser.classes.ListToken = __filename;
|
|
16
|
+
module.exports = ListToken;
|
package/src/parameter.js
CHANGED
|
@@ -113,7 +113,7 @@ class ParameterToken extends fixedToken(Token) {
|
|
|
113
113
|
{childNodes: {length}, firstElementChild} = root,
|
|
114
114
|
/** @type {ParameterToken} */ lastElementChild = firstElementChild?.lastElementChild;
|
|
115
115
|
if (length !== 1 || !firstElementChild?.matches(templateLike ? 'template#T' : 'magic-word#lc')
|
|
116
|
-
|| firstElementChild.
|
|
116
|
+
|| firstElementChild.childNodes.length !== 2
|
|
117
117
|
|| lastElementChild.anon !== this.anon || lastElementChild.name !== '1'
|
|
118
118
|
) {
|
|
119
119
|
throw new SyntaxError(`非法的模板参数:${noWrap(value)}`);
|
|
@@ -139,7 +139,7 @@ class ParameterToken extends fixedToken(Token) {
|
|
|
139
139
|
}
|
|
140
140
|
const root = Parser.parse(`{{:T|${key}=}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
141
141
|
{childNodes: {length}, firstElementChild} = root;
|
|
142
|
-
if (length !== 1 || !firstElementChild?.matches('template#T') || firstElementChild.
|
|
142
|
+
if (length !== 1 || !firstElementChild?.matches('template#T') || firstElementChild.childNodes.length !== 2) {
|
|
143
143
|
throw new SyntaxError(`非法的模板参数名:${key}`);
|
|
144
144
|
}
|
|
145
145
|
const {lastElementChild} = firstElementChild,
|
package/src/syntax.js
CHANGED
|
@@ -38,8 +38,10 @@ class SyntaxToken extends Token {
|
|
|
38
38
|
afterBuild() {
|
|
39
39
|
const that = this,
|
|
40
40
|
/** @type {AstListener} */ syntaxListener = (e, data) => {
|
|
41
|
-
|
|
41
|
+
const pattern = that.#pattern;
|
|
42
|
+
if (!Parser.running && !pattern.test(that.text())) {
|
|
42
43
|
undo(e, data);
|
|
44
|
+
throw new Error(`不可修改 ${that.constructor.name} 的语法:/${pattern.source}/${pattern.flags}`);
|
|
43
45
|
}
|
|
44
46
|
};
|
|
45
47
|
this.addEventListener(['remove', 'insert', 'replace', 'text'], syntaxListener);
|
package/src/table/index.js
CHANGED
|
@@ -70,15 +70,15 @@ class TableToken extends TrToken {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
|
-
* @template {
|
|
73
|
+
* @template {TrToken|SyntaxToken} T
|
|
74
74
|
* @param {T} token
|
|
75
75
|
* @returns {T}
|
|
76
76
|
* @complexity `n`
|
|
77
77
|
*/
|
|
78
78
|
insertAt(token, i = this.childNodes.length) {
|
|
79
|
-
const previous = this.
|
|
79
|
+
const previous = this.children.at(i - 1),
|
|
80
80
|
{closingPattern} = TableToken;
|
|
81
|
-
if (token
|
|
81
|
+
if (token.type === 'td' && previous.type === 'tr') {
|
|
82
82
|
Parser.warn('改为将单元格插入当前行。');
|
|
83
83
|
return previous.appendChild(token);
|
|
84
84
|
} else if (!Parser.running && i === this.childNodes.length && token instanceof SyntaxToken
|
|
@@ -417,14 +417,14 @@ class TableToken extends TrToken {
|
|
|
417
417
|
/** @complexity `n` */
|
|
418
418
|
#prependTableRow() {
|
|
419
419
|
const row = Parser.run(() => new TrToken('\n|-', undefined, this.getAttribute('config'))),
|
|
420
|
-
{
|
|
421
|
-
[,, plain] =
|
|
422
|
-
start =
|
|
423
|
-
/** @type {TdToken[]} */
|
|
424
|
-
index =
|
|
420
|
+
{children} = this,
|
|
421
|
+
[,, plain] = children,
|
|
422
|
+
start = plain?.isPlain() ? 3 : 2,
|
|
423
|
+
/** @type {TdToken[]} */ tdChildren = children.slice(start),
|
|
424
|
+
index = tdChildren.findIndex(({type}) => type !== 'td');
|
|
425
425
|
this.insertAt(row, index === -1 ? -1 : index + start);
|
|
426
426
|
Parser.run(() => {
|
|
427
|
-
for (const cell of
|
|
427
|
+
for (const cell of tdChildren.slice(0, index === -1 ? undefined : index)) {
|
|
428
428
|
if (cell.subtype !== 'caption') {
|
|
429
429
|
row.appendChild(cell);
|
|
430
430
|
}
|
package/src/table/td.js
CHANGED
package/src/table/tr.js
CHANGED
|
@@ -37,7 +37,7 @@ class TrToken extends attributeParent(Token, 1) {
|
|
|
37
37
|
const token = new Constructor(undefined, undefined, this.getAttribute('config'));
|
|
38
38
|
token.firstElementChild.safeReplaceWith(syntax);
|
|
39
39
|
token.children[1].safeReplaceWith(attr);
|
|
40
|
-
if (token.
|
|
40
|
+
if (token.type === 'td') { // TdToken
|
|
41
41
|
token.children[2].safeReplaceWith(inner);
|
|
42
42
|
} else if (inner !== undefined) {
|
|
43
43
|
token.appendChild(inner);
|
|
@@ -48,15 +48,13 @@ class TrToken extends attributeParent(Token, 1) {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
#correct() {
|
|
51
|
-
const [,, child] = this.
|
|
52
|
-
if (
|
|
53
|
-
this.setText(`\n${child}`, 2);
|
|
54
|
-
} else if (typeof child !== 'string' && child?.isPlain()) {
|
|
51
|
+
const [,, child] = this.children;
|
|
52
|
+
if (child?.isPlain()) {
|
|
55
53
|
const {firstChild} = child;
|
|
56
54
|
if (typeof firstChild !== 'string') {
|
|
57
55
|
child.prepend('\n');
|
|
58
56
|
} else if (!firstChild.startsWith('\n')) {
|
|
59
|
-
child.setText(`\n${firstChild}
|
|
57
|
+
child.setText(`\n${firstChild}`);
|
|
60
58
|
}
|
|
61
59
|
}
|
|
62
60
|
}
|
package/src/transclude.js
CHANGED
|
@@ -27,7 +27,7 @@ class TranscludeToken extends Token {
|
|
|
27
27
|
isSubst = subst.includes(lcModifier),
|
|
28
28
|
wasRaw = raw.includes(this.modifier.trim().toLowerCase());
|
|
29
29
|
if (wasRaw && isRaw || !wasRaw && (isSubst || modifier === '')
|
|
30
|
-
|| (Parser.running || this.
|
|
30
|
+
|| (Parser.running || this.childNodes.length > 1) && (isRaw || isSubst || modifier === '')
|
|
31
31
|
) {
|
|
32
32
|
this.setAttribute('modifier', modifier);
|
|
33
33
|
return Boolean(modifier);
|
|
@@ -180,10 +180,10 @@ class TranscludeToken extends Token {
|
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
toString() {
|
|
183
|
-
const {children,
|
|
183
|
+
const {children, childNodes: {length}, firstChild} = this;
|
|
184
184
|
return `{{${this.modifier}${this.modifier && ':'}${
|
|
185
185
|
this.type === 'magic-word'
|
|
186
|
-
? `${String(firstChild)}${
|
|
186
|
+
? `${String(firstChild)}${length > 1 ? ':' : ''}${children.slice(1).map(String).join('|')}`
|
|
187
187
|
: super.toString('|')
|
|
188
188
|
}}}`;
|
|
189
189
|
}
|
|
@@ -201,10 +201,10 @@ class TranscludeToken extends Token {
|
|
|
201
201
|
* @complexity `n`
|
|
202
202
|
*/
|
|
203
203
|
text() {
|
|
204
|
-
const {children,
|
|
204
|
+
const {children, childNodes: {length}, firstElementChild} = this;
|
|
205
205
|
return `{{${this.modifier}${this.modifier && ':'}${
|
|
206
206
|
this.type === 'magic-word'
|
|
207
|
-
? `${firstElementChild.text()}${
|
|
207
|
+
? `${firstElementChild.text()}${length > 1 ? ':' : ''}${text(children.slice(1), '|')}`
|
|
208
208
|
: super.text('|')
|
|
209
209
|
}}}`;
|
|
210
210
|
}
|
|
@@ -261,7 +261,7 @@ class TranscludeToken extends Token {
|
|
|
261
261
|
* @param {ParameterToken} token
|
|
262
262
|
* @complexity `n`
|
|
263
263
|
*/
|
|
264
|
-
insertAt(token, i = this.
|
|
264
|
+
insertAt(token, i = this.childNodes.length) {
|
|
265
265
|
super.insertAt(token, i);
|
|
266
266
|
if (token.anon) {
|
|
267
267
|
this.#handleAnonArgChange(token);
|
|
@@ -381,7 +381,7 @@ class TranscludeToken extends Token {
|
|
|
381
381
|
root = Parser.parse(wikitext, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
382
382
|
{childNodes: {length}, firstElementChild} = root;
|
|
383
383
|
if (length !== 1 || !firstElementChild?.matches(templateLike ? 'template#T' : 'magic-word#lc')
|
|
384
|
-
|| firstElementChild.
|
|
384
|
+
|| firstElementChild.childNodes.length !== 2 || !firstElementChild.lastElementChild.anon
|
|
385
385
|
) {
|
|
386
386
|
throw new SyntaxError(`非法的匿名参数:${noWrap(val)}`);
|
|
387
387
|
}
|
|
@@ -409,7 +409,7 @@ class TranscludeToken extends Token {
|
|
|
409
409
|
root = Parser.parse(wikitext, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
410
410
|
{childNodes: {length}, firstElementChild} = root;
|
|
411
411
|
if (length !== 1 || !firstElementChild?.matches('template#T')
|
|
412
|
-
|| firstElementChild.
|
|
412
|
+
|| firstElementChild.childNodes.length !== 2 || firstElementChild.lastElementChild.name !== key
|
|
413
413
|
) {
|
|
414
414
|
throw new SyntaxError(`非法的命名参数:${key}=${noWrap(value)}`);
|
|
415
415
|
}
|
|
@@ -436,7 +436,7 @@ class TranscludeToken extends Token {
|
|
|
436
436
|
}
|
|
437
437
|
const root = Parser.parse(`{{${title}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
438
438
|
{childNodes: {length}, firstElementChild} = root;
|
|
439
|
-
if (length !== 1 || firstElementChild?.type !== 'template' || firstElementChild.
|
|
439
|
+
if (length !== 1 || firstElementChild?.type !== 'template' || firstElementChild.childNodes.length !== 1) {
|
|
440
440
|
throw new SyntaxError(`非法的模板名称:${title}`);
|
|
441
441
|
}
|
|
442
442
|
this.firstElementChild.replaceChildren(...firstElementChild.firstElementChild.childNodes);
|
|
@@ -452,10 +452,10 @@ class TranscludeToken extends Token {
|
|
|
452
452
|
const root = Parser.parse(`{{#invoke:${title}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
453
453
|
{childNodes: {length}, firstElementChild} = root;
|
|
454
454
|
if (length !== 1 || !firstElementChild?.matches('magic-word#invoke')
|
|
455
|
-
|| firstElementChild.
|
|
455
|
+
|| firstElementChild.childNodes.length !== 2
|
|
456
456
|
) {
|
|
457
457
|
throw new SyntaxError(`非法的模块名称:${title}`);
|
|
458
|
-
} else if (this.
|
|
458
|
+
} else if (this.childNodes.length > 1) {
|
|
459
459
|
this.children[1].replaceChildren(...firstElementChild.lastElementChild.childNodes);
|
|
460
460
|
} else {
|
|
461
461
|
const {lastChild} = firstElementChild;
|
|
@@ -471,16 +471,16 @@ class TranscludeToken extends Token {
|
|
|
471
471
|
throw new Error(`${this.constructor.name}.replaceModule 方法仅用于更换模块!`);
|
|
472
472
|
} else if (typeof func !== 'string') {
|
|
473
473
|
this.typeError('replaceFunction', 'String');
|
|
474
|
-
} else if (this.
|
|
474
|
+
} else if (this.childNodes.length < 2) {
|
|
475
475
|
throw new Error('尚未指定模块名称!');
|
|
476
476
|
}
|
|
477
477
|
const root = Parser.parse(`{{#invoke:M|${func}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
478
478
|
{childNodes: {length}, firstElementChild} = root;
|
|
479
479
|
if (length !== 1 || !firstElementChild?.matches('magic-word#invoke')
|
|
480
|
-
|| firstElementChild.
|
|
480
|
+
|| firstElementChild.childNodes.length !== 3
|
|
481
481
|
) {
|
|
482
482
|
throw new SyntaxError(`非法的模块函数名:${func}`);
|
|
483
|
-
} else if (this.
|
|
483
|
+
} else if (this.childNodes.length > 2) {
|
|
484
484
|
this.children[2].replaceChildren(...firstElementChild.lastElementChild.childNodes);
|
|
485
485
|
} else {
|
|
486
486
|
const {lastChild} = firstElementChild;
|
package/typings/index.d.ts
CHANGED
package/typings/node.d.ts
CHANGED
|
@@ -6,8 +6,8 @@ declare global {
|
|
|
6
6
|
type TokenAttribute<T> =
|
|
7
7
|
T extends 'childNodes' ? (string|Token)[] :
|
|
8
8
|
T extends 'parentNode' ? Token|undefined :
|
|
9
|
-
T extends 'optional'|'tags' ? string[] :
|
|
10
|
-
T extends 'stage' ? number :
|
|
9
|
+
T extends 'optional'|'tags'|'flags' ? string[] :
|
|
10
|
+
T extends 'stage'|'indent' ? number :
|
|
11
11
|
T extends 'config' ? ParserConfig :
|
|
12
12
|
T extends 'accum' ? accum :
|
|
13
13
|
T extends 'acceptable' ? Record<string, Ranges> :
|
|
@@ -15,7 +15,7 @@ declare global {
|
|
|
15
15
|
T extends 'keys' ? Set<string> :
|
|
16
16
|
T extends 'args' ? Record<string, Set<ParameterToken>> :
|
|
17
17
|
T extends 'attr' ? Map<string, string|true> :
|
|
18
|
-
T extends 'include'|'selfLink' ? boolean :
|
|
18
|
+
T extends 'include'|'selfLink'|'ul'|'ol'|'dt'|'unidirectional'|'bidirectional' ? boolean :
|
|
19
19
|
T extends 'pattern' ? RegExp :
|
|
20
20
|
string;
|
|
21
21
|
}
|
package/typings/token.d.ts
CHANGED
package/util/string.js
CHANGED
|
@@ -57,7 +57,19 @@ const explode = (start, end, separator, str) => {
|
|
|
57
57
|
/** @param {string} str */
|
|
58
58
|
const noWrap = str => str.replaceAll('\n', '\\n');
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* @param {string|Token} token
|
|
62
|
+
* @returns {string}
|
|
63
|
+
*/
|
|
64
|
+
const normalizeSpace = (token = '', separator = '') => {
|
|
65
|
+
const Token = require('../src'); // eslint-disable-line no-unused-vars
|
|
66
|
+
return typeof token === 'string'
|
|
67
|
+
? token.replaceAll('\n', ' ')
|
|
68
|
+
: token.childNodes.map(child => typeof child === 'string' ? normalizeSpace(child) : child.toString())
|
|
69
|
+
.join(separator);
|
|
70
|
+
};
|
|
71
|
+
|
|
60
72
|
const extUrlChar = '(?:[\\d.]+|\\[[\\da-f:.]+\\]|[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd])'
|
|
61
73
|
+ '[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]*';
|
|
62
74
|
|
|
63
|
-
module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, noWrap, extUrlChar};
|
|
75
|
+
module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, noWrap, normalizeSpace, extUrlChar};
|
package/src/listToken.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const Token = require('.'),
|
|
3
|
-
AtomToken = require('./atom'),
|
|
4
|
-
{fixToken} = require('./util');
|
|
5
|
-
|
|
6
|
-
class ListToken extends fixToken(Token) {
|
|
7
|
-
type = 'list';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @param {string} syntax
|
|
11
|
-
* @param {?string|number|Token|(string|Token)[]} content
|
|
12
|
-
* @param {Object<string, any>} config
|
|
13
|
-
* @param {Token} parent
|
|
14
|
-
* @param {Token[]} accum
|
|
15
|
-
*/
|
|
16
|
-
constructor(syntax, content, config = require(Token.config), parent = null, accum = [], isTable = false) {
|
|
17
|
-
if (/[^:;#*]/.test(syntax)) {
|
|
18
|
-
throw new RangeError('List语法只接受":"、";"、"#"或"*"!');
|
|
19
|
-
}
|
|
20
|
-
super(new AtomToken(syntax, 'list-syntax'), config, true, parent, accum, ['AtomToken', 'Token']);
|
|
21
|
-
const inner = new Token(content, config, true, this, accum);
|
|
22
|
-
inner.type = 'list-inner';
|
|
23
|
-
inner.set('stage', isTable ? 4 : 10);
|
|
24
|
-
this.lists = new Set(syntax.split(''));
|
|
25
|
-
this.seal();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
isDt() {
|
|
29
|
-
return this.$children[0].contains(';');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
idDd() {
|
|
33
|
-
return this.$children[0].contains(':');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
isOl() {
|
|
37
|
-
return this.$children[0].contains('#');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
isUl() {
|
|
41
|
-
return this.$children[0].contains('*');
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
Token.classes.ListToken = ListToken;
|
|
46
|
-
|
|
47
|
-
module.exports = ListToken;
|