wikiparser-node 1.9.1-b → 1.9.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.
Files changed (171) hide show
  1. package/config/.schema.json +181 -0
  2. package/config/enwiki.json +814 -1
  3. package/config/llwiki.json +35 -1
  4. package/config/moegirl.json +44 -1
  5. package/config/zhwiki.json +466 -1
  6. package/dist/addon/table.js +486 -0
  7. package/dist/addon/token.js +239 -0
  8. package/dist/addon/transclude.js +185 -0
  9. package/dist/base.d.ts +73 -0
  10. package/dist/base.js +39 -0
  11. package/dist/index.d.ts +31 -0
  12. package/dist/index.js +221 -0
  13. package/dist/internal.d.ts +46 -0
  14. package/dist/lib/element.d.ts +124 -0
  15. package/dist/lib/element.js +600 -0
  16. package/dist/lib/node.d.ts +166 -0
  17. package/dist/lib/node.js +480 -0
  18. package/dist/lib/range.d.ts +105 -0
  19. package/dist/lib/range.js +406 -0
  20. package/dist/lib/ranges.d.ts +28 -0
  21. package/dist/lib/ranges.js +126 -0
  22. package/dist/lib/rect.d.ts +18 -0
  23. package/dist/lib/rect.js +34 -0
  24. package/dist/lib/text.d.ts +54 -0
  25. package/dist/lib/text.js +345 -0
  26. package/dist/lib/title.d.ts +40 -0
  27. package/dist/lib/title.js +197 -0
  28. package/dist/mixin/attributesParent.d.ts +49 -0
  29. package/dist/mixin/attributesParent.js +82 -0
  30. package/dist/mixin/fixed.d.ts +5 -0
  31. package/dist/mixin/fixed.js +32 -0
  32. package/dist/mixin/flagsParent.d.ts +41 -0
  33. package/dist/mixin/flagsParent.js +60 -0
  34. package/dist/mixin/hidden.d.ts +6 -0
  35. package/dist/mixin/hidden.js +29 -0
  36. package/dist/mixin/magicLinkParent.d.ts +19 -0
  37. package/dist/mixin/magicLinkParent.js +43 -0
  38. package/dist/mixin/singleLine.d.ts +5 -0
  39. package/dist/mixin/singleLine.js +27 -0
  40. package/dist/mixin/sol.d.ts +5 -0
  41. package/dist/mixin/sol.js +44 -0
  42. package/dist/mixin/syntax.d.ts +8 -0
  43. package/dist/mixin/syntax.js +47 -0
  44. package/dist/parser/braces.js +146 -0
  45. package/dist/parser/commentAndExt.js +79 -0
  46. package/dist/parser/converter.js +39 -0
  47. package/dist/parser/externalLinks.js +37 -0
  48. package/dist/parser/hrAndDoubleUnderscore.js +42 -0
  49. package/dist/parser/html.js +38 -0
  50. package/dist/parser/links.js +101 -0
  51. package/dist/parser/list.js +97 -0
  52. package/dist/parser/magicLinks.js +53 -0
  53. package/dist/parser/quotes.js +67 -0
  54. package/dist/parser/redirect.js +26 -0
  55. package/dist/parser/selector.js +190 -0
  56. package/dist/parser/table.js +123 -0
  57. package/dist/src/arg.d.ts +52 -0
  58. package/dist/src/arg.js +213 -0
  59. package/dist/src/atom.d.ts +12 -0
  60. package/dist/src/atom.js +26 -0
  61. package/dist/src/attribute.d.ts +65 -0
  62. package/dist/src/attribute.js +490 -0
  63. package/dist/src/attributes.d.ts +103 -0
  64. package/dist/src/attributes.js +350 -0
  65. package/dist/src/converter.d.ts +29 -0
  66. package/dist/src/converter.js +134 -0
  67. package/dist/src/converterFlags.d.ts +81 -0
  68. package/dist/src/converterFlags.js +243 -0
  69. package/dist/src/converterRule.d.ts +73 -0
  70. package/dist/src/converterRule.js +210 -0
  71. package/dist/src/extLink.d.ts +37 -0
  72. package/dist/src/extLink.js +191 -0
  73. package/dist/src/gallery.d.ts +48 -0
  74. package/dist/src/gallery.js +139 -0
  75. package/dist/src/heading.d.ts +41 -0
  76. package/dist/src/heading.js +185 -0
  77. package/dist/src/hidden.d.ts +10 -0
  78. package/dist/src/hidden.js +79 -0
  79. package/dist/src/html.d.ts +58 -0
  80. package/dist/src/html.js +339 -0
  81. package/dist/src/imageParameter.d.ts +59 -0
  82. package/dist/src/imageParameter.js +261 -0
  83. package/dist/src/imagemap.d.ts +48 -0
  84. package/dist/src/imagemap.js +145 -0
  85. package/dist/src/imagemapLink.d.ts +31 -0
  86. package/dist/src/imagemapLink.js +93 -0
  87. package/dist/src/index.d.ts +130 -0
  88. package/dist/src/index.js +717 -0
  89. package/dist/src/link/base.d.ts +46 -0
  90. package/dist/src/link/base.js +224 -0
  91. package/dist/src/link/category.d.ts +22 -0
  92. package/dist/src/link/category.js +30 -0
  93. package/dist/src/link/file.d.ts +96 -0
  94. package/dist/src/link/file.js +274 -0
  95. package/dist/src/link/galleryImage.d.ts +30 -0
  96. package/dist/src/link/galleryImage.js +157 -0
  97. package/dist/src/link/index.d.ts +52 -0
  98. package/dist/src/link/index.js +133 -0
  99. package/dist/src/link/redirectTarget.d.ts +37 -0
  100. package/dist/src/link/redirectTarget.js +95 -0
  101. package/dist/src/magicLink.d.ts +60 -0
  102. package/dist/src/magicLink.js +271 -0
  103. package/dist/src/nested.d.ts +41 -0
  104. package/dist/src/nested.js +105 -0
  105. package/dist/src/nowiki/base.d.ts +31 -0
  106. package/dist/src/nowiki/base.js +92 -0
  107. package/dist/src/nowiki/comment.d.ts +15 -0
  108. package/dist/src/nowiki/comment.js +123 -0
  109. package/dist/src/nowiki/dd.d.ts +8 -0
  110. package/dist/src/nowiki/dd.js +24 -0
  111. package/dist/src/nowiki/doubleUnderscore.d.ts +16 -0
  112. package/dist/src/nowiki/doubleUnderscore.js +100 -0
  113. package/dist/src/nowiki/hr.d.ts +5 -0
  114. package/dist/src/nowiki/hr.js +63 -0
  115. package/dist/src/nowiki/index.d.ts +14 -0
  116. package/dist/src/nowiki/index.js +29 -0
  117. package/dist/src/nowiki/list.d.ts +16 -0
  118. package/dist/src/nowiki/list.js +109 -0
  119. package/dist/src/nowiki/listBase.d.ts +5 -0
  120. package/dist/src/nowiki/listBase.js +61 -0
  121. package/dist/src/nowiki/noinclude.d.ts +10 -0
  122. package/dist/src/nowiki/noinclude.js +73 -0
  123. package/dist/src/nowiki/quote.d.ts +17 -0
  124. package/dist/src/nowiki/quote.js +131 -0
  125. package/dist/src/onlyinclude.d.ts +14 -0
  126. package/dist/src/onlyinclude.js +57 -0
  127. package/dist/src/paramTag/index.d.ts +31 -0
  128. package/dist/src/paramTag/index.js +77 -0
  129. package/dist/src/paramTag/inputbox.d.ts +8 -0
  130. package/dist/src/paramTag/inputbox.js +22 -0
  131. package/dist/src/parameter.d.ts +66 -0
  132. package/dist/src/parameter.js +265 -0
  133. package/dist/src/pre.d.ts +29 -0
  134. package/dist/src/pre.js +59 -0
  135. package/dist/src/redirect.d.ts +31 -0
  136. package/dist/src/redirect.js +121 -0
  137. package/dist/src/syntax.d.ts +17 -0
  138. package/dist/src/syntax.js +85 -0
  139. package/dist/src/table/base.d.ts +27 -0
  140. package/dist/src/table/base.js +81 -0
  141. package/dist/src/table/index.d.ts +239 -0
  142. package/dist/src/table/index.js +499 -0
  143. package/dist/src/table/td.d.ts +78 -0
  144. package/dist/src/table/td.js +368 -0
  145. package/dist/src/table/tr.d.ts +30 -0
  146. package/dist/src/table/tr.js +57 -0
  147. package/dist/src/table/trBase.d.ts +49 -0
  148. package/dist/src/table/trBase.js +156 -0
  149. package/dist/src/tagPair/ext.d.ts +30 -0
  150. package/dist/src/tagPair/ext.js +201 -0
  151. package/dist/src/tagPair/include.d.ts +35 -0
  152. package/dist/src/tagPair/include.js +124 -0
  153. package/dist/src/tagPair/index.d.ts +23 -0
  154. package/dist/src/tagPair/index.js +128 -0
  155. package/dist/src/transclude.d.ts +160 -0
  156. package/dist/src/transclude.js +582 -0
  157. package/dist/util/constants.js +27 -0
  158. package/dist/util/debug.js +90 -0
  159. package/dist/util/diff.js +83 -0
  160. package/dist/util/lint.js +32 -0
  161. package/dist/util/string.js +60 -0
  162. package/errors/README +1 -0
  163. package/package.json +20 -30
  164. package/printed/README +1 -0
  165. package/bundle/bundle.min.js +0 -38
  166. package/extensions/dist/base.js +0 -64
  167. package/extensions/dist/editor.js +0 -159
  168. package/extensions/dist/highlight.js +0 -58
  169. package/extensions/dist/lint.js +0 -72
  170. package/extensions/editor.css +0 -64
  171. package/extensions/ui.css +0 -144
@@ -0,0 +1,239 @@
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.some(tags => tags.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(`Invalid tag name: ${tagName}`);
41
+ };
42
+ index_1.Token.prototype.caretPositionFromIndex =
43
+ /** @implements */
44
+ function (index) {
45
+ if (index === undefined) {
46
+ return undefined;
47
+ }
48
+ const { length } = this.toString();
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 = cur.toString().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 (html[2].includes(tag)) {
114
+ throw new RangeError(`Void tag: ${tag}`);
115
+ }
116
+ else if (tag !== undefined && !html.slice(0, 2).some(tags => tags.includes(tag))) {
117
+ throw new RangeError(`Invalid tag name: ${tag}`);
118
+ }
119
+ const { parentNode } = this;
120
+ if (!parentNode) {
121
+ return undefined;
122
+ }
123
+ const isHtml = (0, debug_1.isToken)('html'),
124
+ /**
125
+ * 检查是否为指定的 HTML 标签
126
+ * @param node 节点
127
+ * @param name 标签名
128
+ * @param closing 是否为闭合标签
129
+ */
130
+ checkHtml = (node, name, closing) => isHtml(node)
131
+ && (!name && !voidTags.has(node.name) || node.name === name)
132
+ && (normalTags.has(node.name) || !node.selfClosing)
133
+ && node.closing === closing;
134
+ const { childNodes, length } = parentNode, index = childNodes.indexOf(this);
135
+ let i = index - 1, j = length;
136
+ for (; i >= 0; i--) {
137
+ const open = childNodes[i];
138
+ if (checkHtml(open, tag, false)) {
139
+ for (j = index + 1; j < length; j++) {
140
+ const close = childNodes[j];
141
+ if (checkHtml(close, open.name, true)) {
142
+ break;
143
+ }
144
+ }
145
+ if (j < length) {
146
+ break;
147
+ }
148
+ }
149
+ }
150
+ if (i === -1) {
151
+ return parentNode.findEnclosingHtml(tag);
152
+ }
153
+ const range = this.createRange();
154
+ range.setStart(parentNode, i);
155
+ range.setEnd(parentNode, j + 1);
156
+ return range;
157
+ };
158
+ index_1.Token.prototype.redoQuotes =
159
+ /** @implements */
160
+ function () {
161
+ const acceptable = this.getAcceptable();
162
+ if (acceptable && !('QuoteToken' in acceptable)) {
163
+ return;
164
+ }
165
+ const accum = [];
166
+ for (const child of this.childNodes) {
167
+ if (child.type !== 'quote' && child.type !== 'text') {
168
+ child.replaceWith(`\0${accum.length}e\x7F`);
169
+ accum.push(child);
170
+ }
171
+ }
172
+ const token = debug_1.Shadow.run(() => {
173
+ const node = new index_1.Token(this.toString(), this.getAttribute('config'), accum);
174
+ node.setAttribute('stage', 6);
175
+ return node.parse(7);
176
+ });
177
+ this.replaceChildren(...token.childNodes);
178
+ };
179
+ index_1.Token.prototype.solveConst =
180
+ /** @implements */
181
+ function () {
182
+ const targets = this.querySelectorAll('magic-word, arg'), magicWords = new Set(['if', 'ifeq', 'switch']);
183
+ for (let i = targets.length - 1; i >= 0; i--) {
184
+ const target = targets[i], { type, name, childNodes, length } = target, [, var1, var2 = ''] = childNodes;
185
+ if (type === 'arg' || type === 'magic-word' && magicWords.has(name)) {
186
+ let replace = '';
187
+ if (type === 'arg') {
188
+ replace = target.default === false ? target.toString() : target.default;
189
+ }
190
+ else if (name === 'if' && !var1?.getElementByTypes('magic-word, template')) {
191
+ replace = String(childNodes[String(var1 ?? '').trim() ? 2 : 3] ?? '').trim();
192
+ }
193
+ else if (name === 'ifeq'
194
+ && !childNodes.slice(1, 3).some(child => child.getElementByTypes('magic-word, template'))) {
195
+ replace = String(childNodes[String(var1 ?? '').trim() === String(var2).trim() ? 3 : 4] ?? '').trim();
196
+ }
197
+ else if (name === 'switch' && !var1?.getElementByTypes('magic-word, template')) {
198
+ const key = String(var1 ?? '').trim();
199
+ let defaultVal = '', found = false, transclusion = false;
200
+ for (let j = 2; j < length; j++) {
201
+ const { anon, name: option, value, firstChild } = childNodes[j];
202
+ transclusion = Boolean(firstChild.getElementByTypes('magic-word, template'));
203
+ if (anon) {
204
+ if (j === length - 1) {
205
+ defaultVal = value;
206
+ }
207
+ else if (transclusion) {
208
+ break;
209
+ }
210
+ else {
211
+ found ||= key === value;
212
+ }
213
+ }
214
+ else if (transclusion) {
215
+ break;
216
+ }
217
+ else if (found || option === key) {
218
+ replace = value;
219
+ break;
220
+ }
221
+ else if (option.toLowerCase() === '#default') {
222
+ defaultVal = value;
223
+ }
224
+ if (j === length - 1) {
225
+ replace = defaultVal;
226
+ }
227
+ }
228
+ if (transclusion) {
229
+ continue;
230
+ }
231
+ }
232
+ else {
233
+ continue;
234
+ }
235
+ target.replaceWith(replace);
236
+ }
237
+ }
238
+ };
239
+ constants_1.classes['ExtendedTableToken'] = __filename;
@@ -0,0 +1,185 @@
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('TranscludeToken.setValue method is only for templates!');
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('TranscludeToken.replaceTemplate method is only for templates!');
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('TranscludeToken.replaceModule method is only for modules!');
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('TranscludeToken.replaceModule method is only for modules!');
71
+ }
72
+ else if (this.length < 2) {
73
+ throw new Error('No module name specified!');
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 @typescript-eslint/no-unused-expressions
133
+ /^a\d+$/u;
134
+ // eslint-disable-next-line es-x/no-regexp-lookbehind-assertions
135
+ const str = key.slice(0, -/(?<!\d)\d+$/u.exec(key)[0].length), regex = new RegExp(String.raw `^${(0, string_1.escapeRegExp)(str)}\d+$`, 'u'), series = this.getAllArgs().filter(({ name }) => regex.test(name)), ordered = series.every(({ name }, i) => {
136
+ const j = Number(name.slice(str.length)), cmp = j <= i + 1 && (i === 0 || j >= last || name === key);
137
+ last = j;
138
+ return cmp;
139
+ });
140
+ if (ordered) {
141
+ for (let i = 0; i < series.length; i++) {
142
+ const name = `${str}${i + 1}`, arg = series[i];
143
+ if (arg.name !== name) {
144
+ if (arg.name === key) {
145
+ remaining--;
146
+ }
147
+ arg.rename(name, true);
148
+ }
149
+ }
150
+ }
151
+ }
152
+ if (remaining > 1) {
153
+ index_1.default.error(`${this.type === 'template'
154
+ ? this.name
155
+ : this.normalizeTitle(this.childNodes[1].text(), 828)
156
+ .title} still has ${remaining} duplicated ${key} parameters:\n${[...this.getArgs(key)].map(arg => {
157
+ const { top, left } = arg.getBoundingClientRect();
158
+ return `Line ${String(top)} Column ${String(left)}`;
159
+ }).join('\n')}`);
160
+ duplicatedKeys.push(key);
161
+ continue;
162
+ }
163
+ }
164
+ return duplicatedKeys;
165
+ };
166
+ transclude_1.TranscludeToken.prototype.escapeTables =
167
+ /** @implements */
168
+ function () {
169
+ if (!/\n[^\S\n]*(?::+[^\S\n]*)?\{\|/u.test(this.text())) {
170
+ return this;
171
+ }
172
+ const stripped = this.toString().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');
173
+ for (const table of parsed.childNodes) {
174
+ if (isTable(table)) {
175
+ table.escape();
176
+ }
177
+ }
178
+ const { firstChild, length } = index_1.default.parse(`{{${parsed.toString()}}}`, include, undefined, config);
179
+ if (length !== 1 || !(firstChild instanceof transclude_1.TranscludeToken)) {
180
+ throw new Error('Failed to escape tables!');
181
+ }
182
+ this.safeReplaceWith(firstChild);
183
+ return firstChild;
184
+ };
185
+ constants_1.classes['ExtendedTranscludeToken'] = __filename;
package/dist/base.d.ts ADDED
@@ -0,0 +1,73 @@
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' | 'magic-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", "invalid-isbn", "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
+ getConfig(): Config;
66
+ /**
67
+ * 解析wikitext
68
+ * @param include 是否嵌入
69
+ * @param maxStage 最大解析层级
70
+ */
71
+ parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): AstElement;
72
+ }
73
+ export {};
package/dist/base.js ADDED
@@ -0,0 +1,39 @@
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
+ 'invalid-isbn',
15
+ 'lonely-apos',
16
+ 'lonely-bracket',
17
+ 'lonely-http',
18
+ 'nested-link',
19
+ 'no-arg',
20
+ 'no-duplicate',
21
+ 'no-ignored',
22
+ 'obsolete-attr',
23
+ 'obsolete-tag',
24
+ 'parsing-order',
25
+ 'pipe-like',
26
+ 'table-layout',
27
+ 'tag-like',
28
+ 'unbalanced-header',
29
+ 'unclosed-comment',
30
+ 'unclosed-quote',
31
+ 'unclosed-table',
32
+ 'unescaped',
33
+ 'unknown-page',
34
+ 'unmatched-tag',
35
+ 'unterminated-url',
36
+ 'url-encoding',
37
+ 'var-anchor',
38
+ 'void-ext',
39
+ ];
@@ -0,0 +1,31 @@
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
+ viewOnly: boolean;
7
+ conversionTable: Map<string, string>;
8
+ redirects: Map<string, string>;
9
+ warning: boolean;
10
+ debugging: boolean;
11
+ /**
12
+ * 规范化页面标题
13
+ * @param title 标题(含或不含命名空间前缀)
14
+ * @param defaultNs 命名空间
15
+ * @param include 是否嵌入
16
+ */
17
+ normalizeTitle(title: string, defaultNs?: number, include?: boolean, config?: Config): Title;
18
+ parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): Token;
19
+ /**
20
+ * 是否是跨维基链接
21
+ * @param title 链接标题
22
+ */
23
+ isInterwiki(title: string, config?: Config): RegExpExecArray | null;
24
+ }
25
+ declare const Parser: Parser;
26
+ // @ts-expect-error mixed export styles
27
+ export = Parser;
28
+ export default Parser;
29
+ export type { Config, LintError };
30
+ export type * from './internal';
31
+ declare global { type Acceptable = unknown; }