wikiparser-node 1.8.0-b → 1.8.0
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/config/.schema.json +181 -0
- package/config/enwiki.json +814 -1
- package/config/llwiki.json +35 -1
- package/config/moegirl.json +44 -1
- package/config/zhwiki.json +466 -1
- package/dist/addon/table.js +485 -0
- package/dist/addon/token.js +237 -0
- package/dist/addon/transclude.js +183 -0
- package/dist/base.d.ts +71 -0
- package/dist/base.js +38 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +211 -0
- package/dist/internal.d.ts +46 -0
- package/dist/lib/element.d.ts +140 -0
- package/dist/lib/element.js +597 -0
- package/dist/lib/node.d.ts +166 -0
- package/dist/lib/node.js +480 -0
- package/dist/lib/range.d.ts +105 -0
- package/dist/lib/range.js +406 -0
- package/dist/lib/ranges.d.ts +28 -0
- package/dist/lib/ranges.js +126 -0
- package/dist/lib/text.d.ts +63 -0
- package/dist/lib/text.js +355 -0
- package/dist/lib/title.d.ts +40 -0
- package/dist/lib/title.js +197 -0
- package/dist/mixin/attributesParent.d.ts +49 -0
- package/dist/mixin/attributesParent.js +81 -0
- package/dist/mixin/fixed.d.ts +5 -0
- package/dist/mixin/fixed.js +30 -0
- package/dist/mixin/flagsParent.d.ts +41 -0
- package/dist/mixin/flagsParent.js +59 -0
- package/dist/mixin/hidden.d.ts +4 -0
- package/dist/mixin/hidden.js +24 -0
- package/dist/mixin/magicLinkParent.d.ts +19 -0
- package/dist/mixin/magicLinkParent.js +42 -0
- package/dist/mixin/singleLine.d.ts +5 -0
- package/dist/mixin/singleLine.js +26 -0
- package/dist/mixin/sol.d.ts +5 -0
- package/dist/mixin/sol.js +43 -0
- package/dist/mixin/syntax.d.ts +8 -0
- package/dist/mixin/syntax.js +50 -0
- package/dist/parser/braces.js +130 -0
- package/dist/parser/commentAndExt.js +89 -0
- package/dist/parser/converter.js +40 -0
- package/dist/parser/externalLinks.js +44 -0
- package/dist/parser/hrAndDoubleUnderscore.js +40 -0
- package/dist/parser/html.js +38 -0
- package/dist/parser/links.js +99 -0
- package/dist/parser/list.js +97 -0
- package/dist/parser/magicLinks.js +42 -0
- package/dist/parser/quotes.js +67 -0
- package/dist/parser/redirect.js +26 -0
- package/dist/parser/selector.js +190 -0
- package/dist/parser/table.js +123 -0
- package/dist/src/arg.d.ts +58 -0
- package/dist/src/arg.js +214 -0
- package/dist/src/atom.d.ts +12 -0
- package/dist/src/atom.js +27 -0
- package/dist/src/attribute.d.ts +71 -0
- package/dist/src/attribute.js +502 -0
- package/dist/src/attributes.d.ts +109 -0
- package/dist/src/attributes.js +356 -0
- package/dist/src/converter.d.ts +33 -0
- package/dist/src/converter.js +134 -0
- package/dist/src/converterFlags.d.ts +87 -0
- package/dist/src/converterFlags.js +242 -0
- package/dist/src/converterRule.d.ts +77 -0
- package/dist/src/converterRule.js +209 -0
- package/dist/src/extLink.d.ts +43 -0
- package/dist/src/extLink.js +191 -0
- package/dist/src/gallery.d.ts +54 -0
- package/dist/src/gallery.js +139 -0
- package/dist/src/heading.d.ts +47 -0
- package/dist/src/heading.js +190 -0
- package/dist/src/hidden.d.ts +7 -0
- package/dist/src/hidden.js +23 -0
- package/dist/src/html.d.ts +65 -0
- package/dist/src/html.js +358 -0
- package/dist/src/imageParameter.d.ts +65 -0
- package/dist/src/imageParameter.js +257 -0
- package/dist/src/imagemap.d.ts +54 -0
- package/dist/src/imagemap.js +144 -0
- package/dist/src/imagemapLink.d.ts +31 -0
- package/dist/src/imagemapLink.js +93 -0
- package/dist/src/index.d.ts +142 -0
- package/dist/src/index.js +701 -0
- package/dist/src/link/base.d.ts +52 -0
- package/dist/src/link/base.js +228 -0
- package/dist/src/link/category.d.ts +22 -0
- package/dist/src/link/category.js +30 -0
- package/dist/src/link/file.d.ts +98 -0
- package/dist/src/link/file.js +272 -0
- package/dist/src/link/galleryImage.d.ts +32 -0
- package/dist/src/link/galleryImage.js +158 -0
- package/dist/src/link/index.d.ts +56 -0
- package/dist/src/link/index.js +133 -0
- package/dist/src/link/redirectTarget.d.ts +51 -0
- package/dist/src/link/redirectTarget.js +101 -0
- package/dist/src/magicLink.d.ts +59 -0
- package/dist/src/magicLink.js +220 -0
- package/dist/src/nested.d.ts +43 -0
- package/dist/src/nested.js +105 -0
- package/dist/src/nowiki/base.d.ts +31 -0
- package/dist/src/nowiki/base.js +93 -0
- package/dist/src/nowiki/comment.d.ts +25 -0
- package/dist/src/nowiki/comment.js +76 -0
- package/dist/src/nowiki/dd.d.ts +8 -0
- package/dist/src/nowiki/dd.js +24 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +18 -0
- package/dist/src/nowiki/doubleUnderscore.js +101 -0
- package/dist/src/nowiki/hr.d.ts +5 -0
- package/dist/src/nowiki/hr.js +63 -0
- package/dist/src/nowiki/index.d.ts +16 -0
- package/dist/src/nowiki/index.js +27 -0
- package/dist/src/nowiki/list.d.ts +16 -0
- package/dist/src/nowiki/list.js +109 -0
- package/dist/src/nowiki/listBase.d.ts +5 -0
- package/dist/src/nowiki/listBase.js +61 -0
- package/dist/src/nowiki/noinclude.d.ts +10 -0
- package/dist/src/nowiki/noinclude.js +23 -0
- package/dist/src/nowiki/quote.d.ts +19 -0
- package/dist/src/nowiki/quote.js +137 -0
- package/dist/src/onlyinclude.d.ts +16 -0
- package/dist/src/onlyinclude.js +57 -0
- package/dist/src/paramTag/index.d.ts +37 -0
- package/dist/src/paramTag/index.js +77 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/paramTag/inputbox.js +22 -0
- package/dist/src/parameter.d.ts +70 -0
- package/dist/src/parameter.js +264 -0
- package/dist/src/pre.d.ts +31 -0
- package/dist/src/pre.js +59 -0
- package/dist/src/redirect.d.ts +34 -0
- package/dist/src/redirect.js +119 -0
- package/dist/src/syntax.d.ts +19 -0
- package/dist/src/syntax.js +86 -0
- package/dist/src/table/base.d.ts +27 -0
- package/dist/src/table/base.js +81 -0
- package/dist/src/table/index.d.ts +242 -0
- package/dist/src/table/index.js +498 -0
- package/dist/src/table/td.d.ts +86 -0
- package/dist/src/table/td.js +355 -0
- package/dist/src/table/tr.d.ts +32 -0
- package/dist/src/table/tr.js +58 -0
- package/dist/src/table/trBase.d.ts +53 -0
- package/dist/src/table/trBase.js +156 -0
- package/dist/src/tagPair/ext.d.ts +32 -0
- package/dist/src/tagPair/ext.js +203 -0
- package/dist/src/tagPair/include.d.ts +35 -0
- package/dist/src/tagPair/include.js +74 -0
- package/dist/src/tagPair/index.d.ts +27 -0
- package/dist/src/tagPair/index.js +128 -0
- package/dist/src/transclude.d.ts +166 -0
- package/dist/src/transclude.js +584 -0
- package/dist/util/constants.js +27 -0
- package/dist/util/debug.js +81 -0
- package/dist/util/diff.js +83 -0
- package/dist/util/lint.js +31 -0
- package/dist/util/string.js +60 -0
- package/errors/README +1 -0
- package/package.json +17 -29
- package/printed/README +1 -0
- package/bundle/bundle.min.js +0 -38
- package/extensions/dist/base.js +0 -64
- package/extensions/dist/editor.js +0 -159
- package/extensions/dist/highlight.js +0 -58
- package/extensions/dist/lint.js +0 -72
- package/extensions/editor.css +0 -64
- package/extensions/ui.css +0 -144
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint @stylistic/operator-linebreak: [2, "before", {overrides: {"=": "after"}}] */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const constants_1 = require("../util/constants");
|
|
5
|
+
const debug_1 = require("../util/debug");
|
|
6
|
+
const index_1 = require("../src/index");
|
|
7
|
+
const comment_1 = require("../src/nowiki/comment");
|
|
8
|
+
const include_1 = require("../src/tagPair/include");
|
|
9
|
+
const ext_1 = require("../src/tagPair/ext");
|
|
10
|
+
const html_1 = require("../src/html");
|
|
11
|
+
const attributes_1 = require("../src/attributes");
|
|
12
|
+
index_1.Token.prototype.createComment =
|
|
13
|
+
/** @implements */
|
|
14
|
+
function (data = '') {
|
|
15
|
+
const config = this.getAttribute('config');
|
|
16
|
+
// @ts-expect-error abstract class
|
|
17
|
+
return debug_1.Shadow.run(() => new comment_1.CommentToken(data.replace(/-->/gu, '-->'), true, config));
|
|
18
|
+
};
|
|
19
|
+
index_1.Token.prototype.createElement =
|
|
20
|
+
/** @implements */
|
|
21
|
+
function (tagName, { selfClosing, closing } = {}) {
|
|
22
|
+
const config = this.getAttribute('config'), include = this.getAttribute('include');
|
|
23
|
+
if (tagName === (include ? 'noinclude' : 'includeonly')) {
|
|
24
|
+
return debug_1.Shadow.run(
|
|
25
|
+
// @ts-expect-error abstract class
|
|
26
|
+
() => new include_1.IncludeToken(tagName, '', undefined, selfClosing ? undefined : tagName, config));
|
|
27
|
+
}
|
|
28
|
+
else if (config.ext.includes(tagName)) {
|
|
29
|
+
// @ts-expect-error abstract class
|
|
30
|
+
return debug_1.Shadow.run(() => new ext_1.ExtToken(tagName, '', undefined, selfClosing ? undefined : '', config));
|
|
31
|
+
}
|
|
32
|
+
else if (config.html.flat().includes(tagName)) {
|
|
33
|
+
return debug_1.Shadow.run(() => {
|
|
34
|
+
// @ts-expect-error abstract class
|
|
35
|
+
const attr = new attributes_1.AttributesToken(undefined, 'html-attrs', tagName, config);
|
|
36
|
+
// @ts-expect-error abstract class
|
|
37
|
+
return new html_1.HtmlToken(tagName, attr, Boolean(closing), Boolean(selfClosing), config);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
throw new RangeError(`非法的标签名:${tagName}`);
|
|
41
|
+
};
|
|
42
|
+
index_1.Token.prototype.caretPositionFromIndex =
|
|
43
|
+
/** @implements */
|
|
44
|
+
function (index) {
|
|
45
|
+
if (index === undefined) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
const { length } = String(this);
|
|
49
|
+
if (index >= length || index < -length) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
index += index < 0 ? length : 0;
|
|
53
|
+
let self = this, acc = 0, start = 0;
|
|
54
|
+
while (self.type !== 'text') {
|
|
55
|
+
const { childNodes } = self;
|
|
56
|
+
acc += self.getAttribute('padding');
|
|
57
|
+
for (let i = 0; acc <= index && i < childNodes.length; i++) {
|
|
58
|
+
const cur = childNodes[i], l = String(cur).length;
|
|
59
|
+
acc += l;
|
|
60
|
+
if (acc > index) {
|
|
61
|
+
self = cur;
|
|
62
|
+
acc -= l;
|
|
63
|
+
start = acc;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
acc += self.getGaps(i);
|
|
67
|
+
}
|
|
68
|
+
if (self.childNodes === childNodes) {
|
|
69
|
+
return { offsetNode: self, offset: index - start };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { offsetNode: self, offset: index - start };
|
|
73
|
+
};
|
|
74
|
+
index_1.Token.prototype.sections =
|
|
75
|
+
/** @implements */
|
|
76
|
+
function () {
|
|
77
|
+
if (this.type !== 'root') {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const { childNodes, length } = this, headings = [...childNodes.entries()]
|
|
81
|
+
.filter((entry) => entry[1].type === 'heading')
|
|
82
|
+
.map(([i, { level }]) => [i, level]), lastHeading = [-1, -1, -1, -1, -1, -1], sections = headings.map(([i]) => {
|
|
83
|
+
const range = this.createRange();
|
|
84
|
+
range.setStart(this, i);
|
|
85
|
+
return range;
|
|
86
|
+
});
|
|
87
|
+
for (let i = 0; i < headings.length; i++) {
|
|
88
|
+
const [index, level] = headings[i];
|
|
89
|
+
for (let j = level; j < 6; j++) {
|
|
90
|
+
const last = lastHeading[j];
|
|
91
|
+
if (last >= 0) {
|
|
92
|
+
sections[last].setEnd(this, index);
|
|
93
|
+
}
|
|
94
|
+
lastHeading[j] = j === level ? i : -1;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for (const last of lastHeading) {
|
|
98
|
+
if (last >= 0) {
|
|
99
|
+
sections[last].setEnd(this, length);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const range = this.createRange();
|
|
103
|
+
range.setStart(this, 0);
|
|
104
|
+
range.setEnd(this, headings[0]?.[0] ?? length);
|
|
105
|
+
sections.unshift(range);
|
|
106
|
+
return sections;
|
|
107
|
+
};
|
|
108
|
+
index_1.Token.prototype.findEnclosingHtml =
|
|
109
|
+
/** @implements */
|
|
110
|
+
function (tag) {
|
|
111
|
+
tag = tag?.toLowerCase();
|
|
112
|
+
const { html } = this.getAttribute('config'), normalTags = new Set(html[0]), voidTags = new Set(html[2]);
|
|
113
|
+
if (tag !== undefined && !html.slice(0, 2).flat().includes(tag)) {
|
|
114
|
+
throw new RangeError(`非法的标签或空标签:${tag}`);
|
|
115
|
+
}
|
|
116
|
+
const { parentNode } = this;
|
|
117
|
+
if (!parentNode) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const isHtml = (0, debug_1.isToken)('html'),
|
|
121
|
+
/**
|
|
122
|
+
* 检查是否为指定的 HTML 标签
|
|
123
|
+
* @param node 节点
|
|
124
|
+
* @param name 标签名
|
|
125
|
+
* @param closing 是否为闭合标签
|
|
126
|
+
*/
|
|
127
|
+
checkHtml = (node, name, closing) => isHtml(node)
|
|
128
|
+
&& (!name && !voidTags.has(node.name) || node.name === name)
|
|
129
|
+
&& (normalTags.has(node.name) || !node.selfClosing)
|
|
130
|
+
&& node.closing === closing;
|
|
131
|
+
const { childNodes, length } = parentNode, index = childNodes.indexOf(this);
|
|
132
|
+
let i = index - 1, j = length;
|
|
133
|
+
for (; i >= 0; i--) {
|
|
134
|
+
const open = childNodes[i];
|
|
135
|
+
if (checkHtml(open, tag, false)) {
|
|
136
|
+
for (j = index + 1; j < length; j++) {
|
|
137
|
+
const close = childNodes[j];
|
|
138
|
+
if (checkHtml(close, open.name, true)) {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (j < length) {
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (i === -1) {
|
|
148
|
+
return parentNode.findEnclosingHtml(tag);
|
|
149
|
+
}
|
|
150
|
+
const range = this.createRange();
|
|
151
|
+
range.setStart(parentNode, i);
|
|
152
|
+
range.setEnd(parentNode, j + 1);
|
|
153
|
+
return range;
|
|
154
|
+
};
|
|
155
|
+
index_1.Token.prototype.redoQuotes =
|
|
156
|
+
/** @implements */
|
|
157
|
+
function () {
|
|
158
|
+
const acceptable = this.getAttribute('acceptable');
|
|
159
|
+
if (acceptable && !('QuoteToken' in acceptable)) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const accum = [];
|
|
163
|
+
for (const child of this.childNodes) {
|
|
164
|
+
if (child.type !== 'quote' && child.type !== 'text') {
|
|
165
|
+
child.replaceWith(`\0${accum.length}e\x7F`);
|
|
166
|
+
accum.push(child);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const token = debug_1.Shadow.run(() => {
|
|
170
|
+
const node = new index_1.Token(String(this), this.getAttribute('config'), accum);
|
|
171
|
+
node.type = 'plain';
|
|
172
|
+
node.setAttribute('stage', 6);
|
|
173
|
+
return node.parse(7);
|
|
174
|
+
});
|
|
175
|
+
this.replaceChildren(...token.childNodes);
|
|
176
|
+
};
|
|
177
|
+
index_1.Token.prototype.solveConst =
|
|
178
|
+
/** @implements */
|
|
179
|
+
function () {
|
|
180
|
+
const targets = this.querySelectorAll('magic-word, arg'), magicWords = new Set(['if', 'ifeq', 'switch']);
|
|
181
|
+
for (let i = targets.length - 1; i >= 0; i--) {
|
|
182
|
+
const target = targets[i], { type, name, childNodes, length } = target, [, var1, var2 = ''] = childNodes;
|
|
183
|
+
if (type === 'arg' || type === 'magic-word' && magicWords.has(name)) {
|
|
184
|
+
let replace = '';
|
|
185
|
+
if (type === 'arg') {
|
|
186
|
+
replace = target.default === false ? String(target) : target.default;
|
|
187
|
+
}
|
|
188
|
+
else if (name === 'if' && !var1?.getElementByTypes('magic-word, template')) {
|
|
189
|
+
replace = String(childNodes[String(var1 ?? '').trim() ? 2 : 3] ?? '').trim();
|
|
190
|
+
}
|
|
191
|
+
else if (name === 'ifeq'
|
|
192
|
+
&& !childNodes.slice(1, 3).some(child => child.getElementByTypes('magic-word, template'))) {
|
|
193
|
+
replace = String(childNodes[String(var1 ?? '').trim() === String(var2).trim() ? 3 : 4] ?? '').trim();
|
|
194
|
+
}
|
|
195
|
+
else if (name === 'switch' && !var1?.getElementByTypes('magic-word, template')) {
|
|
196
|
+
const key = String(var1 ?? '').trim();
|
|
197
|
+
let defaultVal = '', found = false, transclusion = false;
|
|
198
|
+
for (let j = 2; j < length; j++) {
|
|
199
|
+
const { anon, name: option, value, firstChild } = childNodes[j];
|
|
200
|
+
transclusion = Boolean(firstChild.getElementByTypes('magic-word, template'));
|
|
201
|
+
if (anon) {
|
|
202
|
+
if (j === length - 1) {
|
|
203
|
+
defaultVal = value;
|
|
204
|
+
}
|
|
205
|
+
else if (transclusion) {
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
found ||= key === value;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else if (transclusion) {
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
else if (found || option === key) {
|
|
216
|
+
replace = value;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
else if (option.toLowerCase() === '#default') {
|
|
220
|
+
defaultVal = value;
|
|
221
|
+
}
|
|
222
|
+
if (j === length - 1) {
|
|
223
|
+
replace = defaultVal;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (transclusion) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
target.replaceWith(replace);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
constants_1.classes['ExtendedTableToken'] = __filename;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint @stylistic/operator-linebreak: [2, "before", {overrides: {"=": "after"}}] */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const constants_1 = require("../util/constants");
|
|
5
|
+
const debug_1 = require("../util/debug");
|
|
6
|
+
const string_1 = require("../util/string");
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const index_2 = require("../src/index");
|
|
9
|
+
const transclude_1 = require("../src/transclude");
|
|
10
|
+
const parameter_1 = require("../src/parameter");
|
|
11
|
+
const atom_1 = require("../src/atom");
|
|
12
|
+
transclude_1.TranscludeToken.prototype.newAnonArg =
|
|
13
|
+
/** @implements */
|
|
14
|
+
function (val) {
|
|
15
|
+
const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(val, this.getAttribute('include'), undefined, config),
|
|
16
|
+
// @ts-expect-error abstract class
|
|
17
|
+
token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
|
|
18
|
+
token.lastChild.append(...childNodes);
|
|
19
|
+
token.afterBuild();
|
|
20
|
+
return this.insertAt(token);
|
|
21
|
+
};
|
|
22
|
+
transclude_1.TranscludeToken.prototype.setValue =
|
|
23
|
+
/** @implements */
|
|
24
|
+
function (key, value) {
|
|
25
|
+
if (!this.isTemplate()) {
|
|
26
|
+
throw new Error('setValue 方法仅供模板使用!');
|
|
27
|
+
}
|
|
28
|
+
const arg = this.getArg(key);
|
|
29
|
+
if (arg) {
|
|
30
|
+
arg.setValue(value);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const include = this.getAttribute('include'), config = this.getAttribute('config'), k = index_1.default.parse(key, include, undefined, config), v = index_1.default.parse(value, include, undefined, config),
|
|
34
|
+
// @ts-expect-error abstract class
|
|
35
|
+
token = debug_1.Shadow.run(() => new parameter_1.ParameterToken(undefined, undefined, config));
|
|
36
|
+
token.firstChild.append(...k.childNodes);
|
|
37
|
+
token.lastChild.append(...v.childNodes);
|
|
38
|
+
token.afterBuild();
|
|
39
|
+
this.insertAt(token);
|
|
40
|
+
};
|
|
41
|
+
transclude_1.TranscludeToken.prototype.replaceTemplate =
|
|
42
|
+
/** @implements */
|
|
43
|
+
function (title) {
|
|
44
|
+
if (this.type === 'magic-word') {
|
|
45
|
+
throw new Error('replaceTemplate 方法仅用于更换模板!');
|
|
46
|
+
}
|
|
47
|
+
const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, this.getAttribute('config'));
|
|
48
|
+
this.firstChild.replaceChildren(...childNodes);
|
|
49
|
+
};
|
|
50
|
+
transclude_1.TranscludeToken.prototype.replaceModule =
|
|
51
|
+
/** @implements */
|
|
52
|
+
function (title) {
|
|
53
|
+
if (this.type !== 'magic-word' || this.name !== 'invoke') {
|
|
54
|
+
throw new Error('replaceModule 方法仅用于更换模块!');
|
|
55
|
+
}
|
|
56
|
+
const config = this.getAttribute('config');
|
|
57
|
+
if (this.length === 1) {
|
|
58
|
+
index_2.Token.prototype.insertAt.call(this, new atom_1.AtomToken(undefined, 'invoke-module', config, [], {
|
|
59
|
+
'Stage-1': ':', '!ExtToken': '',
|
|
60
|
+
}));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const { childNodes } = index_1.default.parse(title, this.getAttribute('include'), 2, config);
|
|
64
|
+
this.childNodes[1].replaceChildren(...childNodes);
|
|
65
|
+
};
|
|
66
|
+
transclude_1.TranscludeToken.prototype.replaceFunction =
|
|
67
|
+
/** @implements */
|
|
68
|
+
function (func) {
|
|
69
|
+
if (this.type !== 'magic-word' || this.name !== 'invoke') {
|
|
70
|
+
throw new Error('replaceModule 方法仅用于更换模块!');
|
|
71
|
+
}
|
|
72
|
+
else if (this.length < 2) {
|
|
73
|
+
throw new Error('尚未指定模块名称!');
|
|
74
|
+
}
|
|
75
|
+
const config = this.getAttribute('config');
|
|
76
|
+
if (this.length === 2) {
|
|
77
|
+
index_2.Token.prototype.insertAt.call(this, new atom_1.AtomToken(undefined, 'invoke-function', config, [], {
|
|
78
|
+
'Stage-1': ':', '!ExtToken': '',
|
|
79
|
+
}));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const { childNodes } = index_1.default.parse(func, this.getAttribute('include'), 2, config);
|
|
83
|
+
this.childNodes[2].replaceChildren(...childNodes);
|
|
84
|
+
};
|
|
85
|
+
transclude_1.TranscludeToken.prototype.fixDuplication =
|
|
86
|
+
/** @implements */
|
|
87
|
+
function (aggressive) {
|
|
88
|
+
if (!this.hasDuplicatedArgs()) {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const duplicatedKeys = [];
|
|
92
|
+
let anonCount = this.getAnonArgs().length;
|
|
93
|
+
for (const [key, args] of this.getDuplicatedArgs()) {
|
|
94
|
+
if (args.length <= 1) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const values = new Map();
|
|
98
|
+
for (const arg of args) {
|
|
99
|
+
const val = arg.getValue().trim();
|
|
100
|
+
if (values.has(val)) {
|
|
101
|
+
values.get(val).push(arg);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
values.set(val, [arg]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
let noMoreAnon = anonCount === 0 || !key.trim() || isNaN(key);
|
|
108
|
+
const emptyArgs = values.get('') ?? [], duplicatedArgs = [...values].filter(([val, { length }]) => val && length > 1).flatMap(([, curArgs]) => {
|
|
109
|
+
const anonIndex = noMoreAnon ? -1 : curArgs.findIndex(({ anon }) => anon);
|
|
110
|
+
if (anonIndex !== -1) {
|
|
111
|
+
noMoreAnon = true;
|
|
112
|
+
}
|
|
113
|
+
curArgs.splice(anonIndex, 1);
|
|
114
|
+
return curArgs;
|
|
115
|
+
}), badArgs = [...emptyArgs, ...duplicatedArgs], index = noMoreAnon ? -1 : emptyArgs.findIndex(({ anon }) => anon);
|
|
116
|
+
if (badArgs.length === args.length) {
|
|
117
|
+
badArgs.splice(index, 1);
|
|
118
|
+
}
|
|
119
|
+
else if (index !== -1) {
|
|
120
|
+
this.anonToNamed();
|
|
121
|
+
anonCount = 0;
|
|
122
|
+
}
|
|
123
|
+
for (const arg of badArgs) {
|
|
124
|
+
arg.remove();
|
|
125
|
+
}
|
|
126
|
+
let remaining = args.length - badArgs.length;
|
|
127
|
+
if (remaining === 1) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
else if (aggressive && (anonCount ? /\D\d+$/u : /(?:^|\D)\d+$/u).test(key)) {
|
|
131
|
+
let last;
|
|
132
|
+
// eslint-disable-next-line es-x/no-regexp-lookbehind-assertions
|
|
133
|
+
const str = key.slice(0, -/(?<!\d)\d+$/u.exec(key)[0].length), regex = new RegExp(`^${(0, string_1.escapeRegExp)(str)}\\d+$`, 'u'), series = this.getAllArgs().filter(({ name }) => regex.test(name)), ordered = series.every(({ name }, i) => {
|
|
134
|
+
const j = Number(name.slice(str.length)), cmp = j <= i + 1 && (i === 0 || j >= last || name === key);
|
|
135
|
+
last = j;
|
|
136
|
+
return cmp;
|
|
137
|
+
});
|
|
138
|
+
if (ordered) {
|
|
139
|
+
for (let i = 0; i < series.length; i++) {
|
|
140
|
+
const name = `${str}${i + 1}`, arg = series[i];
|
|
141
|
+
if (arg.name !== name) {
|
|
142
|
+
if (arg.name === key) {
|
|
143
|
+
remaining--;
|
|
144
|
+
}
|
|
145
|
+
arg.rename(name, true);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (remaining > 1) {
|
|
151
|
+
index_1.default.error(`${this.type === 'template'
|
|
152
|
+
? this.name
|
|
153
|
+
: this.normalizeTitle(this.childNodes[1].text(), 828)
|
|
154
|
+
.title} 还留有 ${remaining} 个重复的 ${key} 参数:${[...this.getArgs(key)].map(arg => {
|
|
155
|
+
const { top, left } = arg.getBoundingClientRect();
|
|
156
|
+
return `第 ${String(top)} 行第 ${String(left)} 列`;
|
|
157
|
+
}).join('、')}`);
|
|
158
|
+
duplicatedKeys.push(key);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return duplicatedKeys;
|
|
163
|
+
};
|
|
164
|
+
transclude_1.TranscludeToken.prototype.escapeTables =
|
|
165
|
+
/** @implements */
|
|
166
|
+
function () {
|
|
167
|
+
if (!/\n[^\S\n]*(?::+[^\S\n]*)?\{\|/u.test(this.text())) {
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
const stripped = String(this).slice(2, -2), include = this.getAttribute('include'), config = this.getAttribute('config'), parsed = index_1.default.parse(stripped, include, 4, config), isTable = (0, debug_1.isToken)('table');
|
|
171
|
+
for (const table of parsed.childNodes) {
|
|
172
|
+
if (isTable(table)) {
|
|
173
|
+
table.escape();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const { firstChild, length } = index_1.default.parse(`{{${String(parsed)}}}`, include, undefined, config);
|
|
177
|
+
if (length !== 1 || !(firstChild instanceof transclude_1.TranscludeToken)) {
|
|
178
|
+
throw new Error('转义表格失败!');
|
|
179
|
+
}
|
|
180
|
+
this.safeReplaceWith(firstChild);
|
|
181
|
+
return firstChild;
|
|
182
|
+
};
|
|
183
|
+
constants_1.classes['ExtendedTranscludeToken'] = __filename;
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export interface Config {
|
|
2
|
+
ext: string[];
|
|
3
|
+
readonly html: [string[], string[], string[]];
|
|
4
|
+
readonly namespaces: Record<string, string>;
|
|
5
|
+
readonly nsid: Record<string, number>;
|
|
6
|
+
readonly parserFunction: [Record<string, string>, string[], string[], string[]];
|
|
7
|
+
readonly doubleUnderscore: [string[], string[]];
|
|
8
|
+
readonly protocol: string;
|
|
9
|
+
readonly img: Record<string, string>;
|
|
10
|
+
readonly redirection: string[];
|
|
11
|
+
readonly variants: string[];
|
|
12
|
+
readonly excludes?: string[];
|
|
13
|
+
readonly interwiki: string[];
|
|
14
|
+
readonly conversionTable?: [string, string][];
|
|
15
|
+
readonly redirects?: [string, string][];
|
|
16
|
+
}
|
|
17
|
+
export type TokenTypes = 'root' | 'plain' | 'redirect' | 'redirect-syntax' | 'redirect-target' | 'onlyinclude' | 'noinclude' | 'include' | 'comment' | 'ext' | 'ext-attrs' | 'ext-attr-dirty' | 'ext-attr' | 'attr-key' | 'attr-value' | 'ext-inner' | 'arg' | 'arg-name' | 'arg-default' | 'hidden' | 'magic-word' | 'magic-word-name' | 'invoke-function' | 'invoke-module' | 'template' | 'template-name' | 'parameter' | 'parameter-key' | 'parameter-value' | 'heading' | 'heading-title' | 'heading-trail' | 'html' | 'html-attrs' | 'html-attr-dirty' | 'html-attr' | 'table' | 'tr' | 'td' | 'table-syntax' | 'table-attrs' | 'table-attr-dirty' | 'table-attr' | 'table-inter' | 'td-inner' | 'hr' | 'double-underscore' | 'link' | 'link-target' | 'link-text' | 'category' | 'file' | 'gallery-image' | 'imagemap-image' | 'image-parameter' | 'quote' | 'ext-link' | 'ext-link-text' | 'ext-link-url' | 'free-ext-link' | 'list' | 'dd' | 'converter' | 'converter-flags' | 'converter-flag' | 'converter-rule' | 'converter-rule-variant' | 'converter-rule-to' | 'converter-rule-from' | 'param-line' | 'imagemap-link';
|
|
18
|
+
export declare const rules: readonly ["bold-header", "format-leakage", "fostered-content", "h1", "illegal-attr", "insecure-style", "invalid-gallery", "invalid-imagemap", "invalid-invoke", "lonely-apos", "lonely-bracket", "lonely-http", "nested-link", "no-arg", "no-duplicate", "no-ignored", "obsolete-attr", "obsolete-tag", "parsing-order", "pipe-like", "table-layout", "tag-like", "unbalanced-header", "unclosed-comment", "unclosed-quote", "unclosed-table", "unescaped", "unknown-page", "unmatched-tag", "unterminated-url", "url-encoding", "var-anchor", "void-ext"];
|
|
19
|
+
export declare namespace LintError {
|
|
20
|
+
type Severity = 'error' | 'warning';
|
|
21
|
+
type Rule = typeof rules[number];
|
|
22
|
+
interface Fix {
|
|
23
|
+
readonly range: [number, number];
|
|
24
|
+
text: string;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export interface LintError {
|
|
28
|
+
rule: LintError.Rule;
|
|
29
|
+
message: string;
|
|
30
|
+
severity: LintError.Severity;
|
|
31
|
+
startIndex: number;
|
|
32
|
+
endIndex: number;
|
|
33
|
+
startLine: number;
|
|
34
|
+
startCol: number;
|
|
35
|
+
endLine: number;
|
|
36
|
+
endCol: number;
|
|
37
|
+
fix?: LintError.Fix;
|
|
38
|
+
suggestions?: (LintError.Fix & {
|
|
39
|
+
desc: string;
|
|
40
|
+
})[];
|
|
41
|
+
}
|
|
42
|
+
export type AST = Record<string, string | number | boolean> & {
|
|
43
|
+
range: [number, number];
|
|
44
|
+
type?: TokenTypes;
|
|
45
|
+
childNodes?: AST[];
|
|
46
|
+
};
|
|
47
|
+
/** 类似Node */
|
|
48
|
+
export interface AstNode {
|
|
49
|
+
type: string;
|
|
50
|
+
readonly childNodes: readonly AstNode[];
|
|
51
|
+
/** Linter */
|
|
52
|
+
lint(): LintError[];
|
|
53
|
+
/** 以HTML格式打印 */
|
|
54
|
+
print(): string;
|
|
55
|
+
}
|
|
56
|
+
/** 类似HTMLElement */
|
|
57
|
+
interface AstElement extends AstNode {
|
|
58
|
+
/** 保存为JSON */
|
|
59
|
+
json(): AST;
|
|
60
|
+
}
|
|
61
|
+
export interface Parser {
|
|
62
|
+
config: Config | string;
|
|
63
|
+
i18n: Record<string, string> | string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* 解析wikitext
|
|
66
|
+
* @param include 是否嵌入
|
|
67
|
+
* @param maxStage 最大解析层级
|
|
68
|
+
*/
|
|
69
|
+
parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): AstElement;
|
|
70
|
+
}
|
|
71
|
+
export {};
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rules = void 0;
|
|
4
|
+
exports.rules = [
|
|
5
|
+
'bold-header',
|
|
6
|
+
'format-leakage',
|
|
7
|
+
'fostered-content',
|
|
8
|
+
'h1',
|
|
9
|
+
'illegal-attr',
|
|
10
|
+
'insecure-style',
|
|
11
|
+
'invalid-gallery',
|
|
12
|
+
'invalid-imagemap',
|
|
13
|
+
'invalid-invoke',
|
|
14
|
+
'lonely-apos',
|
|
15
|
+
'lonely-bracket',
|
|
16
|
+
'lonely-http',
|
|
17
|
+
'nested-link',
|
|
18
|
+
'no-arg',
|
|
19
|
+
'no-duplicate',
|
|
20
|
+
'no-ignored',
|
|
21
|
+
'obsolete-attr',
|
|
22
|
+
'obsolete-tag',
|
|
23
|
+
'parsing-order',
|
|
24
|
+
'pipe-like',
|
|
25
|
+
'table-layout',
|
|
26
|
+
'tag-like',
|
|
27
|
+
'unbalanced-header',
|
|
28
|
+
'unclosed-comment',
|
|
29
|
+
'unclosed-quote',
|
|
30
|
+
'unclosed-table',
|
|
31
|
+
'unescaped',
|
|
32
|
+
'unknown-page',
|
|
33
|
+
'unmatched-tag',
|
|
34
|
+
'unterminated-url',
|
|
35
|
+
'url-encoding',
|
|
36
|
+
'var-anchor',
|
|
37
|
+
'void-ext',
|
|
38
|
+
];
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Config, LintError, Parser as ParserBase } from './base';
|
|
2
|
+
import type { Title } from './lib/title';
|
|
3
|
+
import type { Token } from './internal';
|
|
4
|
+
declare interface Parser extends ParserBase {
|
|
5
|
+
rules: readonly LintError.Rule[];
|
|
6
|
+
conversionTable: Map<string, string>;
|
|
7
|
+
redirects: Map<string, string>;
|
|
8
|
+
warning: boolean;
|
|
9
|
+
debugging: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 规范化页面标题
|
|
12
|
+
* @param title 标题(含或不含命名空间前缀)
|
|
13
|
+
* @param defaultNs 命名空间
|
|
14
|
+
* @param include 是否嵌入
|
|
15
|
+
* @param halfParsed 是否是半解析状态
|
|
16
|
+
* @param decode 是否需要解码
|
|
17
|
+
* @param selfLink 是否允许selfLink
|
|
18
|
+
*/
|
|
19
|
+
normalizeTitle(title: string, defaultNs?: number, include?: boolean, config?: Config, halfParsed?: boolean, decode?: boolean, selfLink?: boolean): Title;
|
|
20
|
+
parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): Token;
|
|
21
|
+
/**
|
|
22
|
+
* 是否是跨维基链接
|
|
23
|
+
* @param title 链接标题
|
|
24
|
+
*/
|
|
25
|
+
isInterwiki(title: string, config?: Config): RegExpExecArray | null;
|
|
26
|
+
}
|
|
27
|
+
declare const Parser: Parser;
|
|
28
|
+
// @ts-expect-error mixed export styles
|
|
29
|
+
export = Parser;
|
|
30
|
+
export type { Config, LintError };
|
|
31
|
+
export type * from './internal';
|
|
32
|
+
declare global {
|
|
33
|
+
type Acceptable = unknown;
|
|
34
|
+
type PrintOpt = unknown;
|
|
35
|
+
}
|