wikiparser-node 1.9.2-b → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +18 -18
  2. package/config/.schema.json +181 -0
  3. package/config/enwiki.json +814 -1
  4. package/config/llwiki.json +35 -1
  5. package/config/moegirl.json +44 -1
  6. package/config/zhwiki.json +466 -1
  7. package/dist/addon/table.js +486 -0
  8. package/dist/addon/token.js +240 -0
  9. package/dist/addon/transclude.js +185 -0
  10. package/dist/base.d.ts +73 -0
  11. package/dist/base.js +39 -0
  12. package/dist/index.d.ts +31 -0
  13. package/dist/index.js +232 -0
  14. package/dist/internal.d.ts +46 -0
  15. package/dist/lib/element.d.ts +124 -0
  16. package/dist/lib/element.js +600 -0
  17. package/dist/lib/node.d.ts +166 -0
  18. package/dist/lib/node.js +472 -0
  19. package/dist/lib/range.d.ts +105 -0
  20. package/dist/lib/range.js +406 -0
  21. package/dist/lib/ranges.d.ts +28 -0
  22. package/dist/lib/ranges.js +126 -0
  23. package/dist/lib/rect.d.ts +18 -0
  24. package/dist/lib/rect.js +34 -0
  25. package/dist/lib/text.d.ts +54 -0
  26. package/dist/lib/text.js +345 -0
  27. package/dist/lib/title.d.ts +40 -0
  28. package/dist/lib/title.js +197 -0
  29. package/dist/mixin/attributesParent.d.ts +49 -0
  30. package/dist/mixin/attributesParent.js +82 -0
  31. package/dist/mixin/fixed.d.ts +5 -0
  32. package/dist/mixin/fixed.js +32 -0
  33. package/dist/mixin/flagsParent.d.ts +41 -0
  34. package/dist/mixin/flagsParent.js +60 -0
  35. package/dist/mixin/hidden.d.ts +6 -0
  36. package/dist/mixin/hidden.js +29 -0
  37. package/dist/mixin/magicLinkParent.d.ts +19 -0
  38. package/dist/mixin/magicLinkParent.js +43 -0
  39. package/dist/mixin/singleLine.d.ts +5 -0
  40. package/dist/mixin/singleLine.js +27 -0
  41. package/dist/mixin/sol.d.ts +5 -0
  42. package/dist/mixin/sol.js +44 -0
  43. package/dist/mixin/syntax.d.ts +8 -0
  44. package/dist/mixin/syntax.js +47 -0
  45. package/dist/parser/braces.js +150 -0
  46. package/dist/parser/commentAndExt.js +79 -0
  47. package/dist/parser/converter.js +39 -0
  48. package/dist/parser/externalLinks.js +37 -0
  49. package/dist/parser/hrAndDoubleUnderscore.js +42 -0
  50. package/dist/parser/html.js +38 -0
  51. package/dist/parser/links.js +101 -0
  52. package/dist/parser/list.js +97 -0
  53. package/dist/parser/magicLinks.js +53 -0
  54. package/dist/parser/quotes.js +67 -0
  55. package/dist/parser/redirect.js +26 -0
  56. package/dist/parser/selector.js +190 -0
  57. package/dist/parser/table.js +123 -0
  58. package/dist/src/arg.d.ts +52 -0
  59. package/dist/src/arg.js +213 -0
  60. package/dist/src/atom.d.ts +12 -0
  61. package/dist/src/atom.js +26 -0
  62. package/dist/src/attribute.d.ts +65 -0
  63. package/dist/src/attribute.js +483 -0
  64. package/dist/src/attributes.d.ts +103 -0
  65. package/dist/src/attributes.js +350 -0
  66. package/dist/src/converter.d.ts +29 -0
  67. package/dist/src/converter.js +134 -0
  68. package/dist/src/converterFlags.d.ts +81 -0
  69. package/dist/src/converterFlags.js +239 -0
  70. package/dist/src/converterRule.d.ts +73 -0
  71. package/dist/src/converterRule.js +210 -0
  72. package/dist/src/extLink.d.ts +37 -0
  73. package/dist/src/extLink.js +191 -0
  74. package/dist/src/gallery.d.ts +48 -0
  75. package/dist/src/gallery.js +139 -0
  76. package/dist/src/heading.d.ts +41 -0
  77. package/dist/src/heading.js +185 -0
  78. package/dist/src/hidden.d.ts +10 -0
  79. package/dist/src/hidden.js +79 -0
  80. package/dist/src/html.d.ts +58 -0
  81. package/dist/src/html.js +334 -0
  82. package/dist/src/imageParameter.d.ts +59 -0
  83. package/dist/src/imageParameter.js +246 -0
  84. package/dist/src/imagemap.d.ts +48 -0
  85. package/dist/src/imagemap.js +145 -0
  86. package/dist/src/imagemapLink.d.ts +31 -0
  87. package/dist/src/imagemapLink.js +93 -0
  88. package/dist/src/index.d.ts +130 -0
  89. package/dist/src/index.js +721 -0
  90. package/dist/src/link/base.d.ts +46 -0
  91. package/dist/src/link/base.js +224 -0
  92. package/dist/src/link/category.d.ts +22 -0
  93. package/dist/src/link/category.js +30 -0
  94. package/dist/src/link/file.d.ts +96 -0
  95. package/dist/src/link/file.js +274 -0
  96. package/dist/src/link/galleryImage.d.ts +30 -0
  97. package/dist/src/link/galleryImage.js +158 -0
  98. package/dist/src/link/index.d.ts +52 -0
  99. package/dist/src/link/index.js +133 -0
  100. package/dist/src/link/redirectTarget.d.ts +37 -0
  101. package/dist/src/link/redirectTarget.js +95 -0
  102. package/dist/src/magicLink.d.ts +60 -0
  103. package/dist/src/magicLink.js +270 -0
  104. package/dist/src/nested.d.ts +41 -0
  105. package/dist/src/nested.js +105 -0
  106. package/dist/src/nowiki/base.d.ts +31 -0
  107. package/dist/src/nowiki/base.js +91 -0
  108. package/dist/src/nowiki/comment.d.ts +15 -0
  109. package/dist/src/nowiki/comment.js +123 -0
  110. package/dist/src/nowiki/dd.d.ts +8 -0
  111. package/dist/src/nowiki/dd.js +24 -0
  112. package/dist/src/nowiki/doubleUnderscore.d.ts +16 -0
  113. package/dist/src/nowiki/doubleUnderscore.js +96 -0
  114. package/dist/src/nowiki/hr.d.ts +5 -0
  115. package/dist/src/nowiki/hr.js +63 -0
  116. package/dist/src/nowiki/index.d.ts +14 -0
  117. package/dist/src/nowiki/index.js +29 -0
  118. package/dist/src/nowiki/list.d.ts +16 -0
  119. package/dist/src/nowiki/list.js +109 -0
  120. package/dist/src/nowiki/listBase.d.ts +5 -0
  121. package/dist/src/nowiki/listBase.js +61 -0
  122. package/dist/src/nowiki/noinclude.d.ts +10 -0
  123. package/dist/src/nowiki/noinclude.js +73 -0
  124. package/dist/src/nowiki/quote.d.ts +17 -0
  125. package/dist/src/nowiki/quote.js +131 -0
  126. package/dist/src/onlyinclude.d.ts +14 -0
  127. package/dist/src/onlyinclude.js +57 -0
  128. package/dist/src/paramTag/index.d.ts +31 -0
  129. package/dist/src/paramTag/index.js +77 -0
  130. package/dist/src/paramTag/inputbox.d.ts +8 -0
  131. package/dist/src/paramTag/inputbox.js +22 -0
  132. package/dist/src/parameter.d.ts +66 -0
  133. package/dist/src/parameter.js +265 -0
  134. package/dist/src/pre.d.ts +29 -0
  135. package/dist/src/pre.js +59 -0
  136. package/dist/src/redirect.d.ts +31 -0
  137. package/dist/src/redirect.js +121 -0
  138. package/dist/src/syntax.d.ts +17 -0
  139. package/dist/src/syntax.js +84 -0
  140. package/dist/src/table/base.d.ts +29 -0
  141. package/dist/src/table/base.js +82 -0
  142. package/dist/src/table/index.d.ts +239 -0
  143. package/dist/src/table/index.js +502 -0
  144. package/dist/src/table/td.d.ts +78 -0
  145. package/dist/src/table/td.js +368 -0
  146. package/dist/src/table/tr.d.ts +30 -0
  147. package/dist/src/table/tr.js +57 -0
  148. package/dist/src/table/trBase.d.ts +49 -0
  149. package/dist/src/table/trBase.js +156 -0
  150. package/dist/src/tagPair/ext.d.ts +30 -0
  151. package/dist/src/tagPair/ext.js +201 -0
  152. package/dist/src/tagPair/include.d.ts +35 -0
  153. package/dist/src/tagPair/include.js +124 -0
  154. package/dist/src/tagPair/index.d.ts +23 -0
  155. package/dist/src/tagPair/index.js +128 -0
  156. package/dist/src/transclude.d.ts +160 -0
  157. package/dist/src/transclude.js +579 -0
  158. package/dist/util/constants.js +25 -0
  159. package/dist/util/debug.js +97 -0
  160. package/dist/util/diff.js +83 -0
  161. package/dist/util/lint.js +32 -0
  162. package/dist/util/string.js +60 -0
  163. package/errors/README +1 -0
  164. package/package.json +20 -30
  165. package/printed/README +1 -0
  166. package/README.en.md +0 -57
  167. package/bundle/bundle.min.js +0 -38
  168. package/extensions/dist/base.js +0 -64
  169. package/extensions/dist/editor.js +0 -159
  170. package/extensions/dist/highlight.js +0 -58
  171. package/extensions/dist/lint.js +0 -72
  172. package/extensions/editor.css +0 -64
  173. package/extensions/ui.css +0 -144
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BoundingRect = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ /** 节点位置 */
6
+ class BoundingRect {
7
+ #pos;
8
+ token;
9
+ start;
10
+ /** 起点行 */
11
+ get top() {
12
+ this.#pos ??= this.getPosition();
13
+ return this.#pos.top;
14
+ }
15
+ /** 起点列 */
16
+ get left() {
17
+ this.#pos ??= this.getPosition();
18
+ return this.#pos.left;
19
+ }
20
+ /**
21
+ * @param token 节点
22
+ * @param start 起点
23
+ */
24
+ constructor(token, start) {
25
+ this.token = token;
26
+ this.start = start;
27
+ }
28
+ /** 计算位置 */
29
+ getPosition() {
30
+ return this.token.getRootNode().posFromIndex(this.start);
31
+ }
32
+ }
33
+ exports.BoundingRect = BoundingRect;
34
+ constants_1.classes['BoundingRect'] = __filename;
@@ -0,0 +1,54 @@
1
+ import { AstNode } from './node';
2
+ import type { LintError } from '../base';
3
+ /** 文本节点 */
4
+ export declare class AstText extends AstNode {
5
+ #private;
6
+ readonly type = "text";
7
+ readonly name: undefined;
8
+ readonly data: string;
9
+ /** 文本长度 */
10
+ get length(): number;
11
+ /** @param text 包含文本 */
12
+ constructor(text: string);
13
+ /** 可见部分 */
14
+ text(): string;
15
+ /**
16
+ * 替换字符串
17
+ * @param text 替换的字符串
18
+ */
19
+ replaceData(text: string): void;
20
+ /** 复制 */
21
+ cloneNode(): AstText;
22
+ /**
23
+ * 在后方添加字符串
24
+ * @param text 添加的字符串
25
+ */
26
+ appendData(text: string): void;
27
+ /**
28
+ * 删减字符串
29
+ * @param offset 起始位置
30
+ * @param count 删减字符数
31
+ */
32
+ deleteData(offset: number, count: number): void;
33
+ /**
34
+ * 插入字符串
35
+ * @param offset 插入位置
36
+ * @param text 待插入的字符串
37
+ */
38
+ insertData(offset: number, text: string): void;
39
+ /**
40
+ * 提取子串
41
+ * @param offset 起始位置
42
+ * @param count 字符数
43
+ */
44
+ substringData(offset: number, count: number): string;
45
+ /**
46
+ * 将文本子节点分裂为两部分
47
+ * @param offset 分裂位置
48
+ * @throws `RangeError` 错误的断开位置
49
+ * @throws `Error` 没有父节点
50
+ */
51
+ splitText(offset: number): AstText;
52
+ /** 转义 `=` */
53
+ escape(): void;
54
+ }
@@ -0,0 +1,345 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AstText = void 0;
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 node_1 = require("./node");
9
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
10
+ /<\s*(?:\/\s*)?([a-z]\w*)|\{+|\}+|\[{2,}|\[(?![^[]*?\])|((?:^|\])[^[]*?)\]+|https?[:/]\/+/giu;
11
+ /^https?:\/\/(?:\[[\da-f:.]+\]|[^[\]<>"\t\n\p{Zs}])[^[\]<>"\t\n\p{Zs}]*\.(?:gif|png|jpg|jpeg)$/iu;
12
+ /* eslint-enable @typescript-eslint/no-unused-expressions */
13
+ const source = String.raw `<\s*(?:/\s*)?([a-z]\w*)|\{+|\}+|\[{2,}|\[(?![^[]*?\])|((?:^|\])[^[]*?)\]+`, errorSyntax = new RegExp(String.raw `${source}|https?[:/]/+`, 'giu'), errorSyntaxUrl = new RegExp(source, 'giu'), extImage = new RegExp(String.raw `^https?://${string_1.extUrlCharFirst}${string_1.extUrlChar}\.(?:gif|png|jpg|jpeg)$`, 'iu'), regexes = {
14
+ '[': /[[\]]/u,
15
+ '{': /[{}]/u,
16
+ ']': /[[\]](?=[^[\]]*$)/u,
17
+ '}': /[{}](?=[^{}]*$)/u,
18
+ }, ruleMap = {
19
+ '<': 'tag-like',
20
+ '[': 'lonely-bracket',
21
+ '{': 'lonely-bracket',
22
+ ']': 'lonely-bracket',
23
+ '}': 'lonely-bracket',
24
+ h: 'lonely-http',
25
+ }, disallowedTags = [
26
+ 'html',
27
+ 'base',
28
+ 'head',
29
+ 'style',
30
+ 'title',
31
+ 'body',
32
+ 'menu',
33
+ 'a',
34
+ 'area',
35
+ 'audio',
36
+ 'img',
37
+ 'map',
38
+ 'track',
39
+ 'video',
40
+ 'embed',
41
+ 'iframe',
42
+ 'object',
43
+ 'picture',
44
+ 'source',
45
+ 'canvas',
46
+ 'script',
47
+ 'col',
48
+ 'colgroup',
49
+ 'tbody',
50
+ 'tfoot',
51
+ 'thead',
52
+ 'button',
53
+ 'datalist',
54
+ 'fieldset',
55
+ 'form',
56
+ 'input',
57
+ 'label',
58
+ 'legend',
59
+ 'meter',
60
+ 'optgroup',
61
+ 'option',
62
+ 'output',
63
+ 'progress',
64
+ 'select',
65
+ 'textarea',
66
+ 'details',
67
+ 'dialog',
68
+ 'slot',
69
+ 'template',
70
+ 'dir',
71
+ 'frame',
72
+ 'frameset',
73
+ 'marquee',
74
+ 'param',
75
+ 'xmp',
76
+ ];
77
+ /** 文本节点 */
78
+ class AstText extends node_1.AstNode {
79
+ type = 'text';
80
+ data = '';
81
+ /* NOT FOR BROWSER */
82
+ /** 文本长度 */
83
+ get length() {
84
+ return this.data.length;
85
+ }
86
+ /* NOT FOR BROWSER END */
87
+ /** @param text 包含文本 */
88
+ constructor(text) {
89
+ super();
90
+ Object.defineProperties(this, {
91
+ childNodes: { enumerable: false, configurable: false },
92
+ type: { enumerable: false, writable: false },
93
+ data: {
94
+ value: text,
95
+ /* NOT FOR BROWSER */
96
+ writable: false,
97
+ },
98
+ });
99
+ }
100
+ /** @private */
101
+ toString() {
102
+ return this.data;
103
+ }
104
+ /** 可见部分 */
105
+ text() {
106
+ return this.data;
107
+ }
108
+ /** @private */
109
+ lint(start = this.getAbsoluteIndex(), errorRegex) {
110
+ const { data, parentNode, nextSibling, previousSibling } = this;
111
+ if (!parentNode) {
112
+ throw new Error('An isolated text node cannot be linted!');
113
+ }
114
+ const { type, name, parentNode: grandparent } = parentNode;
115
+ let isHtmlAttrVal = false;
116
+ if (type === 'attr-value') {
117
+ const { type: grandType, name: grandName, tag } = grandparent;
118
+ if (grandType !== 'ext-attr') {
119
+ isHtmlAttrVal = true;
120
+ }
121
+ else if (tag === 'choose' && (grandName === 'before' || grandName === 'after')) {
122
+ return [];
123
+ }
124
+ }
125
+ errorRegex ??= type === 'free-ext-link'
126
+ || type === 'ext-link-url'
127
+ || type === 'image-parameter' && name === 'link'
128
+ || isHtmlAttrVal
129
+ ? errorSyntaxUrl
130
+ : errorSyntax;
131
+ if (data.search(errorRegex) === -1) {
132
+ return [];
133
+ }
134
+ errorRegex.lastIndex = 0;
135
+ const errors = [], nextType = nextSibling?.type, nextName = nextSibling?.name, previousType = previousSibling?.type, root = this.getRootNode(), { ext, html } = root.getAttribute('config'), { top, left } = root.posFromIndex(start), tags = new Set(['onlyinclude', 'noinclude', 'includeonly', ext, html, disallowedTags].flat(2));
136
+ for (let mt = errorRegex.exec(data); mt; mt = errorRegex.exec(data)) {
137
+ const [, tag, prefix] = mt;
138
+ let { 0: error, index } = mt;
139
+ if (prefix && prefix !== ']') {
140
+ const { length } = prefix;
141
+ index += length;
142
+ error = error.slice(length);
143
+ }
144
+ const { 0: char, length } = error;
145
+ if (char === '<' && !tags.has(tag.toLowerCase())
146
+ || char === '['
147
+ && type === 'ext-link-text'
148
+ && (/&(?:rbrack|#93|#x5[Dd];);/u.test(data.slice(index + 1))
149
+ || nextType === 'ext'
150
+ && nextName === 'nowiki'
151
+ && nextSibling.innerText?.includes(']'))) {
152
+ continue;
153
+ }
154
+ else if (char === ']' && (index || length > 1)) {
155
+ errorRegex.lastIndex--;
156
+ }
157
+ else if (char === 'h' && index === 0 && type === 'ext-link-text' && extImage.test(data)) {
158
+ continue;
159
+ }
160
+ const startIndex = start + index, endIndex = startIndex + length, rootStr = root.toString(), nextChar = rootStr[endIndex], previousChar = rootStr[startIndex - 1], severity = length > 1 && !(char === '<' && !/[\s/>]/u.test(nextChar ?? '')
161
+ || isHtmlAttrVal && (char === '[' || char === ']'))
162
+ || char === '{' && (nextChar === char || previousChar === '-')
163
+ || char === '}' && (previousChar === char || nextChar === '-')
164
+ || char === '[' && (nextChar === char
165
+ || type === 'ext-link-text'
166
+ || nextType === 'free-ext-link' && !data.slice(index + 1).trim())
167
+ || char === ']' && (previousChar === char
168
+ || previousType === 'free-ext-link' && !data.slice(0, index).includes(']'))
169
+ ? 'error'
170
+ : 'warning';
171
+ const leftBracket = char === '{' || char === '[', rightBracket = char === ']' || char === '}';
172
+ if (severity === 'warning' && (leftBracket || rightBracket)) {
173
+ const regex = regexes[char], remains = leftBracket ? data.slice(index + 1) : data.slice(0, index);
174
+ if (char === '{' && regex.exec(remains)?.[0] === '}'
175
+ || char === '}' && regex.exec(remains)?.[0] === '{') {
176
+ continue;
177
+ }
178
+ else if (!remains.includes(char)) {
179
+ const sibling = leftBracket ? 'nextSibling' : 'previousSibling';
180
+ let cur = this[sibling];
181
+ while (cur && (cur.type !== 'text' || !regex.test(cur.data))) {
182
+ cur = cur[sibling];
183
+ }
184
+ if (cur && regex.exec(cur.data)[0] !== char) {
185
+ continue;
186
+ }
187
+ }
188
+ }
189
+ const lines = data.slice(0, index).split('\n'), startLine = lines.length + top - 1, line = lines[lines.length - 1], startCol = lines.length === 1 ? left + line.length : line.length, e = {
190
+ rule: ruleMap[char],
191
+ message: index_1.default.msg('lonely "$1"', char === 'h' ? error : char),
192
+ severity,
193
+ startIndex,
194
+ endIndex,
195
+ startLine,
196
+ endLine: startLine,
197
+ startCol,
198
+ endCol: startCol + length,
199
+ };
200
+ if (char === '<') {
201
+ e.suggestions = [
202
+ {
203
+ desc: 'escape',
204
+ range: [startIndex, startIndex + 1],
205
+ text: '&lt;',
206
+ },
207
+ ];
208
+ }
209
+ else if (char === 'h'
210
+ && !(type === 'ext-link-text' || type === 'link-text')
211
+ && /[\p{L}\d_]/u.test(previousChar || '')) {
212
+ e.suggestions = [
213
+ {
214
+ desc: 'whitespace',
215
+ range: [startIndex, startIndex],
216
+ text: ' ',
217
+ },
218
+ ];
219
+ }
220
+ else if (char === '[' && type === 'ext-link-text') {
221
+ const i = parentNode.getAbsoluteIndex() + parentNode.toString().length;
222
+ e.suggestions = [
223
+ {
224
+ desc: 'escape',
225
+ range: [i, i + 1],
226
+ text: '&#93;',
227
+ },
228
+ ];
229
+ }
230
+ else if (char === ']' && previousType === 'free-ext-link' && severity === 'error') {
231
+ const i = start - previousSibling.toString().length;
232
+ e.fix = {
233
+ range: [i, i],
234
+ text: '[',
235
+ };
236
+ }
237
+ errors.push(e);
238
+ }
239
+ return errors;
240
+ }
241
+ /**
242
+ * 修改内容
243
+ * @param text 新内容
244
+ */
245
+ #setData(text) {
246
+ /* NOT FOR BROWSER */
247
+ const { data } = this, e = new Event('text', { bubbles: true });
248
+ /* NOT FOR BROWSER END */
249
+ this.setAttribute('data', text);
250
+ /* NOT FOR BROWSER */
251
+ if (data !== text) {
252
+ this.dispatchEvent(e, { type: 'text', oldText: data });
253
+ }
254
+ }
255
+ /**
256
+ * 替换字符串
257
+ * @param text 替换的字符串
258
+ */
259
+ replaceData(text) {
260
+ this.#setData(text);
261
+ }
262
+ /** @private */
263
+ print() {
264
+ return (0, string_1.escape)(this.data);
265
+ }
266
+ /* NOT FOR BROWSER */
267
+ /** 复制 */
268
+ cloneNode() {
269
+ return new AstText(this.data);
270
+ }
271
+ /**
272
+ * 在后方添加字符串
273
+ * @param text 添加的字符串
274
+ */
275
+ appendData(text) {
276
+ this.#setData(this.data + text);
277
+ }
278
+ /**
279
+ * 删减字符串
280
+ * @param offset 起始位置
281
+ * @param count 删减字符数
282
+ */
283
+ deleteData(offset, count) {
284
+ this.#setData(this.data.slice(0, offset) + (offset < 0 && offset + count >= 0 ? '' : this.data.slice(offset + count)));
285
+ }
286
+ /**
287
+ * 插入字符串
288
+ * @param offset 插入位置
289
+ * @param text 待插入的字符串
290
+ */
291
+ insertData(offset, text) {
292
+ this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
293
+ }
294
+ /**
295
+ * 提取子串
296
+ * @param offset 起始位置
297
+ * @param count 字符数
298
+ */
299
+ substringData(offset, count) {
300
+ return this.data.substr(offset, count);
301
+ }
302
+ /**
303
+ * 将文本子节点分裂为两部分
304
+ * @param offset 分裂位置
305
+ * @throws `RangeError` 错误的断开位置
306
+ * @throws `Error` 没有父节点
307
+ */
308
+ splitText(offset) {
309
+ if (offset > this.length || offset < -this.length) {
310
+ throw new RangeError(`Wrong offset to split: ${offset}`);
311
+ }
312
+ const { parentNode, data } = this;
313
+ if (!parentNode) {
314
+ throw new Error('The text node to be split has no parent node!');
315
+ }
316
+ const newText = new AstText(data.slice(offset));
317
+ (0, debug_1.setChildNodes)(parentNode, parentNode.childNodes.indexOf(this) + 1, 0, [newText]);
318
+ this.setAttribute('data', data.slice(0, offset));
319
+ return newText;
320
+ }
321
+ /** @private */
322
+ getRelativeIndex(j) {
323
+ if (j === undefined) {
324
+ return super.getRelativeIndex();
325
+ }
326
+ else if (j < 0 || j > this.length) {
327
+ throw new RangeError('Exceeding the text length range!');
328
+ }
329
+ return j;
330
+ }
331
+ /** 转义 `=` */
332
+ escape() {
333
+ const { TranscludeToken } = require('../src/transclude');
334
+ for (let i = this.data.lastIndexOf('='); i >= 0; i = this.data.lastIndexOf('=', i - 1)) {
335
+ if (i < this.length - 1) {
336
+ this.splitText(i + 1);
337
+ }
338
+ // @ts-expect-error abstract class
339
+ this.after(new TranscludeToken('=', [], this.parentNode.getAttribute('config')));
340
+ this.#setData(this.data.slice(0, i));
341
+ }
342
+ }
343
+ }
344
+ exports.AstText = AstText;
345
+ constants_1.classes['AstText'] = __filename;
@@ -0,0 +1,40 @@
1
+ import type { Config } from '../base';
2
+ /** MediaWiki页面标题对象 */
3
+ export declare class Title {
4
+ #private;
5
+ ns: number;
6
+ fragment: string | undefined;
7
+ interwiki: string;
8
+ readonly valid: boolean;
9
+ /** 不含命名空间的标题主体部分 */
10
+ get main(): string;
11
+ set main(title: string);
12
+ /** 命名空间前缀 */
13
+ get prefix(): string;
14
+ /** 完整标题 */
15
+ get title(): string;
16
+ /** 扩展名 */
17
+ get extension(): string | undefined;
18
+ set extension(extension: string | undefined);
19
+ /**
20
+ * @see MediaWikiTitleCodec::splitTitleString
21
+ *
22
+ * @param title 标题(含或不含命名空间前缀)
23
+ * @param defaultNs 命名空间
24
+ * @param decode 是否需要解码
25
+ * @param selfLink 是否允许selfLink
26
+ */
27
+ constructor(title: string, defaultNs: number, config: Config, decode: boolean, selfLink: boolean);
28
+ /** 执行单向转换 */
29
+ autoConvert(): void;
30
+ /** 转换为主页面 */
31
+ toSubjectPage(): void;
32
+ /** 转换为讨论页面 */
33
+ toTalkPage(): void;
34
+ /** 是否是讨论页 */
35
+ isTalkPage(): boolean;
36
+ /** 转换为上一级页面 */
37
+ toBasePage(): void;
38
+ /** 转换为根页面 */
39
+ toRootPage(): void;
40
+ }
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Title = void 0;
4
+ const string_1 = require("../util/string");
5
+ const constants_1 = require("../util/constants");
6
+ const index_1 = require("../index");
7
+ /**
8
+ * PHP的`rawurldecode`函数的JavaScript实现
9
+ * @param str 要解码的字符串
10
+ */
11
+ const rawurldecode = (str) => decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, '%25'));
12
+ /** MediaWiki页面标题对象 */
13
+ class Title {
14
+ #main;
15
+ #namespaces;
16
+ ns;
17
+ fragment;
18
+ interwiki = '';
19
+ valid;
20
+ /** @private */
21
+ encoded = false;
22
+ /* NOT FOR BROWSER */
23
+ #fragment;
24
+ /** @private */
25
+ conversionTable = new Map();
26
+ /** @private */
27
+ redirects = new Map();
28
+ /* NOT FOR BROWSER END */
29
+ /** 不含命名空间的标题主体部分 */
30
+ get main() {
31
+ return this.#main;
32
+ }
33
+ set main(title) {
34
+ title = title.replace(/_/gu, ' ').trim();
35
+ this.#main = title && title[0].toUpperCase() + title.slice(1);
36
+ }
37
+ /** 命名空间前缀 */
38
+ get prefix() {
39
+ const namespace = this.#namespaces[this.ns];
40
+ return namespace + (namespace && ':');
41
+ }
42
+ /** 完整标题 */
43
+ get title() {
44
+ const prefix = this.interwiki + (this.interwiki && ':') + this.prefix;
45
+ let title = (prefix + this.main).replace(/ /gu, '_');
46
+ /* NOT FOR BROWSER */
47
+ let redirected = this.#redirect(title);
48
+ if (redirected) {
49
+ return redirected;
50
+ }
51
+ this.autoConvert();
52
+ title = (prefix + this.main).replace(/ /gu, '_');
53
+ redirected = this.#redirect(title);
54
+ if (redirected) {
55
+ return redirected;
56
+ }
57
+ /* NOT FOR BROWSER END */
58
+ return title;
59
+ }
60
+ /** 扩展名 */
61
+ get extension() {
62
+ const { main } = this, i = main.lastIndexOf('.');
63
+ return i === -1 ? undefined : main.slice(i + 1).toLowerCase();
64
+ }
65
+ /* NOT FOR BROWSER */
66
+ set extension(extension) {
67
+ extension ||= '';
68
+ const { main } = this, i = main.lastIndexOf('.');
69
+ this.main = (i === -1 ? main : main.slice(0, i)) + (extension && '.') + extension;
70
+ }
71
+ /* NOT FOR BROWSER END */
72
+ /**
73
+ * @see MediaWikiTitleCodec::splitTitleString
74
+ *
75
+ * @param title 标题(含或不含命名空间前缀)
76
+ * @param defaultNs 命名空间
77
+ * @param decode 是否需要解码
78
+ * @param selfLink 是否允许selfLink
79
+ */
80
+ constructor(title, defaultNs, config, decode, selfLink) {
81
+ const subpage = title.trim().startsWith('../');
82
+ title = (0, string_1.decodeHtml)(title);
83
+ if (decode && title.includes('%')) {
84
+ try {
85
+ const encoded = /%(?!21|3[ce]|5[bd]|7[b-d])[\da-f]{2}/iu.test(title);
86
+ title = rawurldecode(title);
87
+ this.encoded = encoded;
88
+ }
89
+ catch { }
90
+ }
91
+ title = title.replace(/_/gu, ' ').trim();
92
+ if (subpage) {
93
+ this.ns = 0;
94
+ }
95
+ else {
96
+ let ns = defaultNs;
97
+ if (title.startsWith(':')) {
98
+ ns = 0;
99
+ title = title.slice(1).trim();
100
+ }
101
+ /* NOT FOR BROWSER */
102
+ const iw = defaultNs ? null : index_1.default.isInterwiki(title, config);
103
+ if (iw) {
104
+ this.interwiki = iw[1].toLowerCase();
105
+ title = title.slice(iw.indices[0][1]);
106
+ }
107
+ /* NOT FOR BROWSER END */
108
+ const m = title.split(':');
109
+ if (m.length > 1) {
110
+ const id = config.nsid[m[0].trim().toLowerCase()];
111
+ if (id) {
112
+ ns = id;
113
+ title = m.slice(1).join(':').trim();
114
+ }
115
+ }
116
+ this.ns = ns;
117
+ }
118
+ const i = title.indexOf('#');
119
+ if (i !== -1) {
120
+ let fragment = title.slice(i + 1).trimEnd();
121
+ if (fragment.includes('%')) {
122
+ try {
123
+ fragment = rawurldecode(fragment);
124
+ }
125
+ catch { }
126
+ }
127
+ this.fragment = fragment;
128
+ title = title.slice(0, i).trim();
129
+ }
130
+ this.valid = Boolean(title || this.interwiki || selfLink && this.ns === 0 && this.fragment !== undefined)
131
+ && !/^:|\0\d+[eh!+-]\x7F|[<>[\]{}|\n]|%[\da-f]{2}|(?:^|\/)\.{1,2}(?:$|\/)/iu.test(subpage ? /^(?:\.\.\/)+(.*)/u.exec(title)[1] : title);
132
+ this.main = title;
133
+ Object.defineProperties(this, {
134
+ encoded: { enumerable: false, writable: false },
135
+ /* NOT FOR BROWSER */
136
+ valid: { writable: false },
137
+ conversionTable: { enumerable: false },
138
+ redirects: { enumerable: false },
139
+ });
140
+ this.#namespaces = config.namespaces;
141
+ }
142
+ /** @private */
143
+ toString() {
144
+ return `${this.title}${this.fragment === undefined
145
+ && this.#fragment === undefined
146
+ ? ''
147
+ : `#${this.fragment
148
+ ?? this.#fragment}`}`;
149
+ }
150
+ /* NOT FOR BROWSER */
151
+ /**
152
+ * 处理重定向
153
+ * @param title 原标题
154
+ */
155
+ #redirect(title) {
156
+ const redirected = this.redirects.get(title);
157
+ if (redirected) {
158
+ [title, this.#fragment] = redirected.split('#', 2);
159
+ return title;
160
+ }
161
+ return '';
162
+ }
163
+ /** 执行单向转换 */
164
+ autoConvert() {
165
+ const { conversionTable } = this;
166
+ if (conversionTable.size > 0) {
167
+ const regex = new RegExp([...conversionTable.keys()].sort().reverse().map(string_1.escapeRegExp).join('|'), 'gu');
168
+ this.main = this.main.replace(regex, p => conversionTable.get(p));
169
+ }
170
+ }
171
+ /** 转换为主页面 */
172
+ toSubjectPage() {
173
+ if (this.isTalkPage()) {
174
+ this.ns--;
175
+ }
176
+ }
177
+ /** 转换为讨论页面 */
178
+ toTalkPage() {
179
+ if (!this.isTalkPage()) {
180
+ this.ns++;
181
+ }
182
+ }
183
+ /** 是否是讨论页 */
184
+ isTalkPage() {
185
+ return this.ns % 2 === 1;
186
+ }
187
+ /** 转换为上一级页面 */
188
+ toBasePage() {
189
+ this.main = this.main.replace(/\/[^/]*$/u, '');
190
+ }
191
+ /** 转换为根页面 */
192
+ toRootPage() {
193
+ this.main = this.main.replace(/\/.*/u, '');
194
+ }
195
+ }
196
+ exports.Title = Title;
197
+ constants_1.classes['Title'] = __filename;