wikiparser-node 0.1.0 → 0.2.2
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/index.js +42 -0
- package/lib/element.js +2 -1
- package/mixin/sol.js +8 -4
- package/package.json +2 -2
- package/parser/converter.js +44 -0
- package/parser/table.js +7 -3
- package/src/arg.js +6 -6
- package/src/attribute.js +22 -29
- package/src/converter.js +135 -0
- package/src/converterFlags.js +215 -0
- package/src/converterRule.js +210 -0
- package/src/extLink.js +12 -12
- package/src/imageParameter.js +1 -1
- package/src/index.js +19 -16
- package/src/link/category.js +1 -0
- package/src/link/file.js +2 -2
- package/src/link/index.js +7 -7
- package/src/magicLink.js +5 -1
- package/src/nowiki/dd.js +4 -4
- package/src/parameter.js +2 -2
- package/src/syntax.js +3 -1
- package/src/table/td.js +2 -1
- package/src/table/tr.js +1 -1
- package/src/tagPair/index.js +1 -1
- package/src/transclude.js +14 -14
- package/typings/index.d.ts +2 -0
- package/typings/node.d.ts +2 -2
- package/typings/token.d.ts +1 -0
- package/util/string.js +14 -2
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/magicLink.js
CHANGED
|
@@ -42,10 +42,14 @@ class MagicLinkToken extends Token {
|
|
|
42
42
|
if (parameter?.getValue() === this.text()) {
|
|
43
43
|
this.replaceWith(this.toString());
|
|
44
44
|
}
|
|
45
|
+
return this;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
getUrl() {
|
|
48
|
-
|
|
49
|
+
let url = this.text();
|
|
50
|
+
if (url.startsWith('//')) {
|
|
51
|
+
url = `https:${url}`;
|
|
52
|
+
}
|
|
49
53
|
try {
|
|
50
54
|
return new URL(url);
|
|
51
55
|
} catch (e) {
|
package/src/nowiki/dd.js
CHANGED
|
@@ -9,10 +9,10 @@ const /** @type {Parser} */ Parser = require('../..'),
|
|
|
9
9
|
*/
|
|
10
10
|
class DdToken extends NowikiToken {
|
|
11
11
|
type = 'dd';
|
|
12
|
-
dt;
|
|
13
|
-
ul;
|
|
14
|
-
ol;
|
|
15
|
-
indent;
|
|
12
|
+
dt = false;
|
|
13
|
+
ul = false;
|
|
14
|
+
ol = false;
|
|
15
|
+
indent = 0;
|
|
16
16
|
|
|
17
17
|
/** @param {string} str */
|
|
18
18
|
#update(str) {
|
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/td.js
CHANGED
|
@@ -152,7 +152,7 @@ class TdToken extends fixedToken(TrToken) {
|
|
|
152
152
|
return this;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
|
|
155
|
+
afterBuild() {
|
|
156
156
|
if (this.#innerSyntax.includes('\x00')) {
|
|
157
157
|
this.#innerSyntax = this.buildFromStr(this.#innerSyntax).map(String).join('');
|
|
158
158
|
}
|
|
@@ -196,6 +196,7 @@ class TdToken extends fixedToken(TrToken) {
|
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
getGaps(i = 0) {
|
|
199
|
+
i = i < 0 ? i + this.childNodes.length : i;
|
|
199
200
|
if (i !== 1) {
|
|
200
201
|
return 0;
|
|
201
202
|
}
|
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);
|
package/src/tagPair/index.js
CHANGED
|
@@ -57,7 +57,7 @@ class TagPairToken extends fixedToken(Token) {
|
|
|
57
57
|
}
|
|
58
58
|
return selfClosing
|
|
59
59
|
? `<${opening}${String(firstChild)}/>`
|
|
60
|
-
: `<${opening}${String(firstChild)}>${String(lastChild)}${closed ? `</${closing}>` : ''}`;
|
|
60
|
+
: `<${opening}${String(firstChild)}>${String(lastChild)}${this.closed ? `</${closing}>` : ''}`;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
getPadding() {
|
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,7 +6,7 @@ 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[] :
|
|
9
|
+
T extends 'optional'|'tags'|'flags' ? string[] :
|
|
10
10
|
T extends 'stage'|'indent' ? number :
|
|
11
11
|
T extends 'config' ? ParserConfig :
|
|
12
12
|
T extends 'accum' ? accum :
|
|
@@ -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'|'ul'|'ol'|'dt' ? 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
|
-
+ '[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]*';
|
|
73
|
+
+ '(?:[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]|\\x00\\d+c\\x7f)*';
|
|
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};
|