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
package/mixin/sol.js DELETED
@@ -1,54 +0,0 @@
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;
@@ -1,128 +0,0 @@
1
- 'use strict';
2
-
3
- /** @typedef {import('../typings/parser').BracketExecArray} BracketExecArray */
4
-
5
- const {removeComment} = require('../util/string'),
6
- Parser = require('..'),
7
- HeadingToken = require('../src/heading'),
8
- TranscludeToken = require('../src/transclude'),
9
- ArgToken = require('../src/arg');
10
-
11
- /**
12
- * 解析花括号
13
- * @param {string} text wikitext
14
- * @param {import('../typings/token').accum} accum
15
- * @throws TranscludeToken.constructor()
16
- */
17
- const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
18
- const source = `${config.excludes.includes('heading') ? '' : '^(\0\\d+c\x7F)*={1,6}|'}\\[\\[|\\{{2,}|-\\{(?!\\{)`,
19
- {parserFunction: [,,, subst]} = config,
20
- /** @type {BracketExecArray[]} */ stack = [],
21
- closes = {'=': '\n', '{': '\\}{2,}|\\|', '-': '\\}-', '[': '\\]\\]'},
22
- /** @type {Record<string, string>} */ marks = {'!': '!', '!!': '+', '(!': '{', '!)': '}', '!-': '-', '=': '~'};
23
- let regex = new RegExp(source, 'gmu'),
24
- /** @type {BracketExecArray} */ mt = regex.exec(text),
25
- moreBraces = text.includes('}}'),
26
- lastIndex;
27
- while (mt || lastIndex <= text.length && stack.at(-1)?.[0]?.[0] === '=') {
28
- if (mt?.[1]) {
29
- const [, {length}] = mt;
30
- mt[0] = mt[0].slice(length);
31
- mt.index += length;
32
- }
33
- const {0: syntax, index: curIndex} = mt ?? {0: '\n', index: text.length},
34
- /** @type {BracketExecArray} */ top = stack.pop() ?? {},
35
- {0: open, index, parts, findEqual: topFindEqual, pos: topPos} = top,
36
- innerEqual = syntax === '=' && topFindEqual;
37
- if (syntax === ']]' || syntax === '}-') { // 情形1:闭合内链或转换
38
- lastIndex = curIndex + 2;
39
- } else if (syntax === '\n') { // 情形2:闭合标题
40
- lastIndex = curIndex + 1;
41
- const {pos, findEqual} = stack.at(-1) ?? {};
42
- if (!pos || findEqual || removeComment(text.slice(pos, index)) !== '') {
43
- const rmt = /^(={1,6})(.+)\1((?:\s|\0\d+c\x7F)*)$/u.exec(text.slice(index, curIndex));
44
- if (rmt) {
45
- text = `${text.slice(0, index)}\0${accum.length}h\x7F${text.slice(curIndex)}`;
46
- lastIndex = index + 4 + String(accum.length).length;
47
- new HeadingToken(rmt[1].length, rmt.slice(2), config, accum);
48
- }
49
- }
50
- } else if (syntax === '|' || innerEqual) { // 情形3:模板内部,含行首单个'='
51
- lastIndex = curIndex + 1;
52
- parts.at(-1).push(text.slice(topPos, curIndex));
53
- if (syntax === '|') {
54
- parts.push([]);
55
- }
56
- top.pos = lastIndex;
57
- top.findEqual = syntax === '|';
58
- stack.push(top);
59
- } else if (syntax.startsWith('}}')) { // 情形4:闭合模板
60
- const close = syntax.slice(0, Math.min(open.length, 3)),
61
- rest = open.length - close.length,
62
- {length} = accum;
63
- lastIndex = curIndex + close.length; // 这不是最终的lastIndex
64
- parts.at(-1).push(text.slice(topPos, curIndex));
65
- let skip = false,
66
- ch = 't';
67
- if (close.length === 3) {
68
- const argParts = parts.map(part => part.join('=')),
69
- str = argParts.length > 1 && removeComment(argParts[1]).trim();
70
- new ArgToken(argParts, config, accum);
71
- if (str && str.endsWith(':') && subst.includes(str.slice(0, -1).toLowerCase())) {
72
- ch = 's';
73
- }
74
- } else {
75
- try {
76
- new TranscludeToken(parts[0][0], parts.slice(1), config, accum);
77
- const name = removeComment(parts[0][0]).trim();
78
- if (name in marks) {
79
- ch = marks[name]; // 标记{{!}}等
80
- } else if (/^(?:filepath|(?:full|canonical)urle?):.|^server$/iu.test(name)) {
81
- ch = 'm';
82
- } else if (/^#vardefine:./iu.test(name)) {
83
- ch = 'c';
84
- }
85
- } catch (e) {
86
- if (e instanceof Error && e.message.startsWith('非法的模板名称:')) {
87
- lastIndex = index + open.length;
88
- skip = true;
89
- } else {
90
- throw e;
91
- }
92
- }
93
- }
94
- if (!skip) {
95
- text = `${text.slice(0, index + rest)}\0${length}${ch}\x7F${text.slice(lastIndex)}`;
96
- lastIndex = index + rest + 3 + String(length).length;
97
- if (rest > 1) {
98
- stack.push({0: open.slice(0, rest), index, pos: index + rest, parts: [[]]});
99
- } else if (rest === 1 && text[index - 1] === '-') {
100
- stack.push({0: '-{', index: index - 1, pos: index + 1, parts: [[]]});
101
- }
102
- }
103
- } else { // 情形5:开启
104
- lastIndex = curIndex + syntax.length;
105
- if (syntax[0] === '{') {
106
- mt.pos = lastIndex;
107
- mt.parts = [[]];
108
- }
109
- stack.push(...'0' in top ? [top] : [], mt);
110
- }
111
- moreBraces &&= text.slice(lastIndex).includes('}}');
112
- let curTop = stack.at(-1);
113
- if (!moreBraces && curTop?.[0]?.[0] === '{') {
114
- stack.pop();
115
- curTop = stack.at(-1);
116
- }
117
- regex = new RegExp(source + (curTop
118
- ? `|${closes[curTop[0][0]]}${curTop.findEqual ? '|=' : ''}`
119
- : ''
120
- ), 'gmu');
121
- regex.lastIndex = lastIndex;
122
- mt = regex.exec(text);
123
- }
124
- return text;
125
- };
126
-
127
- Parser.parsers.parseBrackets = __filename;
128
- module.exports = parseBrackets;
@@ -1,62 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- OnlyincludeToken = require('../src/onlyinclude'),
5
- NoincludeToken = require('../src/nowiki/noinclude'),
6
- IncludeToken = require('../src/tagPair/include'),
7
- ExtToken = require('../src/tagPair/ext'),
8
- CommentToken = require('../src/nowiki/comment');
9
-
10
- /**
11
- * 解析HTML注释和扩展标签
12
- * @param {string} text wikitext
13
- * @param {import('../typings/token').accum} accum
14
- * @param {boolean} includeOnly 是否嵌入
15
- */
16
- const parseCommentAndExt = (text, config = Parser.getConfig(), accum = [], includeOnly = false) => {
17
- const onlyinclude = /<onlyinclude>(.*?)<\/onlyinclude>/gsu;
18
- if (includeOnly && text.search(onlyinclude) !== -1) { // `<onlyinclude>`拥有最高优先级
19
- return text.replace(onlyinclude, /** @param {string} inner */ (_, inner) => {
20
- const str = `\0${accum.length}e\x7F`;
21
- new OnlyincludeToken(inner, config, accum);
22
- return str;
23
- }).replace(
24
- /(?<=^|\0\d+e\x7F)[^\0]+(?=$|\0\d+e\x7F)/gu,
25
- substr => {
26
- new NoincludeToken(substr, config, accum);
27
- return `\0${accum.length - 1}c\x7F`;
28
- },
29
- );
30
- }
31
- const ext = config.ext.join('|'),
32
- includeRegex = includeOnly ? 'includeonly' : '(?:no|only)include',
33
- noincludeRegex = includeOnly ? 'noinclude' : 'includeonly',
34
- regex = new RegExp(
35
- '<!--.*?(?:-->|$)|' // comment
36
- + `<${includeRegex}(?:\\s[^>]*?)?>|</${includeRegex}\\s*>|` // <includeonly>
37
- + `<(${ext})(\\s[^>]*?)?(?:/>|>(.*?)</(\\1\\s*)>)|` // 扩展标签
38
- + `<(${noincludeRegex})(\\s[^>]*?)?(?:/>|>(.*?)(?:</(\\5\\s*)>|$))`, // <noinclude>
39
- 'gisu',
40
- );
41
- return text.replace(
42
- regex,
43
- /** @type {function(...string): string} */
44
- (substr, name, attr, inner, closing, include, includeAttr, includeInner, includeClosing) => {
45
- const str = `\0${accum.length}${name ? 'e' : 'c'}\x7F`;
46
- if (name) {
47
- new ExtToken(name, attr, inner, closing, config, accum);
48
- } else if (substr.startsWith('<!--')) {
49
- const closed = substr.endsWith('-->');
50
- new CommentToken(substr.slice(4, closed ? -3 : undefined), closed, config, accum);
51
- } else if (include) {
52
- new IncludeToken(include, includeAttr, includeInner, includeClosing, config, accum);
53
- } else {
54
- new NoincludeToken(substr, config, accum);
55
- }
56
- return str;
57
- },
58
- );
59
- };
60
-
61
- Parser.parsers.parseCommentAndExt = __filename;
62
- module.exports = parseCommentAndExt;
@@ -1,46 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- ConverterToken = require('../src/converter');
5
-
6
- /**
7
- * 解析语言变体转换
8
- * @param {string} wikitext wikitext
9
- * @param {import('../typings/token').accum} accum
10
- */
11
- const parseConverter = (wikitext, config = Parser.getConfig(), accum = []) => {
12
- const regex1 = /-\{/gu,
13
- regex2 = /-\{|\}-/gu,
14
- /** @type {RegExpExecArray[]} */ stack = [];
15
- let regex = regex1,
16
- mt = regex.exec(wikitext);
17
- while (mt) {
18
- const {0: syntax, index} = mt;
19
- if (syntax === '}-') {
20
- const top = stack.pop(),
21
- {length} = accum,
22
- str = wikitext.slice(top.index + 2, index),
23
- i = str.indexOf('|'),
24
- [flags, text] = i === -1 ? [[], str] : [str.slice(0, i).split(';'), str.slice(i + 1)],
25
- temp = text.replace(/(&[#a-z\d]+);/giu, '$1\x01'),
26
- variants = `(?:${config.variants.join('|')})`,
27
- rules = temp.split(new RegExp(`;(?=\\s*(?:${variants}|[^;]*?=>\\s*${variants})\\s*:)`, 'u'))
28
- .map(rule => rule.replaceAll('\x01', ';'));
29
- new ConverterToken(flags, rules, config, accum);
30
- wikitext = `${wikitext.slice(0, top.index)}\0${length}v\x7F${wikitext.slice(index + 2)}`;
31
- if (stack.length === 0) {
32
- regex = regex1;
33
- }
34
- regex.lastIndex = top.index + 3 + String(length).length;
35
- } else {
36
- stack.push(mt);
37
- regex = regex2;
38
- regex.lastIndex = index + 2;
39
- }
40
- mt = regex.exec(wikitext);
41
- }
42
- return wikitext;
43
- };
44
-
45
- Parser.parsers.parseConverter = __filename;
46
- module.exports = parseConverter;
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- const {extUrlChar, extUrlCharFirst} = require('../util/string'),
4
- Parser = require('..'),
5
- ExtLinkToken = require('../src/extLink');
6
-
7
- /**
8
- * 解析外部链接
9
- * @param {string} wikitext wikitext
10
- * @param {import('../typings/token').accum} accum
11
- */
12
- const parseExternalLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
13
- const regex = new RegExp(
14
- `\\[((?:(?:${config.protocol}|//)${extUrlCharFirst}|\0\\d+m\x7F)${
15
- extUrlChar
16
- })(\\p{Zs}*)([^\\]\x01-\x08\x0A-\x1F\uFFFD]*)\\]`,
17
- 'giu',
18
- );
19
- return wikitext.replace(regex, /** @type {function(...string): string} */ (_, url, space, text) => {
20
- const {length} = accum,
21
- mt = /&[lg]t;/u.exec(url);
22
- if (mt) {
23
- url = url.slice(0, mt.index);
24
- space = '';
25
- text = `${url.slice(mt.index)}${space}${text}`;
26
- }
27
- new ExtLinkToken(url, space, text, config, accum);
28
- return `\0${length}w\x7F`;
29
- });
30
- };
31
-
32
- Parser.parsers.parseExternalLinks = __filename;
33
- module.exports = parseExternalLinks;
@@ -1,49 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- AstText = require('../lib/text'),
5
- Token = require('../src'),
6
- HrToken = require('../src/nowiki/hr'),
7
- DoubleUnderscoreToken = require('../src/nowiki/doubleUnderscore'),
8
- HeadingToken = require('../src/heading');
9
-
10
- /**
11
- * 解析\<hr\>和状态开关
12
- * @param {Token & {firstChild: AstText}} root 根节点
13
- * @param {import('../typings/token').accum} accum
14
- */
15
- const parseHrAndDoubleUnderscore = ({firstChild: {data}, type, name}, config = Parser.getConfig(), accum = []) => {
16
- const {doubleUnderscore} = config,
17
- insensitive = new Set(doubleUnderscore[0]),
18
- sensitive = new Set(doubleUnderscore[1]);
19
- if (type !== 'root' && (type !== 'ext-inner' || name !== 'poem')) {
20
- data = `\0${data}`;
21
- }
22
- data = data.replace(
23
- /^((?:\0\d+c\x7F)*)(-{4,})/gmu,
24
- /** @type {function(...string): string} */ (_, lead, m) => {
25
- new HrToken(m.length, config, accum);
26
- return `${lead}\0${accum.length - 1}r\x7F`;
27
- },
28
- ).replace(
29
- new RegExp(`__(${doubleUnderscore.flat().join('|')})__`, 'giu'),
30
- /** @param {string} p1 */ (m, p1) => {
31
- if (insensitive.has(p1.toLowerCase()) || sensitive.has(p1)) {
32
- new DoubleUnderscoreToken(p1, config, accum);
33
- return `\0${accum.length - 1}u\x7F`;
34
- }
35
- return m;
36
- },
37
- ).replace(
38
- /^((?:\0\d+c\x7F)*)(={1,6})(.+)\2((?:[^\S\n]|\0\d+c\x7F)*)$/gmu,
39
- /** @type {function(...string): string} */ (_, lead, equals, heading, trail) => {
40
- const text = `${lead}\0${accum.length}h\x7F`;
41
- new HeadingToken(equals.length, [heading, trail], config, accum);
42
- return text;
43
- },
44
- );
45
- return type === 'root' || type === 'ext-inner' && name === 'poem' ? data : data.slice(1);
46
- };
47
-
48
- Parser.parsers.parseHrAndDoubleUnderscore = __filename;
49
- module.exports = parseHrAndDoubleUnderscore;
package/parser/html.js DELETED
@@ -1,42 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- AttributesToken = require('../src/attributes'),
5
- HtmlToken = require('../src/html');
6
-
7
- /**
8
- * 解析HTML标签
9
- * @param {string} wikitext wikitext
10
- * @param {import('../typings/token').accum} accum
11
- */
12
- const parseHtml = (wikitext, config = Parser.getConfig(), accum = []) => {
13
- const regex = /^(\/?)([a-z][^\s/>]*)((?:\s|\/(?!>))[^>]*?)?(\/?>)([^<]*)$/iu,
14
- elements = new Set(config.html.flat()),
15
- bits = wikitext.split('<');
16
- let text = bits.shift();
17
- for (const x of bits) {
18
- const mt = regex.exec(x),
19
- t = mt?.[2],
20
- name = t?.toLowerCase();
21
- if (!mt || !elements.has(name)) {
22
- text += `<${x}`;
23
- continue;
24
- }
25
- const [, slash,, params = '', brace, rest] = mt,
26
- attr = new AttributesToken(params, 'html-attrs', name, config, accum),
27
- itemprop = attr.getAttr('itemprop');
28
- if (name === 'meta' && (itemprop === undefined || attr.getAttr('content') === undefined)
29
- || name === 'link' && (itemprop === undefined || attr.getAttr('href') === undefined)
30
- ) {
31
- text += `<${x}`;
32
- accum.pop();
33
- continue;
34
- }
35
- text += `\0${accum.length}x\x7F${rest}`;
36
- new HtmlToken(t, attr, slash === '/', brace === '/>', config, accum);
37
- }
38
- return text;
39
- };
40
-
41
- Parser.parsers.parseHtml = __filename;
42
- module.exports = parseHtml;
package/parser/links.js DELETED
@@ -1,94 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- LinkToken = require('../src/link'),
5
- FileToken = require('../src/link/file'),
6
- CategoryToken = require('../src/link/category');
7
-
8
- /**
9
- * 解析内部链接
10
- * @param {string} wikitext wikitext
11
- * @param {import('../typings/token').accum} accum
12
- */
13
- const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
14
- const parseQuotes = require('./quotes.js');
15
- const regex = /^((?:(?!\0\d+!\x7F)[^\n<>[\]{}|])+)(?:(\||\0\d+!\x7F)(.*?[^\]]))?\]\](.*)$/su,
16
- regexImg = /^((?:(?!\0\d+!\x7F)[^\n<>[\]{}|])+)(\||\0\d+!\x7F)(.*)$/su,
17
- regexExt = new RegExp(`^\\s*(?:${config.protocol})`, 'iu'),
18
- bits = wikitext.split('[[');
19
- let s = bits.shift();
20
- for (let i = 0; i < bits.length; i++) {
21
- let mightBeImg, link, delimiter, text, after;
22
- const x = bits[i],
23
- m = regex.exec(x);
24
- if (m) {
25
- [, link, delimiter, text, after] = m;
26
- if (after[0] === ']' && text?.includes('[')) {
27
- text += ']';
28
- after = after.slice(1);
29
- }
30
- } else {
31
- const m2 = regexImg.exec(x);
32
- if (m2) {
33
- mightBeImg = true;
34
- [, link, delimiter, text] = m2;
35
- }
36
- }
37
- if (link === undefined || regexExt.test(link) || /\0\d+[exhbru]\x7F/u.test(link)) {
38
- s += `[[${x}`;
39
- continue;
40
- }
41
- const force = link.trim()[0] === ':';
42
- if (force && mightBeImg) {
43
- s += `[[${x}`;
44
- continue;
45
- }
46
- const title = Parser.normalizeTitle(link, 0, false, config, true, true, true),
47
- {ns, interwiki, valid} = title;
48
- if (!valid) {
49
- s += `[[${x}`;
50
- continue;
51
- } else if (mightBeImg) {
52
- if (interwiki || ns !== 6) {
53
- s += `[[${x}`;
54
- continue;
55
- }
56
- let found;
57
- for (i++; i < bits.length; i++) {
58
- const next = bits[i],
59
- p = next.split(']]');
60
- if (p.length > 2) {
61
- found = true;
62
- text += `[[${p[0]}]]${p[1]}`;
63
- after = p.slice(2).join(']]');
64
- break;
65
- } else if (p.length === 2) {
66
- text += `[[${p[0]}]]${p[1]}`;
67
- } else {
68
- text += `[[${next}`;
69
- break;
70
- }
71
- }
72
- text = parseLinks(text, config, accum);
73
- if (!found) {
74
- s += `[[${link}${delimiter}${text}`;
75
- continue;
76
- }
77
- }
78
- text &&= parseQuotes(text, config, accum);
79
- s += `\0${accum.length}l\x7F${after}`;
80
- let SomeLinkToken = LinkToken;
81
- if (!force) {
82
- if (!interwiki && ns === 6) {
83
- SomeLinkToken = FileToken;
84
- } else if (!interwiki && ns === 14) {
85
- SomeLinkToken = CategoryToken;
86
- }
87
- }
88
- new SomeLinkToken(link, text, config, accum, delimiter);
89
- }
90
- return s;
91
- };
92
-
93
- Parser.parsers.parseLinks = __filename;
94
- module.exports = parseLinks;
package/parser/list.js DELETED
@@ -1,59 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- ListToken = require('../src/nowiki/list'),
5
- DdToken = require('../src/nowiki/dd');
6
-
7
- /**
8
- * 解析列表
9
- * @param {string} text wikitext
10
- * @param {import('../typings/token').accum} accum
11
- */
12
- const parseList = (text, config = Parser.getConfig(), accum = []) => {
13
- const mt = /^((?:\0\d+c\x7F)*)([;:*#]+)/u.exec(text);
14
- if (!mt) {
15
- return text;
16
- }
17
- const [total, comment, prefix] = mt;
18
- text = `${comment}\0${accum.length}d\x7F${text.slice(total.length)}`;
19
- new ListToken(prefix, config, accum);
20
- let dt = prefix.split(';').length - 1;
21
- if (!dt) {
22
- return text;
23
- }
24
- let regex = /:+|-\{/gu,
25
- ex = regex.exec(text),
26
- lc = 0;
27
- while (ex && dt) {
28
- const {0: syntax, index} = ex;
29
- if (syntax[0] === ':') {
30
- if (syntax.length >= dt) {
31
- new DdToken(':'.repeat(dt), config, accum);
32
- return `${text.slice(0, index)}\0${accum.length - 1}d\x7F${text.slice(index + dt)}`;
33
- }
34
- text = `${text.slice(0, index)}\0${accum.length}d\x7F${text.slice(regex.lastIndex)}`;
35
- dt -= syntax.length;
36
- regex.lastIndex = index + 4 + String(accum.length).length;
37
- new DdToken(syntax, config, accum);
38
- } else if (syntax === '-{') {
39
- if (!lc) {
40
- const {lastIndex} = regex;
41
- regex = /-\{|\}-/gu;
42
- regex.lastIndex = lastIndex;
43
- }
44
- lc++;
45
- } else {
46
- lc--;
47
- if (!lc) {
48
- const {lastIndex} = regex;
49
- regex = /:+|-\{/gu;
50
- regex.lastIndex = lastIndex;
51
- }
52
- }
53
- ex = regex.exec(text);
54
- }
55
- return text;
56
- };
57
-
58
- Parser.parsers.parseList = __filename;
59
- module.exports = parseList;
@@ -1,41 +0,0 @@
1
- 'use strict';
2
-
3
- const {extUrlChar, extUrlCharFirst} = require('../util/string'),
4
- Parser = require('..'),
5
- MagicLinkToken = require('../src/magicLink');
6
-
7
- /**
8
- * 解析自由外链
9
- * @param {string} wikitext wikitext
10
- * @param {import('../typings/token').accum} accum
11
- */
12
- const parseMagicLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
13
- const regex = new RegExp(`(?<![\\p{L}\\d_])(?:${config.protocol})(${extUrlCharFirst}${extUrlChar})`, 'giu');
14
- return wikitext.replace(regex, /** @param {string} p1 */ (m, p1) => {
15
- let trail = '',
16
- url = m;
17
- const m2 = /&(?:lt|gt|nbsp|#x0*(?:3[ce]|a0)|#0*(?:6[02]|160));/iu.exec(url);
18
- if (m2) {
19
- trail = url.slice(m2.index);
20
- url = url.slice(0, m2.index);
21
- }
22
- const sep = new RegExp(`[,;.:!?${url.includes('(') ? '' : ')'}]+$`, 'u'),
23
- sepChars = sep.exec(url);
24
- if (sepChars) {
25
- let correction = 0;
26
- if (sepChars[0][0] === ';' && /&(?:[a-z]+|#x[\da-f]+|#\d+)$/iu.test(url.slice(0, sepChars.index))) {
27
- correction = 1;
28
- }
29
- trail = `${url.slice(sepChars.index + correction)}${trail}`;
30
- url = url.slice(0, sepChars.index + correction);
31
- }
32
- if (trail.length >= p1.length) {
33
- return m;
34
- }
35
- new MagicLinkToken(url, false, config, accum);
36
- return `\0${accum.length - 1}w\x7F${trail}`;
37
- });
38
- };
39
-
40
- Parser.parsers.parseMagicLinks = __filename;
41
- module.exports = parseMagicLinks;
package/parser/quotes.js DELETED
@@ -1,64 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- QuoteToken = require('../src/nowiki/quote');
5
-
6
- /**
7
- * 解析单引号
8
- * @param {string} text wikitext
9
- * @param {import('../typings/token').accum} accum
10
- */
11
- const parseQuotes = (text, config = Parser.getConfig(), accum = []) => {
12
- const arr = text.split(/('{2,})/u),
13
- {length} = arr;
14
- if (length === 1) {
15
- return text;
16
- }
17
- let nBold = 0,
18
- nItalic = 0,
19
- firstSingle, firstMulti, firstSpace;
20
- for (let i = 1; i < length; i += 2) {
21
- const {length: len} = arr[i];
22
- switch (len) {
23
- case 2:
24
- nItalic++;
25
- break;
26
- case 4:
27
- arr[i - 1] += `'`;
28
- arr[i] = `'''`;
29
- // fall through
30
- case 3:
31
- nBold++;
32
- if (firstSingle) {
33
- break;
34
- } else if (arr[i - 1].endsWith(' ')) {
35
- if (!firstMulti && !firstSpace) {
36
- firstSpace = i;
37
- }
38
- } else if (arr[i - 1].at(-2) === ' ') {
39
- firstSingle = i;
40
- } else {
41
- firstMulti ||= i;
42
- }
43
- break;
44
- default:
45
- arr[i - 1] += `'`.repeat(len - 5);
46
- arr[i] = `'''''`;
47
- nItalic++;
48
- nBold++;
49
- }
50
- }
51
- if (nItalic % 2 === 1 && nBold % 2 === 1) {
52
- const i = firstSingle ?? firstMulti ?? firstSpace;
53
- arr[i] = `''`;
54
- arr[i - 1] += `'`;
55
- }
56
- for (let i = 1; i < length; i += 2) {
57
- new QuoteToken(arr[i].length, config, accum);
58
- arr[i] = `\0${accum.length - 1}q\x7F`;
59
- }
60
- return arr.join('');
61
- };
62
-
63
- Parser.parsers.parseQuotes = __filename;
64
- module.exports = parseQuotes;