wikiparser-node 0.11.0 → 1.0.0-b

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 (179) hide show
  1. package/README.md +10 -9
  2. package/bundle/bundle.min.js +37 -0
  3. package/config/llwiki.json +1 -35
  4. package/config/minimum.json +136 -0
  5. package/config/moegirl.json +1 -44
  6. package/config/zhwiki.json +1 -466
  7. package/extensions/dist/base.js +105 -0
  8. package/extensions/dist/editor.js +172 -0
  9. package/extensions/dist/highlight.js +46 -0
  10. package/extensions/dist/lint.js +43 -0
  11. package/extensions/editor.css +63 -0
  12. package/extensions/ui.css +119 -0
  13. package/i18n/zh-hans.json +1 -0
  14. package/i18n/zh-hant.json +1 -0
  15. package/package.json +38 -31
  16. package/config/.schema.json +0 -134
  17. package/dist/index.d.ts +0 -114
  18. package/dist/lib/element.d.ts +0 -162
  19. package/dist/lib/node.d.ts +0 -291
  20. package/dist/lib/ranges.d.ts +0 -37
  21. package/dist/lib/text.d.ts +0 -64
  22. package/dist/lib/title.d.ts +0 -21
  23. package/dist/mixin/attributeParent.d.ts +0 -9
  24. package/dist/mixin/fixedToken.d.ts +0 -8
  25. package/dist/mixin/hidden.d.ts +0 -8
  26. package/dist/mixin/singleLine.d.ts +0 -8
  27. package/dist/mixin/sol.d.ts +0 -8
  28. package/dist/parser/brackets.d.ts +0 -12
  29. package/dist/parser/commentAndExt.d.ts +0 -8
  30. package/dist/parser/converter.d.ts +0 -7
  31. package/dist/parser/externalLinks.d.ts +0 -7
  32. package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
  33. package/dist/parser/html.d.ts +0 -7
  34. package/dist/parser/links.d.ts +0 -7
  35. package/dist/parser/list.d.ts +0 -7
  36. package/dist/parser/magicLinks.d.ts +0 -7
  37. package/dist/parser/quotes.d.ts +0 -7
  38. package/dist/parser/selector.d.ts +0 -12
  39. package/dist/parser/table.d.ts +0 -11
  40. package/dist/src/arg.d.ts +0 -54
  41. package/dist/src/atom/hidden.d.ts +0 -5
  42. package/dist/src/atom/index.d.ts +0 -15
  43. package/dist/src/attribute.d.ts +0 -65
  44. package/dist/src/attributes.d.ts +0 -112
  45. package/dist/src/charinsert.d.ts +0 -32
  46. package/dist/src/converter.d.ts +0 -103
  47. package/dist/src/converterFlags.d.ts +0 -83
  48. package/dist/src/converterRule.d.ts +0 -75
  49. package/dist/src/extLink.d.ts +0 -62
  50. package/dist/src/gallery.d.ts +0 -33
  51. package/dist/src/hasNowiki/index.d.ts +0 -14
  52. package/dist/src/hasNowiki/pre.d.ts +0 -13
  53. package/dist/src/heading.d.ts +0 -44
  54. package/dist/src/html.d.ts +0 -56
  55. package/dist/src/imageParameter.d.ts +0 -65
  56. package/dist/src/imagemap.d.ts +0 -37
  57. package/dist/src/imagemapLink.d.ts +0 -21
  58. package/dist/src/index.d.ts +0 -186
  59. package/dist/src/link/category.d.ts +0 -16
  60. package/dist/src/link/file.d.ts +0 -85
  61. package/dist/src/link/galleryImage.d.ts +0 -15
  62. package/dist/src/link/index.d.ts +0 -88
  63. package/dist/src/magicLink.d.ts +0 -36
  64. package/dist/src/nested/choose.d.ts +0 -13
  65. package/dist/src/nested/combobox.d.ts +0 -13
  66. package/dist/src/nested/index.d.ts +0 -18
  67. package/dist/src/nested/references.d.ts +0 -13
  68. package/dist/src/nowiki/comment.d.ts +0 -31
  69. package/dist/src/nowiki/dd.d.ts +0 -17
  70. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -22
  71. package/dist/src/nowiki/hr.d.ts +0 -13
  72. package/dist/src/nowiki/index.d.ts +0 -27
  73. package/dist/src/nowiki/list.d.ts +0 -8
  74. package/dist/src/nowiki/noinclude.d.ts +0 -8
  75. package/dist/src/nowiki/quote.d.ts +0 -13
  76. package/dist/src/onlyinclude.d.ts +0 -24
  77. package/dist/src/paramTag/index.d.ts +0 -29
  78. package/dist/src/paramTag/inputbox.d.ts +0 -8
  79. package/dist/src/parameter.d.ts +0 -75
  80. package/dist/src/syntax.d.ts +0 -20
  81. package/dist/src/table/index.d.ts +0 -273
  82. package/dist/src/table/td.d.ts +0 -100
  83. package/dist/src/table/tr.d.ts +0 -91
  84. package/dist/src/tagPair/ext.d.ts +0 -18
  85. package/dist/src/tagPair/include.d.ts +0 -25
  86. package/dist/src/tagPair/index.d.ts +0 -41
  87. package/dist/src/transclude.d.ts +0 -199
  88. package/dist/tool/index.d.ts +0 -420
  89. package/dist/util/base.d.ts +0 -10
  90. package/dist/util/debug.d.ts +0 -20
  91. package/dist/util/diff.d.ts +0 -8
  92. package/dist/util/lint.d.ts +0 -28
  93. package/dist/util/string.d.ts +0 -55
  94. package/index.js +0 -333
  95. package/lib/element.js +0 -618
  96. package/lib/node.js +0 -730
  97. package/lib/ranges.js +0 -130
  98. package/lib/text.js +0 -265
  99. package/lib/title.js +0 -83
  100. package/mixin/attributeParent.js +0 -117
  101. package/mixin/fixedToken.js +0 -40
  102. package/mixin/hidden.js +0 -21
  103. package/mixin/singleLine.js +0 -31
  104. package/mixin/sol.js +0 -54
  105. package/parser/brackets.js +0 -128
  106. package/parser/commentAndExt.js +0 -62
  107. package/parser/converter.js +0 -46
  108. package/parser/externalLinks.js +0 -33
  109. package/parser/hrAndDoubleUnderscore.js +0 -49
  110. package/parser/html.js +0 -42
  111. package/parser/links.js +0 -94
  112. package/parser/list.js +0 -59
  113. package/parser/magicLinks.js +0 -41
  114. package/parser/quotes.js +0 -64
  115. package/parser/selector.js +0 -180
  116. package/parser/table.js +0 -114
  117. package/src/arg.js +0 -207
  118. package/src/atom/hidden.js +0 -13
  119. package/src/atom/index.js +0 -43
  120. package/src/attribute.js +0 -472
  121. package/src/attributes.js +0 -453
  122. package/src/charinsert.js +0 -97
  123. package/src/converter.js +0 -176
  124. package/src/converterFlags.js +0 -284
  125. package/src/converterRule.js +0 -256
  126. package/src/extLink.js +0 -180
  127. package/src/gallery.js +0 -149
  128. package/src/hasNowiki/index.js +0 -44
  129. package/src/hasNowiki/pre.js +0 -40
  130. package/src/heading.js +0 -134
  131. package/src/html.js +0 -254
  132. package/src/imageParameter.js +0 -303
  133. package/src/imagemap.js +0 -199
  134. package/src/imagemapLink.js +0 -41
  135. package/src/index.js +0 -938
  136. package/src/link/category.js +0 -44
  137. package/src/link/file.js +0 -287
  138. package/src/link/galleryImage.js +0 -120
  139. package/src/link/index.js +0 -388
  140. package/src/magicLink.js +0 -151
  141. package/src/nested/choose.js +0 -24
  142. package/src/nested/combobox.js +0 -23
  143. package/src/nested/index.js +0 -96
  144. package/src/nested/references.js +0 -23
  145. package/src/nowiki/comment.js +0 -71
  146. package/src/nowiki/dd.js +0 -59
  147. package/src/nowiki/doubleUnderscore.js +0 -56
  148. package/src/nowiki/hr.js +0 -41
  149. package/src/nowiki/index.js +0 -56
  150. package/src/nowiki/list.js +0 -16
  151. package/src/nowiki/noinclude.js +0 -28
  152. package/src/nowiki/quote.js +0 -69
  153. package/src/onlyinclude.js +0 -64
  154. package/src/paramTag/index.js +0 -89
  155. package/src/paramTag/inputbox.js +0 -35
  156. package/src/parameter.js +0 -239
  157. package/src/syntax.js +0 -91
  158. package/src/table/index.js +0 -985
  159. package/src/table/td.js +0 -343
  160. package/src/table/tr.js +0 -319
  161. package/src/tagPair/ext.js +0 -146
  162. package/src/tagPair/include.js +0 -50
  163. package/src/tagPair/index.js +0 -131
  164. package/src/transclude.js +0 -843
  165. package/tool/index.js +0 -1209
  166. package/typings/api.d.ts +0 -9
  167. package/typings/array.d.ts +0 -29
  168. package/typings/event.d.ts +0 -22
  169. package/typings/index.d.ts +0 -118
  170. package/typings/node.d.ts +0 -35
  171. package/typings/parser.d.ts +0 -12
  172. package/typings/table.d.ts +0 -10
  173. package/typings/token.d.ts +0 -31
  174. package/typings/tool.d.ts +0 -6
  175. package/util/base.js +0 -17
  176. package/util/debug.js +0 -73
  177. package/util/diff.js +0 -76
  178. package/util/lint.js +0 -57
  179. package/util/string.js +0 -126
@@ -1,180 +0,0 @@
1
- 'use strict';
2
-
3
- /** @typedef {import('../typings/parser').SelectorArray} SelectorArray */
4
- /** @typedef {import('../typings/parser').pseudo} pseudo */
5
-
6
- const Parser = require('..');
7
-
8
- const /** @type {Set<pseudo>} */ simplePseudos = new Set([
9
- 'root',
10
- 'first-child',
11
- 'first-of-type',
12
- 'last-child',
13
- 'last-of-type',
14
- 'only-child',
15
- 'only-of-type',
16
- 'empty',
17
- 'parent',
18
- 'header',
19
- 'hidden',
20
- 'visible',
21
- 'only-whitespace',
22
- 'local-link',
23
- 'read-only',
24
- 'read-write',
25
- 'invalid',
26
- 'required',
27
- 'optional',
28
- ]),
29
- /** @type {pseudo[]} */ complexPseudos = [
30
- 'is',
31
- 'not',
32
- 'nth-child',
33
- 'nth-of-type',
34
- 'nth-last-child',
35
- 'nth-last-of-type',
36
- 'contains',
37
- 'has',
38
- 'lang',
39
- ],
40
- specialChars = [
41
- ['[', '&lbrack;'],
42
- [']', '&rbrack;'],
43
- ['(', '&lpar;'],
44
- [')', '&rpar;'],
45
- ['"', '&quot;'],
46
- [`'`, '&apos;'],
47
- [':', '&colon;'],
48
- ['\\', '&bsol;'],
49
- ['&', '&amp;'],
50
- ],
51
- pseudoRegex = new RegExp(`:(${complexPseudos.join('|')})$`, 'u'),
52
- regularRegex = /[[(,>+~]|\s+/u,
53
- attributeRegex = /^\s*(\w+)\s*(?:([~|^$*!]?=)\s*("[^"]*"|'[^']*'|[^\s[\]]+)(?:\s+(i))?\s*)?\]/u,
54
- functionRegex = /^(\s*"[^"]*"\s*|\s*'[^']*'\s*|[^()]*)\)/u,
55
- grouping = new Set([',', '>', '+', '~']),
56
- combinator = new Set(['>', '+', '~', '']);
57
-
58
- /**
59
- * 清理转义符号
60
- * @param {string} selector
61
- */
62
- const sanitize = selector => {
63
- for (const [c, escaped] of specialChars) {
64
- selector = selector.replaceAll(`\\${c}`, escaped);
65
- }
66
- return selector;
67
- };
68
-
69
- /**
70
- * 还原转义符号
71
- * @param {string|undefined} selector
72
- */
73
- const desanitize = selector => {
74
- if (selector === undefined) {
75
- return undefined;
76
- }
77
- for (const [c, escaped] of specialChars) {
78
- selector = selector.replaceAll(escaped, c);
79
- }
80
- return selector.trim();
81
- };
82
-
83
- /**
84
- * 去除首尾的引号
85
- * @param {string|undefined} val 属性值或伪选择器函数的参数
86
- */
87
- const deQuote = val => {
88
- if (val === undefined) {
89
- return undefined;
90
- }
91
- const quotes = /^(["']).*\1$/u.exec(val)?.[1];
92
- return quotes ? val.slice(1, -1) : val;
93
- };
94
-
95
- /**
96
- * 解析简单伪选择器
97
- * @param {SelectorArray} step 当前顶部
98
- * @param {string} str 不含属性和复杂伪选择器的语句
99
- * @throws `SyntaxError` 非法的选择器
100
- */
101
- const pushSimple = (step, str) => {
102
- const pieces = str.trim().split(':'),
103
- // eslint-disable-next-line unicorn/explicit-length-check
104
- i = pieces.slice(1).findIndex(pseudo => simplePseudos.has(pseudo)) + 1 || pieces.length;
105
- if (pieces.slice(i).some(pseudo => !simplePseudos.has(pseudo))) {
106
- throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
107
- }
108
- step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
109
- };
110
-
111
- /**
112
- * 解析选择器
113
- * @param {string} selector
114
- * @throws `SyntaxError` 非法的选择器
115
- */
116
- const parseSelector = selector => {
117
- selector = selector.trim();
118
- const /** @type {SelectorArray[][]} */ stack = [[[]]];
119
- let sanitized = sanitize(selector),
120
- regex = regularRegex,
121
- mt = regex.exec(sanitized),
122
- [condition] = stack,
123
- [step] = condition;
124
- while (mt) {
125
- let {0: syntax, index} = mt;
126
- if (syntax.trim() === '') {
127
- index += syntax.length;
128
- const char = sanitized[index];
129
- syntax = grouping.has(char) ? char : '';
130
- }
131
- if (syntax === ',') { // 情形1:并列
132
- pushSimple(step, sanitized.slice(0, index));
133
- condition = [[]];
134
- [step] = condition;
135
- stack.push(condition);
136
- } else if (combinator.has(syntax)) { // 情形2:关系
137
- pushSimple(step, sanitized.slice(0, index));
138
- if (!step.some(Boolean)) {
139
- throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
140
- }
141
- step.relation = syntax;
142
- step = [];
143
- condition.push(step);
144
- } else if (syntax === '[') { // 情形3:属性开启
145
- pushSimple(step, sanitized.slice(0, index));
146
- regex = attributeRegex;
147
- } else if (syntax.endsWith(']')) { // 情形4:属性闭合
148
- mt[3] = desanitize(deQuote(mt[3]));
149
- step.push(mt.slice(1));
150
- regex = regularRegex;
151
- } else if (syntax === '(') { // 情形5:伪选择器开启
152
- const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
153
- if (!pseudoExec) {
154
- throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
155
- }
156
- pushSimple(step, sanitized.slice(0, pseudoExec.index));
157
- step.push(pseudoExec[1]); // 临时存放复杂伪选择器
158
- regex = functionRegex;
159
- } else { // 情形6:伪选择器闭合
160
- const /** @type {pseudo} */ pseudo = step.pop();
161
- mt.push(pseudo);
162
- mt[1] = deQuote(mt[1]);
163
- step.push(mt.slice(1));
164
- regex = regularRegex;
165
- }
166
- sanitized = sanitized.slice(index + syntax.length);
167
- if (grouping.has(syntax)) {
168
- sanitized = sanitized.trim();
169
- }
170
- mt = regex.exec(sanitized);
171
- }
172
- if (regex === regularRegex) {
173
- pushSimple(step, sanitized);
174
- return stack;
175
- }
176
- throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
177
- };
178
-
179
- Parser.parsers.parseSelector = __filename;
180
- module.exports = parseSelector;
package/parser/table.js DELETED
@@ -1,114 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- AstText = require('../lib/text'),
5
- Token = require('../src'),
6
- TableToken = require('../src/table'),
7
- TrToken = require('../src/table/tr'),
8
- TdToken = require('../src/table/td'),
9
- DdToken = require('../src/nowiki/dd');
10
-
11
- /**
12
- * 解析表格,注意`tr`和`td`包含开头的换行
13
- * @param {Token & {firstChild: AstText}} root 根节点
14
- * @param {import('../typings/token').accum} accum
15
- */
16
- const parseTable = ({firstChild: {data}, type, name}, config = Parser.getConfig(), accum = []) => {
17
- const /** @type {TrToken[]} */ stack = [],
18
- lines = data.split('\n');
19
- let out = type === 'root' || type === 'parameter-value' || type === 'ext-inner' && name === 'poem'
20
- ? ''
21
- : `\n${lines.shift()}`;
22
-
23
- /**
24
- * 向表格中插入纯文本
25
- * @param {string} str 待插入的文本
26
- * @param {TrToken} top 当前解析的表格或表格行
27
- */
28
- const push = (str, top) => {
29
- if (!top) {
30
- out += str;
31
- return;
32
- }
33
- const /** @type {Token}} */ {lastChild} = top;
34
- if (lastChild.constructor === Token) {
35
- lastChild.setText(String(lastChild) + str);
36
- } else {
37
- const token = new Token(str, config, true, accum);
38
- token.type = 'table-inter';
39
- top.insertAt(token.setAttribute('stage', 3));
40
- }
41
- };
42
- for (const outLine of lines) {
43
- let top = stack.pop();
44
- const [spaces] = /^(?:\s|\0\d+c\x7F)*/u.exec(outLine),
45
- line = outLine.slice(spaces.length),
46
- matchesStart = /^(:*)((?:\s|\0\d+c\x7F)*)(\{\||\{(?:\0\d+c\x7F)*\0\d+!\x7F|\0\d+\{\x7F)(.*)$/u.exec(line);
47
- if (matchesStart) {
48
- while (top && top.type !== 'td') {
49
- top = stack.pop();
50
- }
51
- const [, indent, moreSpaces, tableSyntax, attr] = matchesStart;
52
- if (indent) {
53
- new DdToken(indent, config, accum);
54
- }
55
- push(`\n${spaces}${indent && `\0${accum.length - 1}d\x7F`}${moreSpaces}\0${accum.length}b\x7F`, top);
56
- const table = new TableToken(tableSyntax, attr, config, accum);
57
- stack.push(...top ? [top] : [], table);
58
- continue;
59
- } else if (!top) {
60
- out += `\n${outLine}`;
61
- continue;
62
- }
63
- const matches = // eslint-disable-line operator-linebreak
64
- /^(?:(\|\}|\0\d+!\x7F\}|\0\d+\}\x7F)|(\|-+|\0\d+!\x7F-+|\0\d+-\x7F-*)(?!-)|(!|(?:\||\0\d+!\x7F)\+?))(.*)$/u
65
- .exec(line);
66
- if (!matches) {
67
- push(`\n${outLine}`, top);
68
- stack.push(...top ? [top] : []);
69
- continue;
70
- }
71
- const [, closing, row, cell, attr] = matches;
72
- if (closing) {
73
- while (!(top instanceof TableToken)) {
74
- top = stack.pop();
75
- }
76
- top.close(`\n${spaces}${closing}`, true);
77
- push(attr, stack.at(-1));
78
- } else if (row) {
79
- if (top.type === 'td') {
80
- top = stack.pop();
81
- }
82
- if (top.type === 'tr') {
83
- top = stack.pop();
84
- }
85
- const tr = new TrToken(`\n${spaces}${row}`, attr, config, accum);
86
- stack.push(top, tr);
87
- top.insertAt(tr);
88
- } else {
89
- if (top.type === 'td') {
90
- top = stack.pop();
91
- }
92
- const regex = cell === '!'
93
- ? /!!|(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu
94
- : /(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu;
95
- let mt = regex.exec(attr),
96
- lastIndex = 0,
97
- lastSyntax = `\n${spaces}${cell}`;
98
- while (mt) {
99
- const td = new TdToken(lastSyntax, attr.slice(lastIndex, mt.index), config, accum);
100
- top.insertAt(td);
101
- ({lastIndex} = regex);
102
- [lastSyntax] = mt;
103
- mt = regex.exec(attr);
104
- }
105
- const td = new TdToken(lastSyntax, attr.slice(lastIndex), config, accum);
106
- stack.push(top, td);
107
- top.insertAt(td);
108
- }
109
- }
110
- return out.slice(1);
111
- };
112
-
113
- Parser.parsers.parseTable = __filename;
114
- module.exports = parseTable;
package/src/arg.js DELETED
@@ -1,207 +0,0 @@
1
- 'use strict';
2
-
3
- const {text, noWrap} = require('../util/string'),
4
- {generateForSelf, generateForChild} = require('../util/lint'),
5
- Parser = require('..'),
6
- Token = require('.');
7
-
8
- /**
9
- * `{{{}}}`包裹的参数
10
- * @classdesc `{childNodes: [AtomToken, ?Token, ...HiddenToken]}`
11
- */
12
- class ArgToken extends Token {
13
- type = 'arg';
14
-
15
- /** default */
16
- get default() {
17
- return this.childNodes[1]?.text() ?? false;
18
- }
19
-
20
- /**
21
- * @param {string[]} parts 以'|'分隔的各部分
22
- * @param {import('../typings/token').accum} accum
23
- * @complexity `n`
24
- */
25
- constructor(parts, config = Parser.getConfig(), accum = []) {
26
- super(undefined, config, true, accum, {
27
- AtomToken: 0, Token: 1, HiddenToken: '2:',
28
- });
29
- for (let i = 0; i < parts.length; i++) {
30
- if (i === 0 || i > 1) {
31
- const AtomToken = i === 0 ? require('./atom') : require('./atom/hidden');
32
- const token = new AtomToken(parts[i], i === 0 ? 'arg-name' : undefined, config, accum, {
33
- 'Stage-2': ':', '!HeadingToken': '',
34
- });
35
- super.insertAt(token);
36
- } else {
37
- const token = new Token(parts[i], config, true, accum);
38
- token.type = 'arg-default';
39
- super.insertAt(token.setAttribute('stage', 2));
40
- }
41
- }
42
- this.getAttribute('protectChildren')(0);
43
- }
44
-
45
- /**
46
- * @override
47
- * @param {string} selector
48
- */
49
- toString(selector) {
50
- return selector && this.matches(selector) ? '' : `{{{${super.toString(selector, '|')}}}}`;
51
- }
52
-
53
- /**
54
- * @override
55
- * @returns {string}
56
- */
57
- text() {
58
- return `{{{${text(this.childNodes.slice(0, 2), '|')}}}}`;
59
- }
60
-
61
- /** @override */
62
- getPadding() {
63
- return 3;
64
- }
65
-
66
- /** @override */
67
- getGaps() {
68
- return 1;
69
- }
70
-
71
- /** @override */
72
- print() {
73
- return super.print({pre: '{{{', post: '}}}', sep: '|'});
74
- }
75
-
76
- /**
77
- * @override
78
- * @param {number} start 起始位置
79
- * @returns {import('../typings/token').LintError[]}
80
- */
81
- lint(start = this.getAbsoluteIndex()) {
82
- if (!this.getAttribute('include')) {
83
- return [generateForSelf(this, {start}, 'unexpected template argument')];
84
- }
85
- const {childNodes: [argName, argDefault, ...rest]} = this,
86
- errors = argName.lint(start + 3);
87
- if (argDefault) {
88
- errors.push(...argDefault.lint(start + 4 + String(argName).length));
89
- }
90
- if (rest.length > 0) {
91
- const rect = {start, ...this.getRootNode().posFromIndex(start)};
92
- errors.push(...rest.map(child => {
93
- const error = generateForChild(child, rect, 'invisible content inside triple brackets'),
94
- {startIndex, startCol, excerpt} = error;
95
- return {...error, startIndex: startIndex - 1, startCol: startCol - 1, excerpt: `|${excerpt}`};
96
- }));
97
- }
98
- return errors;
99
- }
100
-
101
- /** @override */
102
- cloneNode() {
103
- const [name, ...cloned] = this.cloneChildNodes();
104
- return Parser.run(() => {
105
- const token = new ArgToken([''], this.getAttribute('config'));
106
- token.firstChild.safeReplaceWith(name);
107
- token.append(...cloned);
108
- token.afterBuild();
109
- return token;
110
- });
111
- }
112
-
113
- /** @override */
114
- afterBuild() {
115
- this.setAttribute('name', this.firstChild.text().trim());
116
- const /** @type {import('../typings/event').AstListener} */ argListener = ({prevTarget}) => {
117
- if (prevTarget === this.firstChild) {
118
- this.setAttribute('name', prevTarget.text().trim());
119
- }
120
- };
121
- this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
122
- }
123
-
124
- /**
125
- * 移除无效部分
126
- * @complexity `n`
127
- */
128
- removeRedundant() {
129
- Parser.run(() => {
130
- for (let i = this.length - 1; i > 1; i--) {
131
- super.removeAt(i);
132
- }
133
- });
134
- }
135
-
136
- /**
137
- * 移除子节点,且在移除`arg-default`子节点时自动移除全部多余子节点
138
- * @param {number} i 移除位置
139
- * @returns {Token}
140
- */
141
- removeAt(i) {
142
- if (i === 1) {
143
- this.removeRedundant();
144
- }
145
- return super.removeAt(i);
146
- }
147
-
148
- /**
149
- * @override
150
- * @param {Token} token 待插入的子节点
151
- * @param {number} i 插入位置
152
- * @throws `RangeError` 不可插入多余子节点
153
- */
154
- insertAt(token, i = this.length) {
155
- const j = i < 0 ? i + this.length : i;
156
- if (j > 1) {
157
- throw new RangeError(`${this.constructor.name} 不可插入多余的子节点!`);
158
- }
159
- super.insertAt(token, i);
160
- if (j === 1) {
161
- token.type = 'arg-default';
162
- }
163
- return token;
164
- }
165
-
166
- /**
167
- * 设置参数名
168
- * @param {string} name 新参数名
169
- * @throws `SyntaxError` 非法的参数名
170
- */
171
- setName(name) {
172
- name = String(name);
173
- const root = Parser.parse(`{{{${name}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
174
- {length, firstChild: arg} = root;
175
- if (length !== 1 || arg.type !== 'arg' || arg.length !== 1) {
176
- throw new SyntaxError(`非法的参数名称:${noWrap(name)}`);
177
- }
178
- const {firstChild} = arg;
179
- arg.destroy();
180
- this.firstChild.safeReplaceWith(firstChild);
181
- }
182
-
183
- /**
184
- * 设置预设值
185
- * @param {string} value 预设值
186
- * @throws `SyntaxError` 非法的参数预设值
187
- */
188
- setDefault(value) {
189
- value = String(value);
190
- const root = Parser.parse(`{{{|${value}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
191
- {length, firstChild: arg} = root;
192
- if (length !== 1 || arg.type !== 'arg' || arg.length !== 2) {
193
- throw new SyntaxError(`非法的参数预设值:${noWrap(value)}`);
194
- }
195
- const {childNodes: [, oldDefault]} = this,
196
- {lastChild} = arg;
197
- arg.destroy();
198
- if (oldDefault) {
199
- oldDefault.safeReplaceWith(lastChild);
200
- } else {
201
- this.insertAt(lastChild);
202
- }
203
- }
204
- }
205
-
206
- Parser.classes.ArgToken = __filename;
207
- module.exports = ArgToken;
@@ -1,13 +0,0 @@
1
- 'use strict';
2
-
3
- const hidden = require('../../mixin/hidden'),
4
- Parser = require('../..'),
5
- AtomToken = require('.');
6
-
7
- /** 不可见的节点 */
8
- class HiddenToken extends hidden(AtomToken) {
9
- type = 'hidden';
10
- }
11
-
12
- Parser.classes.HiddenToken = __filename;
13
- module.exports = HiddenToken;
package/src/atom/index.js DELETED
@@ -1,43 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('../..'),
4
- Token = require('..');
5
-
6
- /**
7
- * 不会被继续解析的plain Token
8
- * @classdesc `{childNodes: ...AstText|Token}`
9
- */
10
- class AtomToken extends Token {
11
- type = 'plain';
12
-
13
- /**
14
- * @param {string} wikitext wikitext
15
- * @param {string|undefined} type Token.type
16
- * @param {import('../../typings/token').accum} accum
17
- * @param {import('../../typings/token').acceptable} acceptable 可接受的子节点设置
18
- */
19
- constructor(wikitext, type, config = Parser.getConfig(), accum = [], acceptable = undefined) {
20
- super(wikitext, config, true, accum, acceptable);
21
- if (type) {
22
- this.type = type;
23
- }
24
- }
25
-
26
- /**
27
- * @override
28
- * @this {AtomToken & {constructor: typeof AtomToken}}
29
- */
30
- cloneNode() {
31
- const cloned = this.cloneChildNodes(),
32
- config = this.getAttribute('config'),
33
- acceptable = this.getAttribute('acceptable');
34
- return Parser.run(() => {
35
- const token = new this.constructor(undefined, this.type, config, [], acceptable);
36
- token.append(...cloned);
37
- return token;
38
- });
39
- }
40
- }
41
-
42
- Parser.classes.AtomToken = __filename;
43
- module.exports = AtomToken;