wikiparser-node 0.10.0 → 0.11.0-m

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/README.md +44 -32
  2. package/config/.schema.json +1 -8
  3. package/config/default.json +1 -2
  4. package/config/llwiki.json +1 -36
  5. package/config/moegirl.json +1 -45
  6. package/config/zhwiki.json +1 -467
  7. package/dist/index.d.ts +110 -0
  8. package/dist/lib/element.d.ts +48 -0
  9. package/dist/lib/node.d.ts +108 -0
  10. package/dist/lib/text.d.ts +30 -0
  11. package/dist/lib/title.d.ts +15 -0
  12. package/dist/mixin/hidden.d.ts +8 -0
  13. package/dist/parser/brackets.d.ts +12 -0
  14. package/dist/parser/commentAndExt.d.ts +8 -0
  15. package/dist/parser/converter.d.ts +7 -0
  16. package/dist/parser/externalLinks.d.ts +7 -0
  17. package/dist/parser/hrAndDoubleUnderscore.d.ts +11 -0
  18. package/dist/parser/html.d.ts +7 -0
  19. package/dist/parser/links.d.ts +7 -0
  20. package/dist/parser/list.d.ts +7 -0
  21. package/dist/parser/magicLinks.d.ts +7 -0
  22. package/dist/parser/quotes.d.ts +7 -0
  23. package/dist/parser/table.d.ts +11 -0
  24. package/dist/src/arg.d.ts +25 -0
  25. package/dist/src/atom/hidden.d.ts +5 -0
  26. package/dist/src/atom/index.d.ts +14 -0
  27. package/dist/src/attribute.d.ts +42 -0
  28. package/dist/src/attributes.d.ts +33 -0
  29. package/dist/src/converter.d.ts +29 -0
  30. package/dist/src/converterFlags.d.ts +27 -0
  31. package/dist/src/converterRule.d.ts +29 -0
  32. package/dist/src/extLink.d.ts +22 -0
  33. package/dist/src/gallery.d.ts +19 -0
  34. package/dist/src/hasNowiki/index.d.ts +14 -0
  35. package/dist/src/hasNowiki/pre.d.ts +13 -0
  36. package/dist/src/heading.d.ts +24 -0
  37. package/dist/src/html.d.ts +33 -0
  38. package/dist/src/imageParameter.d.ts +23 -0
  39. package/dist/src/imagemap.d.ts +26 -0
  40. package/dist/src/imagemapLink.d.ts +16 -0
  41. package/dist/src/index.d.ts +53 -0
  42. package/dist/src/link/category.d.ts +8 -0
  43. package/dist/src/link/file.d.ts +27 -0
  44. package/dist/src/link/galleryImage.d.ts +15 -0
  45. package/dist/src/link/index.d.ts +29 -0
  46. package/dist/src/magicLink.d.ts +14 -0
  47. package/dist/src/nested/choose.d.ts +13 -0
  48. package/dist/src/nested/combobox.d.ts +13 -0
  49. package/dist/src/nested/index.d.ts +20 -0
  50. package/dist/src/nested/references.d.ts +13 -0
  51. package/dist/src/nowiki/comment.d.ts +19 -0
  52. package/dist/src/nowiki/dd.d.ts +8 -0
  53. package/dist/src/nowiki/doubleUnderscore.d.ts +12 -0
  54. package/dist/src/nowiki/hr.d.ts +13 -0
  55. package/dist/src/nowiki/index.d.ts +18 -0
  56. package/dist/src/nowiki/list.d.ts +8 -0
  57. package/dist/src/nowiki/noinclude.d.ts +8 -0
  58. package/dist/src/nowiki/quote.d.ts +20 -0
  59. package/dist/src/onlyinclude.d.ts +17 -0
  60. package/dist/src/paramTag/index.d.ts +24 -0
  61. package/dist/src/paramTag/inputbox.d.ts +8 -0
  62. package/dist/src/parameter.d.ts +28 -0
  63. package/dist/src/syntax.d.ts +15 -0
  64. package/dist/src/table/index.d.ts +16 -0
  65. package/dist/src/table/td.d.ts +39 -0
  66. package/dist/src/table/tr.d.ts +17 -0
  67. package/dist/src/tagPair/ext.d.ts +17 -0
  68. package/dist/src/tagPair/include.d.ts +16 -0
  69. package/dist/src/tagPair/index.d.ts +28 -0
  70. package/dist/src/transclude.d.ts +75 -0
  71. package/dist/util/lint.d.ts +28 -0
  72. package/dist/util/string.d.ts +31 -0
  73. package/i18n/zh-hans.json +1 -1
  74. package/i18n/zh-hant.json +1 -1
  75. package/index.js +5 -257
  76. package/lib/element.js +7 -482
  77. package/lib/node.js +11 -540
  78. package/lib/text.js +3 -96
  79. package/lib/title.js +1 -28
  80. package/mixin/hidden.js +0 -3
  81. package/package.json +11 -5
  82. package/parser/brackets.js +3 -2
  83. package/parser/commentAndExt.js +8 -6
  84. package/parser/converter.js +1 -2
  85. package/parser/externalLinks.js +1 -2
  86. package/parser/hrAndDoubleUnderscore.js +1 -2
  87. package/parser/html.js +1 -2
  88. package/parser/links.js +5 -6
  89. package/parser/list.js +1 -2
  90. package/parser/magicLinks.js +1 -2
  91. package/parser/quotes.js +1 -2
  92. package/parser/table.js +1 -2
  93. package/src/arg.js +4 -118
  94. package/src/atom/hidden.js +0 -2
  95. package/src/atom/index.js +1 -18
  96. package/src/attribute.js +6 -190
  97. package/src/attributes.js +5 -308
  98. package/src/converter.js +3 -109
  99. package/src/converterFlags.js +1 -188
  100. package/src/converterRule.js +2 -184
  101. package/src/extLink.js +2 -122
  102. package/src/gallery.js +2 -56
  103. package/src/hasNowiki/index.js +1 -13
  104. package/src/hasNowiki/pre.js +1 -13
  105. package/src/heading.js +4 -55
  106. package/src/html.js +5 -120
  107. package/src/imageParameter.js +3 -165
  108. package/src/imagemap.js +3 -62
  109. package/src/imagemapLink.js +2 -14
  110. package/src/index.js +9 -530
  111. package/src/link/category.js +1 -32
  112. package/src/link/file.js +3 -158
  113. package/src/link/galleryImage.js +3 -61
  114. package/src/link/index.js +4 -273
  115. package/src/magicLink.js +6 -87
  116. package/src/nested/choose.js +1 -2
  117. package/src/nested/combobox.js +1 -2
  118. package/src/nested/index.js +8 -32
  119. package/src/nested/references.js +1 -2
  120. package/src/nowiki/comment.js +2 -26
  121. package/src/nowiki/dd.js +1 -47
  122. package/src/nowiki/doubleUnderscore.js +1 -31
  123. package/src/nowiki/hr.js +2 -21
  124. package/src/nowiki/index.js +2 -24
  125. package/src/nowiki/list.js +2 -5
  126. package/src/nowiki/noinclude.js +0 -14
  127. package/src/nowiki/quote.js +2 -16
  128. package/src/onlyinclude.js +2 -27
  129. package/src/paramTag/index.js +2 -25
  130. package/src/paramTag/inputbox.js +2 -5
  131. package/src/parameter.js +6 -148
  132. package/src/syntax.js +1 -69
  133. package/src/table/index.js +2 -939
  134. package/src/table/td.js +6 -226
  135. package/src/table/tr.js +3 -248
  136. package/src/tagPair/ext.js +4 -23
  137. package/src/tagPair/include.js +1 -25
  138. package/src/tagPair/index.js +3 -52
  139. package/src/transclude.js +6 -513
  140. package/typings/api.d.ts +9 -0
  141. package/typings/index.d.ts +51 -0
  142. package/typings/node.d.ts +16 -0
  143. package/typings/parser.d.ts +5 -0
  144. package/typings/token.d.ts +28 -0
  145. package/util/lint.js +2 -0
  146. package/util/string.js +0 -51
  147. package/lib/ranges.js +0 -130
  148. package/mixin/attributeParent.js +0 -117
  149. package/mixin/fixedToken.js +0 -40
  150. package/mixin/singleLine.js +0 -31
  151. package/mixin/sol.js +0 -54
  152. package/parser/selector.js +0 -177
  153. package/src/charinsert.js +0 -97
  154. package/tool/index.js +0 -1202
  155. package/util/debug.js +0 -73
@@ -1,10 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const {text, noWrap, print, extUrlChar, extUrlCharFirst} = require('../util/string'),
3
+ const {extUrlChar, extUrlCharFirst} = require('../util/string'),
4
4
  {generateForSelf} = require('../util/lint'),
5
5
  Title = require('../lib/title'),
6
6
  Parser = require('..'),
7
- AstText = require('../lib/text'),
8
7
  Token = require('.');
9
8
 
10
9
  const params = new Set(['alt', 'link', 'lang', 'page', 'caption']);
@@ -62,69 +61,9 @@ class ImageParameterToken extends Token {
62
61
  return this.name === 'link' ? validate('link', super.text(), this.getAttribute('config')) : undefined;
63
62
  }
64
63
 
65
- set link(value) {
66
- if (this.name === 'link') {
67
- this.setValue(value);
68
- }
69
- }
70
-
71
- /** getValue()的getter */
72
- get value() {
73
- return this.getValue();
74
- }
75
-
76
- set value(value) {
77
- this.setValue(value);
78
- }
79
-
80
- /** 图片大小 */
81
- get size() {
82
- if (this.name === 'width') {
83
- const /** @type {string} */ size = this.getValue().trim();
84
- if (!size.includes('{{')) {
85
- const [width, height = ''] = size.split('x');
86
- return {width, height};
87
- }
88
- const /** @type {{childNodes: AstText[]}} */ token = Parser.parse(size, false, 2, this.getAttribute('config')),
89
- i = token.childNodes.findIndex(({type, data}) => type === 'text' && data.includes('x')),
90
- str = token.childNodes[i];
91
- if (i === -1) {
92
- return {width: size, height: ''};
93
- }
94
- str.splitText(str.data.indexOf('x'));
95
- str.nextSibling.splitText(1);
96
- return {width: text(token.childNodes.slice(0, i + 1)), height: text(token.childNodes.slice(i + 2))};
97
- }
98
- return undefined;
99
- }
100
-
101
- /** 图片宽度 */
102
- get width() {
103
- return this.size?.width;
104
- }
105
-
106
- set width(width) {
107
- if (this.name === 'width') {
108
- const {height} = this;
109
- this.setValue(`${String(width || '')}${height && 'x'}${height}`);
110
- }
111
- }
112
-
113
- /** 图片高度 */
114
- get height() {
115
- return this.size?.height;
116
- }
117
-
118
- set height(height) {
119
- height = String(height || '');
120
- if (this.name === 'width') {
121
- this.setValue(`${this.width}${height && 'x'}${height}`);
122
- }
123
- }
124
-
125
64
  /**
126
65
  * @param {string} str 图片参数
127
- * @param {accum} accum
66
+ * @param {import('../typings/token').accum} accum
128
67
  */
129
68
  constructor(str, config = Parser.getConfig(), accum = []) {
130
69
  let mt;
@@ -142,7 +81,6 @@ class ImageParameterToken extends Token {
142
81
  this.#syntax = str;
143
82
  } else {
144
83
  super(mt[2], config, true, accum, {
145
- 'Stage-2': ':', '!HeadingToken': ':',
146
84
  });
147
85
  this.#syntax = `${mt[1]}${param[0]}${mt[3]}`;
148
86
  }
@@ -167,10 +105,9 @@ class ImageParameterToken extends Token {
167
105
 
168
106
  /**
169
107
  * @override
170
- * @param {string} selector
171
108
  */
172
109
  toString(selector) {
173
- return this.#syntax && !(selector && this.matches(selector))
110
+ return this.#syntax
174
111
  ? this.#syntax.replace('$1', super.toString(selector))
175
112
  : super.toString(selector);
176
113
  }
@@ -199,105 +136,6 @@ class ImageParameterToken extends Token {
199
136
  }
200
137
  return errors;
201
138
  }
202
-
203
- /** @override */
204
- print() {
205
- return this.#syntax
206
- ? `<span class="wpb-image-parameter">${
207
- this.#syntax.replace('$1', `<span class="wpb-image-caption">${print(this.childNodes)}</span>`)
208
- }</span>`
209
- : super.print({class: 'image-caption'});
210
- }
211
-
212
- /** @override */
213
- cloneNode() {
214
- const cloned = this.cloneChildNodes(),
215
- config = this.getAttribute('config');
216
- return Parser.run(() => {
217
- const token = new ImageParameterToken(this.#syntax.replace('$1', ''), config);
218
- token.replaceChildren(...cloned);
219
- return token;
220
- });
221
- }
222
-
223
- /**
224
- * @override
225
- * @template {string} T
226
- * @param {T} key 属性键
227
- * @returns {TokenAttribute<T>}
228
- */
229
- getAttribute(key) {
230
- return key === 'syntax' ? this.#syntax : super.getAttribute(key);
231
- }
232
-
233
- /**
234
- * @override
235
- * @param {PropertyKey} key 属性键
236
- */
237
- hasAttribute(key) {
238
- return key === 'syntax' || super.hasAttribute(key);
239
- }
240
-
241
- /** 是否是不可变参数 */
242
- #isVoid() {
243
- return this.#syntax && !this.#syntax.includes('$1');
244
- }
245
-
246
- /**
247
- * @override
248
- * @template {Token} T
249
- * @param {T} token 待插入的子节点
250
- * @param {number} i 插入位置
251
- * @complexity `n`
252
- * @throws `Error` 不接受自定义输入的图片参数
253
- */
254
- insertAt(token, i = this.length) {
255
- if (!Parser.running && this.#isVoid()) {
256
- throw new Error(`图片参数 ${this.name} 不接受自定义输入!`);
257
- }
258
- return super.insertAt(token, i);
259
- }
260
-
261
- /**
262
- * 获取参数值
263
- * @complexity `n`
264
- */
265
- getValue() {
266
- return this.name === 'invalid' ? this.text() : this.#isVoid() || super.text();
267
- }
268
-
269
- /**
270
- * 设置参数值
271
- * @param {string|boolean} value 参数值
272
- * @complexity `n`
273
- * @throws SyntaxError` 非法的参数值
274
- */
275
- setValue(value) {
276
- if (this.name === 'invalid') {
277
- throw new Error('无效的图片参数!');
278
- } else if (this.#isVoid()) {
279
- if (typeof value !== 'boolean') {
280
- this.typeError('setValue', 'Boolean');
281
- } else if (value === false) {
282
- this.remove();
283
- }
284
- return;
285
- } else if (typeof value !== 'string') {
286
- this.typeError('setValue', 'String');
287
- }
288
- const root = Parser.parse(`[[File:F|${
289
- this.#syntax ? this.#syntax.replace('$1', value) : value
290
- }]]`, this.getAttribute('include'), 6, this.getAttribute('config')),
291
- {length, firstChild: file} = root,
292
- {lastChild: imageParameter, type, name, length: fileLength} = file;
293
- if (length !== 1 || type !== 'file' || name !== 'File:F' || fileLength !== 2
294
- || imageParameter.name !== this.name
295
- ) {
296
- throw new SyntaxError(`非法的 ${this.name} 参数:${noWrap(value)}`);
297
- }
298
- this.replaceChildren(...imageParameter.childNodes);
299
- }
300
139
  }
301
140
 
302
- Parser.classes.ImageParameterToken = __filename;
303
141
  module.exports = ImageParameterToken;
package/src/imagemap.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const {generateForSelf, generateForChild} = require('../util/lint'),
4
- singleLine = require('../mixin/singleLine'),
5
4
  Parser = require('..'),
6
5
  Token = require('.'),
7
6
  NoincludeToken = require('./nowiki/noinclude'),
@@ -24,29 +23,20 @@ class ImagemapToken extends Token {
24
23
  return this.childNodes.find(({type}) => type === 'imagemap-image');
25
24
  }
26
25
 
27
- /**
28
- * 链接
29
- * @returns {ImagemapLinkToken[]}
30
- */
31
- get links() {
32
- return this.childNodes.filter(({type}) => type === 'imagemap-link');
33
- }
34
-
35
26
  /**
36
27
  * @param {string} inner 标签内部wikitext
37
- * @param {accum} accum
28
+ * @param {import('../typings/token').accum} accum
38
29
  * @throws `SyntaxError` 没有合法图片
39
30
  */
40
31
  constructor(inner, config = Parser.getConfig(), accum = []) {
41
32
  super(undefined, config, true, accum, {
42
- GalleryImageToken: ':', ImagemapLinkToken: ':', SingleLineNoincludeToken: ':', AstText: ':',
43
33
  });
44
34
  if (!inner) {
45
35
  return;
46
36
  }
47
37
  const lines = inner.split('\n'),
48
38
  protocols = new Set(config.protocol.split('|')),
49
- SingleLineNoincludeToken = singleLine(NoincludeToken),
39
+ SingleLineNoincludeToken = NoincludeToken,
50
40
  fallback = /** @param {string} line 一行文本 */ line => {
51
41
  super.insertAt(new SingleLineNoincludeToken(line, config, accum));
52
42
  };
@@ -59,7 +49,7 @@ class ImagemapToken extends Token {
59
49
  } else if (first) {
60
50
  const [file, ...options] = line.split('|'),
61
51
  title = this.normalizeTitle(file, 0, true);
62
- if (title.valid && !title.interwiki && title.ns === 6) {
52
+ if (title.valid && title.ns === 6) {
63
53
  const token = new GalleryImageToken(
64
54
  file, options.length > 0 ? options.join('|') : undefined, config, accum,
65
55
  );
@@ -68,7 +58,6 @@ class ImagemapToken extends Token {
68
58
  first = false;
69
59
  continue;
70
60
  } else {
71
- Parser.error('<imagemap>标签内必须先包含一张合法图片!', line);
72
61
  error = true;
73
62
  }
74
63
  } else if (line.trim().split(/[\t ]/u)[0] === 'desc') {
@@ -112,7 +101,6 @@ class ImagemapToken extends Token {
112
101
 
113
102
  /**
114
103
  * @override
115
- * @param {string} selector
116
104
  */
117
105
  toString(selector) {
118
106
  return super.toString(selector, '\n');
@@ -128,11 +116,6 @@ class ImagemapToken extends Token {
128
116
  return 1;
129
117
  }
130
118
 
131
- /** @override */
132
- print() {
133
- return super.print({sep: '\n'});
134
- }
135
-
136
119
  /**
137
120
  * @override
138
121
  * @param {number} start 起始位置
@@ -152,48 +135,6 @@ class ImagemapToken extends Token {
152
135
  }
153
136
  return errors;
154
137
  }
155
-
156
- /**
157
- * @override
158
- * @template {string|Token} T
159
- * @param {T} token 待插入的节点
160
- * @param {number} i 插入位置
161
- * @throws `Error` 当前缺少合法图片
162
- * @throws `RangeError` 已有一张合法图片
163
- */
164
- insertAt(token, i = 0) {
165
- const {image} = this;
166
- if (!image && (token.type === 'imagemap-link' || token.type === 'text')) {
167
- throw new Error('当前缺少一张合法图片!');
168
- } else if (image && token.type === 'imagemap-image') {
169
- throw new RangeError('已有一张合法图片!');
170
- }
171
- return super.insertAt(token, i);
172
- }
173
-
174
- /**
175
- * @override
176
- * @param {number} i 移除位置
177
- * @throws `Error` 禁止移除图片
178
- */
179
- removeAt(i) {
180
- const child = this.childNodes[i];
181
- if (child.type === 'imagemap-image') {
182
- throw new Error('禁止移除<imagemap>内的图片!');
183
- }
184
- return super.removeAt(i);
185
- }
186
-
187
- /** @override */
188
- cloneNode() {
189
- const cloned = this.cloneChildNodes();
190
- return Parser.run(() => {
191
- const token = new ImagemapToken(undefined, this.getAttribute('config'));
192
- token.append(...cloned);
193
- return token;
194
- });
195
- }
196
138
  }
197
139
 
198
- Parser.classes.ImagemapToken = __filename;
199
140
  module.exports = ImagemapToken;
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const Title = require('../lib/title'),
4
- fixedToken = require('../mixin/fixedToken'),
5
- singleLine = require('../mixin/singleLine'),
6
- Parser = require('..'),
7
4
  Token = require('.'),
8
5
  NoincludeToken = require('./nowiki/noinclude'),
9
6
  LinkToken = require('./link'),
@@ -13,22 +10,14 @@ const Title = require('../lib/title'),
13
10
  * `<imagemap>`内的链接
14
11
  * @classdesc `{childNodes: [AstText, LinkToken|ExtLinkToken, NoincludeToken]}`
15
12
  */
16
- class ImagemapLinkToken extends fixedToken(singleLine(Token)) {
13
+ class ImagemapLinkToken extends Token {
17
14
  type = 'imagemap-link';
18
15
 
19
- /**
20
- * 内外链接
21
- * @this {{childNodes: (LinkToken|ExtLinkToken)[]}}
22
- */
23
- get link() {
24
- return this.childNodes[1].link;
25
- }
26
-
27
16
  /**
28
17
  * @param {string} pre 链接前的文本
29
18
  * @param {[string, string, string|Title]} linkStuff 内外链接
30
19
  * @param {string} post 链接后的文本
31
- * @param {accum} accum
20
+ * @param {import('../typings/token').accum} accum
32
21
  */
33
22
  constructor(pre, linkStuff, post, config, accum) {
34
23
  const SomeLinkToken = linkStuff.length === 2 ? LinkToken : ExtLinkToken;
@@ -37,5 +26,4 @@ class ImagemapLinkToken extends fixedToken(singleLine(Token)) {
37
26
  }
38
27
  }
39
28
 
40
- Parser.classes.ImagemapLinkToken = __filename;
41
29
  module.exports = ImagemapLinkToken;