wikiparser-node 0.11.0-m → 0.11.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.
Files changed (141) hide show
  1. package/config/.schema.json +7 -0
  2. package/config/default.json +1 -0
  3. package/config/llwiki.json +35 -0
  4. package/config/moegirl.json +44 -0
  5. package/config/zhwiki.json +466 -0
  6. package/dist/index.d.ts +4 -0
  7. package/dist/lib/element.d.ts +116 -2
  8. package/dist/lib/node.d.ts +193 -10
  9. package/dist/lib/ranges.d.ts +37 -0
  10. package/dist/lib/text.d.ts +35 -1
  11. package/dist/lib/title.d.ts +6 -0
  12. package/dist/mixin/attributeParent.d.ts +9 -0
  13. package/dist/mixin/fixedToken.d.ts +8 -0
  14. package/dist/mixin/singleLine.d.ts +8 -0
  15. package/dist/mixin/sol.d.ts +8 -0
  16. package/dist/parser/selector.d.ts +12 -0
  17. package/dist/src/arg.d.ts +30 -1
  18. package/dist/src/atom/index.d.ts +2 -1
  19. package/dist/src/attribute.d.ts +24 -1
  20. package/dist/src/attributes.d.ts +79 -0
  21. package/dist/src/charinsert.d.ts +32 -0
  22. package/dist/src/converter.d.ts +75 -1
  23. package/dist/src/converterFlags.d.ts +57 -1
  24. package/dist/src/converterRule.d.ts +47 -1
  25. package/dist/src/extLink.d.ts +41 -1
  26. package/dist/src/gallery.d.ts +15 -1
  27. package/dist/src/heading.d.ts +22 -2
  28. package/dist/src/html.d.ts +25 -2
  29. package/dist/src/imageParameter.d.ts +43 -1
  30. package/dist/src/imagemap.d.ts +12 -1
  31. package/dist/src/imagemapLink.d.ts +5 -0
  32. package/dist/src/index.d.ts +136 -3
  33. package/dist/src/link/category.d.ts +8 -0
  34. package/dist/src/link/file.d.ts +58 -0
  35. package/dist/src/link/index.d.ts +61 -2
  36. package/dist/src/magicLink.d.ts +22 -0
  37. package/dist/src/nested/index.d.ts +3 -5
  38. package/dist/src/nowiki/comment.d.ts +13 -1
  39. package/dist/src/nowiki/dd.d.ts +9 -0
  40. package/dist/src/nowiki/doubleUnderscore.d.ts +11 -1
  41. package/dist/src/nowiki/index.d.ts +11 -2
  42. package/dist/src/nowiki/quote.d.ts +0 -7
  43. package/dist/src/onlyinclude.d.ts +8 -1
  44. package/dist/src/paramTag/index.d.ts +8 -3
  45. package/dist/src/parameter.d.ts +48 -1
  46. package/dist/src/syntax.d.ts +6 -1
  47. package/dist/src/table/index.d.ts +257 -0
  48. package/dist/src/table/td.d.ts +67 -6
  49. package/dist/src/table/tr.d.ts +74 -0
  50. package/dist/src/tagPair/ext.d.ts +2 -1
  51. package/dist/src/tagPair/include.d.ts +9 -0
  52. package/dist/src/tagPair/index.d.ts +14 -1
  53. package/dist/src/transclude.d.ts +125 -1
  54. package/dist/tool/index.d.ts +420 -0
  55. package/dist/util/base.d.ts +10 -0
  56. package/dist/util/debug.d.ts +20 -0
  57. package/dist/util/diff.d.ts +8 -0
  58. package/dist/util/string.d.ts +24 -0
  59. package/index.js +256 -4
  60. package/lib/element.js +488 -6
  61. package/lib/node.js +495 -6
  62. package/lib/ranges.js +130 -0
  63. package/lib/text.js +96 -1
  64. package/lib/title.js +28 -1
  65. package/mixin/attributeParent.js +117 -0
  66. package/mixin/fixedToken.js +40 -0
  67. package/mixin/hidden.js +3 -0
  68. package/mixin/singleLine.js +31 -0
  69. package/mixin/sol.js +54 -0
  70. package/package.json +5 -2
  71. package/parser/brackets.js +1 -0
  72. package/parser/commentAndExt.js +1 -0
  73. package/parser/converter.js +1 -0
  74. package/parser/externalLinks.js +1 -0
  75. package/parser/hrAndDoubleUnderscore.js +1 -0
  76. package/parser/html.js +1 -0
  77. package/parser/links.js +5 -4
  78. package/parser/list.js +1 -0
  79. package/parser/magicLinks.js +1 -0
  80. package/parser/quotes.js +1 -0
  81. package/parser/selector.js +180 -0
  82. package/parser/table.js +1 -0
  83. package/src/arg.js +116 -2
  84. package/src/atom/hidden.js +2 -0
  85. package/src/atom/index.js +17 -0
  86. package/src/attribute.js +189 -3
  87. package/src/attributes.js +307 -4
  88. package/src/charinsert.js +97 -0
  89. package/src/converter.js +108 -2
  90. package/src/converterFlags.js +187 -0
  91. package/src/converterRule.js +183 -1
  92. package/src/extLink.js +121 -1
  93. package/src/gallery.js +54 -0
  94. package/src/hasNowiki/index.js +12 -0
  95. package/src/hasNowiki/pre.js +12 -0
  96. package/src/heading.js +54 -3
  97. package/src/html.js +118 -3
  98. package/src/imageParameter.js +164 -2
  99. package/src/imagemap.js +61 -2
  100. package/src/imagemapLink.js +13 -1
  101. package/src/index.js +530 -3
  102. package/src/link/category.js +32 -1
  103. package/src/link/file.js +157 -2
  104. package/src/link/galleryImage.js +60 -2
  105. package/src/link/index.js +270 -1
  106. package/src/magicLink.js +83 -0
  107. package/src/nested/choose.js +1 -0
  108. package/src/nested/combobox.js +1 -0
  109. package/src/nested/index.js +27 -0
  110. package/src/nested/references.js +1 -0
  111. package/src/nowiki/comment.js +25 -1
  112. package/src/nowiki/dd.js +47 -1
  113. package/src/nowiki/doubleUnderscore.js +31 -1
  114. package/src/nowiki/hr.js +20 -1
  115. package/src/nowiki/index.js +23 -1
  116. package/src/nowiki/list.js +5 -2
  117. package/src/nowiki/noinclude.js +14 -0
  118. package/src/nowiki/quote.js +14 -0
  119. package/src/onlyinclude.js +26 -1
  120. package/src/paramTag/index.js +24 -1
  121. package/src/paramTag/inputbox.js +4 -1
  122. package/src/parameter.js +147 -5
  123. package/src/syntax.js +68 -0
  124. package/src/table/index.js +941 -2
  125. package/src/table/td.js +230 -5
  126. package/src/table/tr.js +247 -2
  127. package/src/tagPair/ext.js +21 -1
  128. package/src/tagPair/include.js +24 -0
  129. package/src/tagPair/index.js +56 -2
  130. package/src/transclude.js +512 -5
  131. package/tool/index.js +1209 -0
  132. package/typings/array.d.ts +29 -0
  133. package/typings/event.d.ts +22 -0
  134. package/typings/index.d.ts +67 -0
  135. package/typings/node.d.ts +19 -0
  136. package/typings/parser.d.ts +7 -0
  137. package/typings/table.d.ts +10 -0
  138. package/typings/token.d.ts +3 -0
  139. package/typings/tool.d.ts +6 -0
  140. package/util/debug.js +73 -0
  141. package/util/string.js +51 -0
package/lib/ranges.js ADDED
@@ -0,0 +1,130 @@
1
+ 'use strict';
2
+
3
+ const Parser = require('..');
4
+
5
+ /** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
6
+ class Range {
7
+ start;
8
+ end;
9
+ step;
10
+
11
+ /**
12
+ * @param {string|Range} str 表达式
13
+ * @throws `RangeError` 起点、终点和步长均应为整数
14
+ * @throws `RangeError` n的系数不能为0
15
+ * @throws `RangeError` 应使用CSS选择器或Python切片的格式
16
+ */
17
+ constructor(str) {
18
+ if (str instanceof Range) {
19
+ Object.assign(this, str);
20
+ return;
21
+ }
22
+ str = str.trim();
23
+ if (str === 'odd') {
24
+ Object.assign(this, {start: 1, end: Infinity, step: 2});
25
+ } else if (str === 'even') {
26
+ Object.assign(this, {start: 0, end: Infinity, step: 2});
27
+ } else if (str.includes(':')) {
28
+ const [start, end, step = '1'] = str.split(':', 3);
29
+ this.start = Number(start);
30
+ this.end = Number(end || Infinity);
31
+ this.step = Math.max(Number(step), 1);
32
+ if (!Number.isInteger(this.start)) {
33
+ throw new RangeError(`起点 ${this.start} 应为整数!`);
34
+ } else if (this.end !== Infinity && !Number.isInteger(this.end)) {
35
+ throw new RangeError(`终点 ${this.end} 应为整数!`);
36
+ } else if (!Number.isInteger(this.step)) {
37
+ throw new RangeError(`步长 ${this.step} 应为整数!`);
38
+ }
39
+ } else {
40
+ const mt = /^([+-])?(\d+)?n(?:([+-])(\d+))?$/u.exec(str);
41
+ if (mt) {
42
+ const [, sgnA = '+', a = 1, sgnB = '+'] = mt,
43
+ b = Number(mt[4] ?? 0);
44
+ this.step = Number(a);
45
+ if (this.step === 0) {
46
+ throw new RangeError(`参数 ${str} 中 "n" 的系数不允许为 0!`);
47
+ } else if (sgnA === '+') {
48
+ this.start = sgnB === '+' || b === 0 ? b : this.step - 1 - (b - 1) % this.step;
49
+ this.end = Infinity;
50
+ } else if (sgnB === '-') {
51
+ this.start = 0;
52
+ this.end = b > 0 ? 0 : this.step;
53
+ } else {
54
+ this.start = b % this.step;
55
+ this.end = this.step + b;
56
+ }
57
+ } else {
58
+ throw new RangeError(`参数 ${str} 应写作CSS选择器的 "an+b" 形式或Python切片!`);
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * 将Range转换为针对特定数组的下标集
65
+ * @param {number|*[]} arr 参考数组
66
+ * @complexity `n`
67
+ */
68
+ applyTo(arr) {
69
+ return new Array(typeof arr === 'number' ? arr : arr.length).fill().map((_, i) => i).slice(this.start, this.end)
70
+ .filter((_, j) => j % this.step === 0);
71
+ }
72
+ }
73
+
74
+ /** @extends {Array<number|Range>} */
75
+ class Ranges extends Array {
76
+ /** @param {number|string|Range|(number|string|Range)[]} arr 表达式数组 */
77
+ constructor(arr) {
78
+ super();
79
+ if (arr === undefined) {
80
+ return;
81
+ }
82
+ arr = Array.isArray(arr) ? arr : [arr];
83
+ for (const ele of arr) {
84
+ if (ele instanceof Range) {
85
+ this.push(new Range(ele));
86
+ continue;
87
+ }
88
+ const number = Number(ele);
89
+ if (Number.isInteger(number)) {
90
+ this.push(number);
91
+ } else if (typeof ele === 'string' && Number.isNaN(number)) {
92
+ try {
93
+ const range = new Range(ele);
94
+ this.push(range);
95
+ } catch {}
96
+ }
97
+ }
98
+ }
99
+
100
+ /**
101
+ * 将Ranges转换为针对特定Array的下标集
102
+ * @param {number|*[]} arr 参考数组
103
+ * @complexity `n`
104
+ */
105
+ applyTo(arr) {
106
+ const length = typeof arr === 'number' ? arr : arr.length;
107
+ return [
108
+ ...new Set(
109
+ [...this].flatMap(ele => {
110
+ if (typeof ele === 'number') {
111
+ return ele < 0 ? ele + length : ele;
112
+ }
113
+ return ele.applyTo(length);
114
+ }),
115
+ ),
116
+ ].filter(i => i >= 0 && i < length).sort();
117
+ }
118
+
119
+ /**
120
+ * 检查某个下标是否符合表达式
121
+ * @param {string} str 表达式
122
+ * @param {number} i 待检查的下标
123
+ */
124
+ static nth(str, i) {
125
+ return new Ranges(str.split(',')).applyTo(i + 1).includes(i);
126
+ }
127
+ }
128
+
129
+ Parser.classes.Ranges = __filename;
130
+ module.exports = Ranges;
package/lib/text.js CHANGED
@@ -66,10 +66,19 @@ class AstText extends AstNode {
66
66
  type = 'text';
67
67
  /** @type {string} */ data;
68
68
 
69
+ /** 文本长度 */
70
+ get length() {
71
+ return this.data.length;
72
+ }
73
+
69
74
  /** @param {string} text 包含文本 */
70
75
  constructor(text = '') {
71
76
  super();
72
- this.data = text;
77
+ Object.defineProperties(this, {
78
+ data: {value: text, writable: false},
79
+ childNodes: {enumerable: false, configurable: false},
80
+ type: {enumerable: false, writable: false, configurable: false},
81
+ });
73
82
  }
74
83
 
75
84
  /** 输出字符串 */
@@ -155,7 +164,12 @@ class AstText extends AstNode {
155
164
  */
156
165
  #setData(text) {
157
166
  text = String(text);
167
+ const {data} = this,
168
+ e = new Event('text', {bubbles: true});
158
169
  this.setAttribute('data', text);
170
+ if (data !== text) {
171
+ this.dispatchEvent(e, {oldText: data, newText: text});
172
+ }
159
173
  }
160
174
 
161
175
  /**
@@ -165,6 +179,87 @@ class AstText extends AstNode {
165
179
  replaceData(text = '') {
166
180
  this.#setData(text);
167
181
  }
182
+
183
+ /** 复制 */
184
+ cloneNode() {
185
+ return new AstText(this.data);
186
+ }
187
+
188
+ /**
189
+ * @override
190
+ * @template {string} T
191
+ * @param {T} key 属性键
192
+ * @returns {import('../typings/node').TokenAttribute<T>}
193
+ * @throws `Error` 文本节点没有子节点
194
+ */
195
+ getAttribute(key) {
196
+ return key === 'verifyChild'
197
+ ? () => {
198
+ throw new Error('文本节点没有子节点!');
199
+ }
200
+ : super.getAttribute(key);
201
+ }
202
+
203
+ /**
204
+ * 在后方添加字符串
205
+ * @param {string} text 添加的字符串
206
+ */
207
+ appendData(text) {
208
+ this.#setData(this.data + text);
209
+ }
210
+
211
+ /**
212
+ * 删减字符串
213
+ * @param {number} offset 起始位置
214
+ * @param {number} count 删减字符数
215
+ */
216
+ deleteData(offset, count) {
217
+ this.#setData(this.data.slice(0, offset) + this.data.slice(offset + count));
218
+ }
219
+
220
+ /**
221
+ * 插入字符串
222
+ * @param {number} offset 插入位置
223
+ * @param {string} text 待插入的字符串
224
+ */
225
+ insertData(offset, text) {
226
+ this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
227
+ }
228
+
229
+ /**
230
+ * 提取子串
231
+ * @param {number} offset 起始位置
232
+ * @param {number} count 字符数
233
+ */
234
+ substringData(offset, count) {
235
+ return this.data.slice(offset, offset + count);
236
+ }
237
+
238
+ /**
239
+ * 将文本子节点分裂为两部分
240
+ * @param {number} offset 分裂位置
241
+ * @throws `RangeError` 错误的断开位置
242
+ * @throws `Error` 没有父节点
243
+ */
244
+ splitText(offset) {
245
+ if (!Number.isInteger(offset)) {
246
+ this.typeError('splitText', 'Number');
247
+ } else if (offset > this.length || offset < -this.length) {
248
+ throw new RangeError(`错误的断开位置!${offset}`);
249
+ }
250
+ const {parentNode, data} = this;
251
+ if (!parentNode) {
252
+ throw new Error('待分裂的文本节点没有父节点!');
253
+ }
254
+ const newText = new AstText(data.slice(offset)),
255
+ childNodes = [...parentNode.childNodes];
256
+ this.setAttribute('data', data.slice(0, offset));
257
+ childNodes.splice(childNodes.indexOf(this) + 1, 0, newText);
258
+ newText.setAttribute('parentNode', parentNode);
259
+ parentNode.setAttribute('childNodes', childNodes);
260
+ return newText;
261
+ }
168
262
  }
169
263
 
264
+ Parser.classes.AstText = __filename;
170
265
  module.exports = AstText;
package/lib/title.js CHANGED
@@ -9,6 +9,10 @@ class Title {
9
9
  ns = 0;
10
10
  fragment;
11
11
  encoded = false;
12
+ title = '';
13
+ main = '';
14
+ prefix = '';
15
+ interwiki = '';
12
16
 
13
17
  /**
14
18
  * @param {string} title 标题(含或不含命名空间前缀)
@@ -32,6 +36,11 @@ class Title {
32
36
  namespace = '';
33
37
  title = title.slice(1).trim();
34
38
  }
39
+ const iw = defaultNs ? undefined : Parser.isInterwiki(title, config);
40
+ if (iw) {
41
+ this.interwiki = iw[1].toLowerCase();
42
+ title = title.slice(iw[0].length);
43
+ }
35
44
  const m = title.split(':');
36
45
  if (m.length > 1) {
37
46
  const id = namespaces[nsid[m[0].trim().toLowerCase()]];
@@ -45,12 +54,30 @@ class Title {
45
54
  let fragment;
46
55
  if (i !== -1) {
47
56
  fragment = title.slice(i + 1).trimEnd();
57
+ if (fragment.includes('%')) {
58
+ try {
59
+ fragment = decodeURIComponent(fragment);
60
+ } catch {}
61
+ } else if (fragment.includes('.')) {
62
+ try {
63
+ fragment = decodeURIComponent(fragment.replaceAll('.', '%'));
64
+ } catch {}
65
+ }
48
66
  title = title.slice(0, i).trim();
49
67
  }
50
- this.valid = Boolean(title || selfLink && fragment !== undefined)
68
+ this.valid = Boolean(title || selfLink && fragment !== undefined || this.interwiki)
51
69
  && !/\0\d+[eh!+-]\x7F|[<>[\]{}|]|%[\da-f]{2}/iu.test(title);
52
70
  this.fragment = fragment;
71
+ this.main = title && `${title[0].toUpperCase()}${title.slice(1)}`;
72
+ this.prefix = `${namespace}${namespace && ':'}`;
73
+ this.title = `${iw ? `${this.interwiki}:` : ''}${this.prefix}${this.main.replaceAll(' ', '_')}`;
74
+ }
75
+
76
+ /** @override */
77
+ toString() {
78
+ return `${this.title}${this.fragment === undefined ? '' : `#${this.fragment}`}`;
53
79
  }
54
80
  }
55
81
 
82
+ Parser.classes.Title = __filename;
56
83
  module.exports = Title;
@@ -0,0 +1,117 @@
1
+ 'use strict';
2
+
3
+ const Parser = require('..'),
4
+ AttributesToken = require('../src/attributes');
5
+
6
+ /**
7
+ * 子节点含有AttributesToken的类
8
+ * @template T
9
+ * @param {T} Constructor 基类
10
+ * @param {number} i AttributesToken子节点的位置
11
+ * @returns {T}
12
+ */
13
+ const attributeParent = (Constructor, i = 0) => class extends Constructor {
14
+ /** getAttrs()方法的getter写法 */
15
+ get attributes() {
16
+ return this.getAttrs();
17
+ }
18
+
19
+ /** 以字符串表示的class属性 */
20
+ get className() {
21
+ const attr = this.getAttr('class');
22
+ return typeof attr === 'string' ? attr : '';
23
+ }
24
+
25
+ set className(className) {
26
+ this.setAttr('class', className);
27
+ }
28
+
29
+ /** 以Set表示的class属性 */
30
+ get classList() {
31
+ return new Set(this.className.split(/\s/u));
32
+ }
33
+
34
+ /** id属性 */
35
+ get id() {
36
+ const attr = this.getAttr('id');
37
+ return typeof attr === 'string' ? attr : '';
38
+ }
39
+
40
+ set id(id) {
41
+ this.setAttr('id', id);
42
+ }
43
+
44
+ /**
45
+ * AttributesToken子节点是否具有某属性
46
+ * @this {{childNodes: AttributesToken[]}}
47
+ * @param {string} key 属性键
48
+ */
49
+ hasAttr(key) {
50
+ return this.childNodes.at(i).hasAttr(key);
51
+ }
52
+
53
+ /**
54
+ * 获取AttributesToken子节点的属性
55
+ * @this {{childNodes: AttributesToken[]}}
56
+ * @param {string} key 属性键
57
+ */
58
+ getAttr(key) {
59
+ return this.childNodes.at(i).getAttr(key);
60
+ }
61
+
62
+ /**
63
+ * 列举AttributesToken子节点的属性键
64
+ * @this {{childNodes: AttributesToken[]}}
65
+ */
66
+ getAttrNames() {
67
+ return this.childNodes.at(i).getAttrNames();
68
+ }
69
+
70
+ /**
71
+ * AttributesToken子节点是否具有任意属性
72
+ * @this {{childNodes: AttributesToken[]}}
73
+ */
74
+ hasAttrs() {
75
+ return this.childNodes.at(i).hasAttrs();
76
+ }
77
+
78
+ /**
79
+ * 获取AttributesToken子节点的全部标签属性
80
+ * @this {{childNodes: AttributesToken[]}}
81
+ */
82
+ getAttrs() {
83
+ return this.childNodes.at(i).getAttrs();
84
+ }
85
+
86
+ /**
87
+ * 对AttributesToken子节点设置属性
88
+ * @this {{childNodes: AttributesToken[]}}
89
+ * @param {string} key 属性键
90
+ * @param {string|boolean} value 属性值
91
+ */
92
+ setAttr(key, value) {
93
+ return this.childNodes.at(i).setAttr(key, value);
94
+ }
95
+
96
+ /**
97
+ * 移除AttributesToken子节点的某属性
98
+ * @this {{childNodes: AttributesToken[]}}
99
+ * @param {string} key 属性键
100
+ */
101
+ removeAttr(key) {
102
+ this.childNodes.at(i).removeAttr(key);
103
+ }
104
+
105
+ /**
106
+ * 开关AttributesToken子节点的某属性
107
+ * @this {{childNodes: AttributesToken[]}}
108
+ * @param {string} key 属性键
109
+ * @param {boolean|undefined} force 强制开启或关闭
110
+ */
111
+ toggleAttr(key, force) {
112
+ this.childNodes.at(i).toggleAttr(key, force);
113
+ }
114
+ };
115
+
116
+ Parser.mixins.attributeParent = __filename;
117
+ module.exports = attributeParent;
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const Parser = require('..'),
4
+ Token = require('../src');
5
+
6
+ /**
7
+ * 不可增删子节点的类
8
+ * @template T
9
+ * @param {T} Constructor 基类
10
+ * @returns {T}
11
+ */
12
+ const fixedToken = Constructor => class extends Constructor {
13
+ static fixed = true;
14
+
15
+ /**
16
+ * 移除子节点
17
+ * @throws `Error`
18
+ */
19
+ removeAt() {
20
+ throw new Error(`${this.constructor.name} 不可删除元素!`);
21
+ }
22
+
23
+ /**
24
+ * 插入子节点
25
+ * @template {Token} T
26
+ * @param {T} token 待插入的子节点
27
+ * @param {number} i 插入位置
28
+ * @throws `Error`
29
+ */
30
+ insertAt(token, i = this.length) {
31
+ if (Parser.running) {
32
+ super.insertAt(token, i);
33
+ return token;
34
+ }
35
+ throw new Error(`${this.constructor.name} 不可插入元素!`);
36
+ }
37
+ };
38
+
39
+ Parser.mixins.fixedToken = __filename;
40
+ module.exports = fixedToken;
package/mixin/hidden.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const Parser = require('..');
4
+
3
5
  /**
4
6
  * 解析后不可见的类
5
7
  * @template T
@@ -15,4 +17,5 @@ const hidden = Constructor => class extends Constructor {
15
17
  }
16
18
  };
17
19
 
20
+ Parser.mixins.hidden = __filename;
18
21
  module.exports = hidden;
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ const Parser = require('..');
4
+
5
+ /**
6
+ * 不可包含换行符的类
7
+ * @template {Function} T
8
+ * @param {T} Constructor 基类
9
+ * @returns {T}
10
+ */
11
+ const singleLine = Constructor => {
12
+ const SingleLineConstructor = class extends Constructor {
13
+ /**
14
+ * @override
15
+ * @param {string} selector
16
+ */
17
+ toString(selector) {
18
+ return super.toString(selector).replaceAll('\n', ' ');
19
+ }
20
+
21
+ /** @override */
22
+ text() {
23
+ return super.text().replaceAll('\n', ' ');
24
+ }
25
+ };
26
+ Object.defineProperty(SingleLineConstructor, 'name', {value: `SingleLine${Constructor.name}`});
27
+ return SingleLineConstructor;
28
+ };
29
+
30
+ Parser.mixins.singleLine = __filename;
31
+ module.exports = singleLine;
package/mixin/sol.js ADDED
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const Parser = require('..'),
4
+ Token = require('../src');
5
+
6
+ /**
7
+ * 只能位于行首的类
8
+ * @template T
9
+ * @param {T} Constructor 基类
10
+ * @returns {T}
11
+ */
12
+ const sol = Constructor => class SolToken extends Constructor {
13
+ /**
14
+ * 是否可以视为root节点
15
+ * @this {Token}
16
+ */
17
+ #isRoot() {
18
+ const {parentNode, type} = this;
19
+ return parentNode?.type === 'root'
20
+ || type !== 'heading' && parentNode?.type === 'ext-inner' && parentNode.name === 'poem';
21
+ }
22
+
23
+ /**
24
+ * 在前方插入newline
25
+ * @this {SolToken & Token}
26
+ */
27
+ prependNewLine() {
28
+ return (this.previousVisibleSibling || !this.#isRoot()) && !String(this.previousVisibleSibling).endsWith('\n')
29
+ ? '\n'
30
+ : '';
31
+ }
32
+
33
+ /**
34
+ * 还原为wikitext
35
+ * @this {SolToken & Token}
36
+ * @param {string} selector
37
+ */
38
+ toString(selector) {
39
+ return selector && this.matches(selector) ? '' : `${this.prependNewLine()}${super.toString(selector)}`;
40
+ }
41
+
42
+ /** 获取padding */
43
+ getPadding() {
44
+ return this.prependNewLine().length;
45
+ }
46
+
47
+ /** 可见部分 */
48
+ text() {
49
+ return `${this.prependNewLine()}${super.text()}`;
50
+ }
51
+ };
52
+
53
+ Parser.mixins.sol = __filename;
54
+ module.exports = sol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "0.11.0-m",
3
+ "version": "0.11.0",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -21,6 +21,7 @@
21
21
  "/mixin/",
22
22
  "/parser/",
23
23
  "/src/",
24
+ "/tool/",
24
25
  "/util/",
25
26
  "/typings/",
26
27
  "/dist/"
@@ -32,9 +33,11 @@
32
33
  },
33
34
  "scripts": {
34
35
  "build": "rm -rf dist/ && tsc && grep -rl '/typings' dist/ | xargs gsed -i 's|/typings|/../typings|g' && node ./bin/tsc.js",
36
+ "doc": "node ./bin/doc.js",
37
+ "toc": "node ./bin/toc.js",
35
38
  "test": "eslint . && ajv -s config/.schema.json -d 'config/*.json' --strict=true --strict-required=false",
36
39
  "real": "node test/real.js",
37
- "single": "node test/single.js"
40
+ "single": "node --prof test/single.js && node --prof-process isolate-0x*-v8.log > test/processed.txt && rm isolate-0x*-v8.log"
38
41
  },
39
42
  "devDependencies": {
40
43
  "@types/node": "^17.0.23",
@@ -124,4 +124,5 @@ const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
124
124
  return text;
125
125
  };
126
126
 
127
+ Parser.parsers.parseBrackets = __filename;
127
128
  module.exports = parseBrackets;
@@ -58,4 +58,5 @@ const parseCommentAndExt = (text, config = Parser.getConfig(), accum = [], inclu
58
58
  );
59
59
  };
60
60
 
61
+ Parser.parsers.parseCommentAndExt = __filename;
61
62
  module.exports = parseCommentAndExt;
@@ -42,4 +42,5 @@ const parseConverter = (wikitext, config = Parser.getConfig(), accum = []) => {
42
42
  return wikitext;
43
43
  };
44
44
 
45
+ Parser.parsers.parseConverter = __filename;
45
46
  module.exports = parseConverter;
@@ -29,4 +29,5 @@ const parseExternalLinks = (wikitext, config = Parser.getConfig(), accum = []) =
29
29
  });
30
30
  };
31
31
 
32
+ Parser.parsers.parseExternalLinks = __filename;
32
33
  module.exports = parseExternalLinks;
@@ -45,4 +45,5 @@ const parseHrAndDoubleUnderscore = ({firstChild: {data}, type, name}, config = P
45
45
  return type === 'root' || type === 'ext-inner' && name === 'poem' ? data : data.slice(1);
46
46
  };
47
47
 
48
+ Parser.parsers.parseHrAndDoubleUnderscore = __filename;
48
49
  module.exports = parseHrAndDoubleUnderscore;
package/parser/html.js CHANGED
@@ -38,4 +38,5 @@ const parseHtml = (wikitext, config = Parser.getConfig(), accum = []) => {
38
38
  return text;
39
39
  };
40
40
 
41
+ Parser.parsers.parseHtml = __filename;
41
42
  module.exports = parseHtml;
package/parser/links.js CHANGED
@@ -44,12 +44,12 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
44
44
  continue;
45
45
  }
46
46
  const title = Parser.normalizeTitle(link, 0, false, config, true, true, true),
47
- {ns, valid} = title;
47
+ {ns, interwiki, valid} = title;
48
48
  if (!valid) {
49
49
  s += `[[${x}`;
50
50
  continue;
51
51
  } else if (mightBeImg) {
52
- if (ns !== 6) {
52
+ if (interwiki || ns !== 6) {
53
53
  s += `[[${x}`;
54
54
  continue;
55
55
  }
@@ -79,9 +79,9 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
79
79
  s += `\0${accum.length}l\x7F${after}`;
80
80
  let SomeLinkToken = LinkToken;
81
81
  if (!force) {
82
- if (ns === 6) {
82
+ if (!interwiki && ns === 6) {
83
83
  SomeLinkToken = FileToken;
84
- } else if (ns === 14) {
84
+ } else if (!interwiki && ns === 14) {
85
85
  SomeLinkToken = CategoryToken;
86
86
  }
87
87
  }
@@ -90,4 +90,5 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
90
90
  return s;
91
91
  };
92
92
 
93
+ Parser.parsers.parseLinks = __filename;
93
94
  module.exports = parseLinks;
package/parser/list.js CHANGED
@@ -55,4 +55,5 @@ const parseList = (text, config = Parser.getConfig(), accum = []) => {
55
55
  return text;
56
56
  };
57
57
 
58
+ Parser.parsers.parseList = __filename;
58
59
  module.exports = parseList;
@@ -37,4 +37,5 @@ const parseMagicLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
37
37
  });
38
38
  };
39
39
 
40
+ Parser.parsers.parseMagicLinks = __filename;
40
41
  module.exports = parseMagicLinks;
package/parser/quotes.js CHANGED
@@ -60,4 +60,5 @@ const parseQuotes = (text, config = Parser.getConfig(), accum = []) => {
60
60
  return arr.join('');
61
61
  };
62
62
 
63
+ Parser.parsers.parseQuotes = __filename;
63
64
  module.exports = parseQuotes;