wikiparser-node 0.9.0 → 0.9.2-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 (87) hide show
  1. package/bundle/bundle.min.js +38 -0
  2. package/extensions/editor.css +62 -0
  3. package/extensions/editor.js +328 -0
  4. package/extensions/ui.css +119 -0
  5. package/package.json +13 -12
  6. package/README.md +0 -39
  7. package/config/default.json +0 -832
  8. package/config/llwiki.json +0 -630
  9. package/config/moegirl.json +0 -729
  10. package/config/zhwiki.json +0 -1269
  11. package/index.js +0 -333
  12. package/lib/element.js +0 -611
  13. package/lib/node.js +0 -770
  14. package/lib/ranges.js +0 -130
  15. package/lib/text.js +0 -245
  16. package/lib/title.js +0 -83
  17. package/mixin/attributeParent.js +0 -117
  18. package/mixin/fixedToken.js +0 -40
  19. package/mixin/hidden.js +0 -21
  20. package/mixin/singleLine.js +0 -31
  21. package/mixin/sol.js +0 -54
  22. package/parser/brackets.js +0 -126
  23. package/parser/commentAndExt.js +0 -59
  24. package/parser/converter.js +0 -46
  25. package/parser/externalLinks.js +0 -33
  26. package/parser/hrAndDoubleUnderscore.js +0 -38
  27. package/parser/html.js +0 -42
  28. package/parser/links.js +0 -94
  29. package/parser/list.js +0 -59
  30. package/parser/magicLinks.js +0 -41
  31. package/parser/quotes.js +0 -64
  32. package/parser/selector.js +0 -177
  33. package/parser/table.js +0 -114
  34. package/src/arg.js +0 -207
  35. package/src/atom/hidden.js +0 -13
  36. package/src/atom/index.js +0 -43
  37. package/src/attribute.js +0 -470
  38. package/src/attributes.js +0 -453
  39. package/src/charinsert.js +0 -97
  40. package/src/converter.js +0 -176
  41. package/src/converterFlags.js +0 -284
  42. package/src/converterRule.js +0 -256
  43. package/src/extLink.js +0 -180
  44. package/src/gallery.js +0 -149
  45. package/src/hasNowiki/index.js +0 -44
  46. package/src/hasNowiki/pre.js +0 -40
  47. package/src/heading.js +0 -134
  48. package/src/html.js +0 -254
  49. package/src/imageParameter.js +0 -301
  50. package/src/imagemap.js +0 -199
  51. package/src/imagemapLink.js +0 -41
  52. package/src/index.js +0 -936
  53. package/src/link/category.js +0 -44
  54. package/src/link/file.js +0 -287
  55. package/src/link/galleryImage.js +0 -120
  56. package/src/link/index.js +0 -384
  57. package/src/magicLink.js +0 -149
  58. package/src/nested/choose.js +0 -24
  59. package/src/nested/combobox.js +0 -23
  60. package/src/nested/index.js +0 -93
  61. package/src/nested/references.js +0 -23
  62. package/src/nowiki/comment.js +0 -71
  63. package/src/nowiki/dd.js +0 -59
  64. package/src/nowiki/doubleUnderscore.js +0 -56
  65. package/src/nowiki/hr.js +0 -41
  66. package/src/nowiki/index.js +0 -56
  67. package/src/nowiki/list.js +0 -16
  68. package/src/nowiki/noinclude.js +0 -28
  69. package/src/nowiki/quote.js +0 -69
  70. package/src/onlyinclude.js +0 -64
  71. package/src/paramTag/index.js +0 -89
  72. package/src/paramTag/inputbox.js +0 -35
  73. package/src/parameter.js +0 -239
  74. package/src/syntax.js +0 -91
  75. package/src/table/index.js +0 -983
  76. package/src/table/td.js +0 -338
  77. package/src/table/tr.js +0 -319
  78. package/src/tagPair/ext.js +0 -148
  79. package/src/tagPair/include.js +0 -50
  80. package/src/tagPair/index.js +0 -126
  81. package/src/transclude.js +0 -843
  82. package/tool/index.js +0 -1202
  83. package/util/base.js +0 -17
  84. package/util/debug.js +0 -73
  85. package/util/diff.js +0 -76
  86. package/util/lint.js +0 -55
  87. 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,126 +0,0 @@
1
- 'use strict';
2
-
3
- const {removeComment} = require('../util/string'),
4
- Parser = require('..'),
5
- HeadingToken = require('../src/heading'),
6
- TranscludeToken = require('../src/transclude'),
7
- ArgToken = require('../src/arg');
8
-
9
- /**
10
- * 解析花括号
11
- * @param {string} text wikitext
12
- * @param {accum} accum
13
- * @throws TranscludeToken.constructor()
14
- */
15
- const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
16
- const source = `${config.excludes.includes('heading') ? '' : '^(\0\\d+c\x7F)*={1,6}|'}\\[\\[|\\{{2,}|-\\{(?!\\{)`,
17
- {parserFunction: [,,, subst]} = config,
18
- /** @type {BracketExecArray[]} */ stack = [],
19
- closes = {'=': '\n', '{': '\\}{2,}|\\|', '-': '\\}-', '[': '\\]\\]'},
20
- /** @type {Record<string, string>} */ marks = {'!': '!', '!!': '+', '(!': '{', '!)': '}', '!-': '-', '=': '~'};
21
- let regex = new RegExp(source, 'gmu'),
22
- /** @type {BracketExecArray} */ mt = regex.exec(text),
23
- moreBraces = text.includes('}}'),
24
- lastIndex;
25
- while (mt || lastIndex <= text.length && stack.at(-1)?.[0]?.[0] === '=') {
26
- if (mt?.[1]) {
27
- const [, {length}] = mt;
28
- mt[0] = mt[0].slice(length);
29
- mt.index += length;
30
- }
31
- const {0: syntax, index: curIndex} = mt ?? {0: '\n', index: text.length},
32
- /** @type {BracketExecArray} */ top = stack.pop() ?? {},
33
- {0: open, index, parts, findEqual: topFindEqual, pos: topPos} = top,
34
- innerEqual = syntax === '=' && topFindEqual;
35
- if (syntax === ']]' || syntax === '}-') { // 情形1:闭合内链或转换
36
- lastIndex = curIndex + 2;
37
- } else if (syntax === '\n') { // 情形2:闭合标题
38
- lastIndex = curIndex + 1;
39
- const {pos, findEqual} = stack.at(-1) ?? {};
40
- if (!pos || findEqual || removeComment(text.slice(pos, index)) !== '') {
41
- const rmt = /^(={1,6})(.+)\1((?:\s|\0\d+c\x7F)*)$/u.exec(text.slice(index, curIndex));
42
- if (rmt) {
43
- text = `${text.slice(0, index)}\0${accum.length}h\x7F${text.slice(curIndex)}`;
44
- lastIndex = index + 4 + String(accum.length).length;
45
- new HeadingToken(rmt[1].length, rmt.slice(2), config, accum);
46
- }
47
- }
48
- } else if (syntax === '|' || innerEqual) { // 情形3:模板内部,含行首单个'='
49
- lastIndex = curIndex + 1;
50
- parts.at(-1).push(text.slice(topPos, curIndex));
51
- if (syntax === '|') {
52
- parts.push([]);
53
- }
54
- top.pos = lastIndex;
55
- top.findEqual = syntax === '|';
56
- stack.push(top);
57
- } else if (syntax.startsWith('}}')) { // 情形4:闭合模板
58
- const close = syntax.slice(0, Math.min(open.length, 3)),
59
- rest = open.length - close.length,
60
- {length} = accum;
61
- lastIndex = curIndex + close.length; // 这不是最终的lastIndex
62
- parts.at(-1).push(text.slice(topPos, curIndex));
63
- let skip = false,
64
- ch = 't';
65
- if (close.length === 3) {
66
- const argParts = parts.map(part => part.join('=')),
67
- str = argParts.length > 1 && removeComment(argParts[1]).trim();
68
- new ArgToken(argParts, config, accum);
69
- if (str && str.endsWith(':') && subst.includes(str.slice(0, -1).toLowerCase())) {
70
- ch = 's';
71
- }
72
- } else {
73
- const name = removeComment(parts[0][0]).trim();
74
- if (name in marks) {
75
- ch = marks[name]; // 标记{{!}}等
76
- } else if (/^(?:filepath|(?:full|canonical)urle?):.|^server$/iu.test(name)) {
77
- ch = 'm';
78
- } else if (/^#vardefine:./iu.test(name)) {
79
- ch = 'c';
80
- }
81
- try {
82
- new TranscludeToken(parts[0][0], parts.slice(1), config, accum);
83
- } catch (e) {
84
- if (e instanceof Error && e.message.startsWith('非法的模板名称:')) {
85
- lastIndex = index + open.length;
86
- skip = true;
87
- } else {
88
- throw e;
89
- }
90
- }
91
- }
92
- if (!skip) {
93
- text = `${text.slice(0, index + rest)}\0${length}${ch}\x7F${text.slice(lastIndex)}`;
94
- lastIndex = index + rest + 3 + String(length).length;
95
- if (rest > 1) {
96
- stack.push({0: open.slice(0, rest), index, pos: index + rest, parts: [[]]});
97
- } else if (rest === 1 && text[index - 1] === '-') {
98
- stack.push({0: '-{', index: index - 1, pos: index + 1, parts: [[]]});
99
- }
100
- }
101
- } else { // 情形5:开启
102
- lastIndex = curIndex + syntax.length;
103
- if (syntax[0] === '{') {
104
- mt.pos = lastIndex;
105
- mt.parts = [[]];
106
- }
107
- stack.push(...'0' in top ? [top] : [], mt);
108
- }
109
- moreBraces &&= text.slice(lastIndex).includes('}}');
110
- let curTop = stack.at(-1);
111
- if (!moreBraces && curTop?.[0]?.[0] === '{') {
112
- stack.pop();
113
- curTop = stack.at(-1);
114
- }
115
- regex = new RegExp(source + (curTop
116
- ? `|${closes[curTop[0][0]]}${curTop.findEqual ? '|=' : ''}`
117
- : ''
118
- ), 'gmu');
119
- regex.lastIndex = lastIndex;
120
- mt = regex.exec(text);
121
- }
122
- return text;
123
- };
124
-
125
- Parser.parsers.parseBrackets = __filename;
126
- module.exports = parseBrackets;
@@ -1,59 +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 {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(/(?<=^|\0\d+e\x7F)[^\0]+(?=$|\0\d+e\x7F)/gu, substr => {
24
- new NoincludeToken(substr, config, accum);
25
- return `\0${accum.length - 1}c\x7F`;
26
- });
27
- }
28
- const ext = config.ext.join('|'),
29
- includeRegex = includeOnly ? 'includeonly' : '(?:no|only)include',
30
- noincludeRegex = includeOnly ? 'noinclude' : 'includeonly',
31
- regex = new RegExp(
32
- '<!--.*?(?:-->|$)|' // comment
33
- + `<${includeRegex}(?:\\s[^>]*?)?>|</${includeRegex}\\s*>|` // <includeonly>
34
- + `<(${ext})(\\s[^>]*?)?(?:/>|>(.*?)</(\\1\\s*)>)|` // 扩展标签
35
- + `<(${noincludeRegex})(\\s[^>]*?)?(?:/>|>(.*?)(?:</(\\5\\s*)>|$))`, // <noinclude>
36
- 'gisu',
37
- );
38
- return text.replace(
39
- regex,
40
- /** @type {function(...string): string} */
41
- (substr, name, attr, inner, closing, include, includeAttr, includeInner, includeClosing) => {
42
- const str = `\0${accum.length}${name ? 'e' : 'c'}\x7F`;
43
- if (name) {
44
- new ExtToken(name, attr, inner, closing, config, accum);
45
- } else if (substr.startsWith('<!--')) {
46
- const closed = substr.endsWith('-->');
47
- new CommentToken(substr.slice(4, closed ? -3 : undefined), closed, config, accum);
48
- } else if (include) {
49
- new IncludeToken(include, includeAttr, includeInner, includeClosing, config, accum);
50
- } else {
51
- new NoincludeToken(substr, config, accum);
52
- }
53
- return str;
54
- },
55
- );
56
- };
57
-
58
- Parser.parsers.parseCommentAndExt = __filename;
59
- 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 {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 {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,38 +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
-
9
- /**
10
- * 解析\<hr\>和状态开关
11
- * @param {Token & {firstChild: AstText}} root 根节点
12
- * @param {accum} accum
13
- */
14
- const parseHrAndDoubleUnderscore = ({firstChild: {data}, type, name}, config = Parser.getConfig(), accum = []) => {
15
- const {doubleUnderscore} = config,
16
- insensitive = new Set(doubleUnderscore[0]),
17
- sensitive = new Set(doubleUnderscore[1]);
18
- if (type !== 'root' && (type !== 'ext-inner' || name !== 'poem')) {
19
- data = `\0${data}`;
20
- }
21
- data = data.replace(/^((?:\0\d+c\x7F)*)(-{4,})/gmu, (_, lead, m) => {
22
- new HrToken(m.length, config, accum);
23
- return `${lead}\0${accum.length - 1}r\x7F`;
24
- }).replace(
25
- new RegExp(`__(${doubleUnderscore.flat().join('|')})__`, 'giu'),
26
- /** @param {string} p1 */ (m, p1) => {
27
- if (insensitive.has(p1.toLowerCase()) || sensitive.has(p1)) {
28
- new DoubleUnderscoreToken(p1, config, accum);
29
- return `\0${accum.length - 1}u\x7F`;
30
- }
31
- return m;
32
- },
33
- );
34
- return type === 'root' || type === 'ext-inner' && name === 'poem' ? data : data.slice(1);
35
- };
36
-
37
- Parser.parsers.parseHrAndDoubleUnderscore = __filename;
38
- 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 {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 {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 {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 {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 {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;