wikiparser-node 1.4.3-b → 1.4.4

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 (166) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/config/.schema.json +172 -0
  4. package/config/enwiki.json +814 -1
  5. package/config/llwiki.json +35 -1
  6. package/config/moegirl.json +44 -1
  7. package/config/zhwiki.json +466 -1
  8. package/dist/addon/table.d.ts +6 -0
  9. package/dist/addon/table.js +564 -0
  10. package/dist/base.d.ts +58 -0
  11. package/dist/index.d.ts +30 -0
  12. package/dist/index.js +211 -0
  13. package/dist/internal.d.ts +44 -0
  14. package/dist/lib/element.d.ts +155 -0
  15. package/dist/lib/element.js +655 -0
  16. package/dist/lib/node.d.ts +146 -0
  17. package/dist/lib/node.js +428 -0
  18. package/dist/lib/range.d.ts +104 -0
  19. package/dist/lib/range.js +385 -0
  20. package/dist/lib/ranges.d.ts +26 -0
  21. package/dist/lib/ranges.js +118 -0
  22. package/dist/lib/text.d.ts +62 -0
  23. package/dist/lib/text.js +306 -0
  24. package/dist/lib/title.d.ts +38 -0
  25. package/dist/lib/title.js +164 -0
  26. package/dist/mixin/attributesParent.d.ts +50 -0
  27. package/dist/mixin/attributesParent.js +78 -0
  28. package/dist/mixin/fixed.d.ts +6 -0
  29. package/dist/mixin/fixed.js +30 -0
  30. package/dist/mixin/flagsParent.d.ts +72 -0
  31. package/dist/mixin/flagsParent.js +59 -0
  32. package/dist/mixin/hidden.d.ts +5 -0
  33. package/dist/mixin/hidden.js +24 -0
  34. package/dist/mixin/magicLinkParent.d.ts +40 -0
  35. package/dist/mixin/magicLinkParent.js +42 -0
  36. package/dist/mixin/singleLine.d.ts +6 -0
  37. package/dist/mixin/singleLine.js +26 -0
  38. package/dist/mixin/sol.d.ts +6 -0
  39. package/dist/mixin/sol.js +43 -0
  40. package/dist/mixin/syntax.d.ts +9 -0
  41. package/dist/mixin/syntax.js +50 -0
  42. package/dist/parser/braces.js +131 -0
  43. package/dist/parser/commentAndExt.js +90 -0
  44. package/dist/parser/converter.js +41 -0
  45. package/dist/parser/externalLinks.js +32 -0
  46. package/dist/parser/hrAndDoubleUnderscore.js +41 -0
  47. package/dist/parser/html.js +39 -0
  48. package/dist/parser/links.js +95 -0
  49. package/dist/parser/list.js +68 -0
  50. package/dist/parser/magicLinks.js +43 -0
  51. package/dist/parser/quotes.js +68 -0
  52. package/dist/parser/selector.js +162 -0
  53. package/dist/parser/table.js +124 -0
  54. package/dist/src/arg.d.ts +58 -0
  55. package/dist/src/arg.js +196 -0
  56. package/dist/src/atom.d.ts +12 -0
  57. package/dist/src/atom.js +27 -0
  58. package/dist/src/attribute.d.ts +69 -0
  59. package/dist/src/attribute.js +475 -0
  60. package/dist/src/attributes.d.ts +109 -0
  61. package/dist/src/attributes.js +346 -0
  62. package/dist/src/converter.d.ts +33 -0
  63. package/dist/src/converter.js +134 -0
  64. package/dist/src/converterFlags.d.ts +87 -0
  65. package/dist/src/converterFlags.js +226 -0
  66. package/dist/src/converterRule.d.ts +77 -0
  67. package/dist/src/converterRule.js +209 -0
  68. package/dist/src/extLink.d.ts +43 -0
  69. package/dist/src/extLink.js +186 -0
  70. package/dist/src/gallery.d.ts +54 -0
  71. package/dist/src/gallery.js +126 -0
  72. package/dist/src/heading.d.ts +47 -0
  73. package/dist/src/heading.js +190 -0
  74. package/dist/src/hidden.d.ts +7 -0
  75. package/dist/src/hidden.js +23 -0
  76. package/dist/src/html.d.ts +65 -0
  77. package/dist/src/html.js +337 -0
  78. package/dist/src/imageParameter.d.ts +65 -0
  79. package/dist/src/imageParameter.js +246 -0
  80. package/dist/src/imagemap.d.ts +56 -0
  81. package/dist/src/imagemap.js +150 -0
  82. package/dist/src/imagemapLink.d.ts +31 -0
  83. package/dist/src/imagemapLink.js +93 -0
  84. package/dist/src/index.d.ts +143 -0
  85. package/dist/src/index.js +803 -0
  86. package/dist/src/link/base.d.ts +52 -0
  87. package/dist/src/link/base.js +209 -0
  88. package/dist/src/link/category.d.ts +13 -0
  89. package/dist/src/link/category.js +28 -0
  90. package/dist/src/link/file.d.ts +96 -0
  91. package/dist/src/link/file.js +265 -0
  92. package/dist/src/link/galleryImage.d.ts +32 -0
  93. package/dist/src/link/galleryImage.js +157 -0
  94. package/dist/src/link/index.d.ts +56 -0
  95. package/dist/src/link/index.js +133 -0
  96. package/dist/src/magicLink.d.ts +59 -0
  97. package/dist/src/magicLink.js +199 -0
  98. package/dist/src/nested.d.ts +43 -0
  99. package/dist/src/nested.js +92 -0
  100. package/dist/src/nowiki/base.d.ts +31 -0
  101. package/dist/src/nowiki/base.js +93 -0
  102. package/dist/src/nowiki/comment.d.ts +25 -0
  103. package/dist/src/nowiki/comment.js +68 -0
  104. package/dist/src/nowiki/dd.d.ts +8 -0
  105. package/dist/src/nowiki/dd.js +24 -0
  106. package/dist/src/nowiki/doubleUnderscore.d.ts +18 -0
  107. package/dist/src/nowiki/doubleUnderscore.js +101 -0
  108. package/dist/src/nowiki/hr.d.ts +5 -0
  109. package/dist/src/nowiki/hr.js +63 -0
  110. package/dist/src/nowiki/index.d.ts +16 -0
  111. package/dist/src/nowiki/index.js +21 -0
  112. package/dist/src/nowiki/list.d.ts +16 -0
  113. package/dist/src/nowiki/list.js +97 -0
  114. package/dist/src/nowiki/listBase.d.ts +5 -0
  115. package/dist/src/nowiki/listBase.js +61 -0
  116. package/dist/src/nowiki/noinclude.d.ts +10 -0
  117. package/dist/src/nowiki/noinclude.js +23 -0
  118. package/dist/src/nowiki/quote.d.ts +14 -0
  119. package/dist/src/nowiki/quote.js +116 -0
  120. package/dist/src/onlyinclude.d.ts +16 -0
  121. package/dist/src/onlyinclude.js +57 -0
  122. package/dist/src/paramTag/index.d.ts +37 -0
  123. package/dist/src/paramTag/index.js +69 -0
  124. package/dist/src/paramTag/inputbox.d.ts +8 -0
  125. package/dist/src/paramTag/inputbox.js +22 -0
  126. package/dist/src/parameter.d.ts +70 -0
  127. package/dist/src/parameter.js +262 -0
  128. package/dist/src/pre.d.ts +28 -0
  129. package/dist/src/pre.js +55 -0
  130. package/dist/src/syntax.d.ts +20 -0
  131. package/dist/src/syntax.js +86 -0
  132. package/dist/src/table/base.d.ts +30 -0
  133. package/dist/src/table/base.js +132 -0
  134. package/dist/src/table/index.d.ts +233 -0
  135. package/dist/src/table/index.js +396 -0
  136. package/dist/src/table/td.d.ts +92 -0
  137. package/dist/src/table/td.js +337 -0
  138. package/dist/src/table/tr.d.ts +32 -0
  139. package/dist/src/table/tr.js +58 -0
  140. package/dist/src/table/trBase.d.ts +53 -0
  141. package/dist/src/table/trBase.js +154 -0
  142. package/dist/src/tagPair/ext.d.ts +32 -0
  143. package/dist/src/tagPair/ext.js +203 -0
  144. package/dist/src/tagPair/include.d.ts +35 -0
  145. package/dist/src/tagPair/include.js +63 -0
  146. package/dist/src/tagPair/index.d.ts +27 -0
  147. package/dist/src/tagPair/index.js +128 -0
  148. package/dist/src/transclude.d.ts +167 -0
  149. package/dist/src/transclude.js +710 -0
  150. package/dist/util/constants.js +113 -0
  151. package/dist/util/debug.js +88 -0
  152. package/dist/util/diff.js +82 -0
  153. package/dist/util/lint.js +30 -0
  154. package/dist/util/string.js +53 -0
  155. package/errors/README +1 -0
  156. package/i18n/zh-hans.json +1 -0
  157. package/i18n/zh-hant.json +1 -0
  158. package/package.json +15 -30
  159. package/printed/README +1 -0
  160. package/bundle/bundle.min.js +0 -36
  161. package/extensions/dist/base.js +0 -64
  162. package/extensions/dist/editor.js +0 -159
  163. package/extensions/dist/highlight.js +0 -59
  164. package/extensions/dist/lint.js +0 -48
  165. package/extensions/editor.css +0 -63
  166. package/extensions/ui.css +0 -141
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseExternalLinks = void 0;
4
+ const string_1 = require("../util/string");
5
+ const constants_1 = require("../util/constants");
6
+ const index_1 = require("../index");
7
+ const extLink_1 = require("../src/extLink");
8
+ /**
9
+ * 解析外部链接
10
+ * @param wikitext
11
+ * @param config
12
+ * @param accum
13
+ */
14
+ const parseExternalLinks = (wikitext, config = index_1.default.getConfig(), accum = []) => {
15
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
16
+ /\[((?:\[[\da-f:.]+\]|[^[\]\t\n\p{Zs}])[^[\]\t\n\p{Zs}]*(?=[[\]\t\p{Zs}]|\0\d))(\p{Zs}*(?=\P{Zs}))([^\]\n]*)\]/giu;
17
+ const regex = new RegExp(`\\[((?:(?:${config.protocol}|//)${string_1.extUrlCharFirst}|\0\\d+m\x7F)${string_1.extUrlChar}(?=[[\\]<>"\\t\\p{Zs}]|\0\\d))`
18
+ + '(\\p{Zs}*(?=\\P{Zs}))([^\\]\x01-\x08\x0A-\x1F\uFFFD]*)\\]', 'giu');
19
+ return wikitext.replace(regex, (_, url, space, text) => {
20
+ const { length } = accum, mt = /&[lg]t;/u.exec(url);
21
+ if (mt) {
22
+ url = url.slice(0, mt.index);
23
+ space = '';
24
+ text = `${url.slice(mt.index)}${space}${text}`;
25
+ }
26
+ // @ts-expect-error abstract class
27
+ new extLink_1.ExtLinkToken(url, space, text, config, accum);
28
+ return `\0${length}w\x7F`;
29
+ });
30
+ };
31
+ exports.parseExternalLinks = parseExternalLinks;
32
+ constants_1.parsers['parseExternalLinks'] = __filename;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseHrAndDoubleUnderscore = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const index_1 = require("../index");
6
+ const hr_1 = require("../src/nowiki/hr");
7
+ const doubleUnderscore_1 = require("../src/nowiki/doubleUnderscore");
8
+ const heading_1 = require("../src/heading");
9
+ /**
10
+ * 解析`<hr>`和状态开关
11
+ * @param {Token} root 根节点
12
+ * @param config
13
+ * @param accum
14
+ */
15
+ const parseHrAndDoubleUnderscore = ({ firstChild: { data }, type, name }, config = index_1.default.getConfig(), accum = []) => {
16
+ const { doubleUnderscore } = config, insensitive = new Set(doubleUnderscore[0]), sensitive = new Set(doubleUnderscore[1]);
17
+ if (type !== 'root' && (type !== 'ext-inner' || name !== 'poem')) {
18
+ data = `\0${data}`;
19
+ }
20
+ data = data.replace(/^((?:\0\d+c\x7F)*)(-{4,})/gmu, (_, lead, m) => {
21
+ // @ts-expect-error abstract class
22
+ new hr_1.HrToken(m, config, accum);
23
+ return `${lead}\0${accum.length - 1}r\x7F`;
24
+ }).replace(new RegExp(`__(${doubleUnderscore.flat().join('|')})__`, 'giu'), (m, p1) => {
25
+ const caseSensitive = sensitive.has(p1);
26
+ if (caseSensitive || insensitive.has(p1.toLowerCase())) {
27
+ // @ts-expect-error abstract class
28
+ new doubleUnderscore_1.DoubleUnderscoreToken(p1, caseSensitive, config, accum);
29
+ return `\0${accum.length - 1}u\x7F`;
30
+ }
31
+ return m;
32
+ }).replace(/^((?:\0\d+c\x7F)*)(={1,6})(.+)\2((?:[^\S\n]|\0\d+c\x7F)*)$/gmu, (_, lead, equals, heading, trail) => {
33
+ const text = `${lead}\0${accum.length}h\x7F`;
34
+ // @ts-expect-error abstract class
35
+ new heading_1.HeadingToken(equals.length, [heading, trail], config, accum);
36
+ return text;
37
+ });
38
+ return type === 'root' || type === 'ext-inner' && name === 'poem' ? data : data.slice(1);
39
+ };
40
+ exports.parseHrAndDoubleUnderscore = parseHrAndDoubleUnderscore;
41
+ constants_1.parsers['parseHrAndDoubleUnderscore'] = __filename;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseHtml = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const index_1 = require("../index");
6
+ const attributes_1 = require("../src/attributes");
7
+ const html_1 = require("../src/html");
8
+ /**
9
+ * 解析HTML标签
10
+ * @param wikitext
11
+ * @param config
12
+ * @param accum
13
+ */
14
+ const parseHtml = (wikitext, config = index_1.default.getConfig(), accum = []) => {
15
+ const regex = /^(\/?)([a-z][^\s/>]*)((?:\s|\/(?!>))[^>]*?)?(\/?>)([^<]*)$/iu, elements = new Set(config.html.flat()), bits = wikitext.split('<');
16
+ let text = bits.shift();
17
+ for (const x of bits) {
18
+ const mt = regex.exec(x), t = mt?.[2], name = t?.toLowerCase();
19
+ if (!mt || !elements.has(name)) {
20
+ text += `<${x}`;
21
+ continue;
22
+ }
23
+ const [, slash, , params = '', brace, rest] = mt,
24
+ // @ts-expect-error abstract class
25
+ attr = new attributes_1.AttributesToken(params, 'html-attrs', name, config, accum), itemprop = attr.getAttr('itemprop');
26
+ if (name === 'meta' && (itemprop === undefined || attr.getAttr('content') === undefined)
27
+ || name === 'link' && (itemprop === undefined || attr.getAttr('href') === undefined)) {
28
+ text += `<${x}`;
29
+ accum.pop();
30
+ continue;
31
+ }
32
+ text += `\0${accum.length}x\x7F${rest}`;
33
+ // @ts-expect-error abstract class
34
+ new html_1.HtmlToken(t, attr, slash === '/', brace === '/>', config, accum);
35
+ }
36
+ return text;
37
+ };
38
+ exports.parseHtml = parseHtml;
39
+ constants_1.parsers['parseHtml'] = __filename;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseLinks = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const index_1 = require("../index");
6
+ const index_2 = require("../src/link/index");
7
+ const file_1 = require("../src/link/file");
8
+ const category_1 = require("../src/link/category");
9
+ /**
10
+ * 解析内部链接
11
+ * @param wikitext
12
+ * @param config
13
+ * @param accum
14
+ */
15
+ const parseLinks = (wikitext, config = index_1.default.getConfig(), accum = []) => {
16
+ const { parseQuotes } = require('./quotes');
17
+ const regex = /^((?:(?!\0\d+!\x7F)[^\n[\]{}|])+)(?:(\||\0\d+!\x7F)(.*?[^\]]))?\]\](.*)$/su, regexImg = /^((?:(?!\0\d+!\x7F)[^\n[\]{}|])+)(\||\0\d+!\x7F)(.*)$/su, regexExt = new RegExp(`^\\s*(?:${config.protocol}|//)`, 'iu'), bits = wikitext.split('[[');
18
+ let s = bits.shift();
19
+ for (let i = 0; i < bits.length; i++) {
20
+ let mightBeImg = false, link, delimiter, text, after;
21
+ const x = bits[i], m = regex.exec(x);
22
+ if (m) {
23
+ [, link, delimiter, text, after] = m;
24
+ if (after.startsWith(']') && text?.includes('[')) {
25
+ text += ']';
26
+ after = after.slice(1);
27
+ }
28
+ }
29
+ else {
30
+ const m2 = regexImg.exec(x);
31
+ if (m2) {
32
+ mightBeImg = true;
33
+ [, link, delimiter, text] = m2;
34
+ }
35
+ }
36
+ if (link === undefined || regexExt.test(link) || /\0\d+[exhbru]\x7F/u.test(link)) {
37
+ s += `[[${x}`;
38
+ continue;
39
+ }
40
+ const force = link.trim().startsWith(':');
41
+ if (force && mightBeImg) {
42
+ s += `[[${x}`;
43
+ continue;
44
+ }
45
+ const title = index_1.default.normalizeTitle(link, 0, false, config, true, true, true), { ns, valid, interwiki } = title;
46
+ if (!valid) {
47
+ s += `[[${x}`;
48
+ continue;
49
+ }
50
+ else if (mightBeImg) {
51
+ if (interwiki || ns !== 6) {
52
+ s += `[[${x}`;
53
+ continue;
54
+ }
55
+ let found = false;
56
+ for (i++; i < bits.length; i++) {
57
+ const next = bits[i], p = next.split(']]');
58
+ if (p.length > 2) {
59
+ found = true;
60
+ text += `[[${p[0]}]]${p[1]}`;
61
+ after = p.slice(2).join(']]');
62
+ break;
63
+ }
64
+ else if (p.length === 2) {
65
+ text += `[[${p[0]}]]${p[1]}`;
66
+ }
67
+ else {
68
+ text += `[[${next}`;
69
+ break;
70
+ }
71
+ }
72
+ text = (0, exports.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 = index_2.LinkToken;
81
+ if (!force) {
82
+ if (!interwiki && ns === 6) {
83
+ SomeLinkToken = file_1.FileToken;
84
+ }
85
+ else if (!interwiki && ns === 14) {
86
+ SomeLinkToken = category_1.CategoryToken;
87
+ }
88
+ }
89
+ // @ts-expect-error abstract class
90
+ new SomeLinkToken(link, text, config, accum, delimiter);
91
+ }
92
+ return s;
93
+ };
94
+ exports.parseLinks = parseLinks;
95
+ constants_1.parsers['parseLinks'] = __filename;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseList = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const index_1 = require("../index");
6
+ const list_1 = require("../src/nowiki/list");
7
+ const dd_1 = require("../src/nowiki/dd");
8
+ /**
9
+ * 解析列表
10
+ * @param wikitext
11
+ * @param config
12
+ * @param accum
13
+ */
14
+ const parseList = (wikitext, config = index_1.default.getConfig(), accum = []) => {
15
+ const mt = /^((?:\0\d+c\x7F)*)([;:*#]+)/u.exec(wikitext);
16
+ if (!mt) {
17
+ return wikitext;
18
+ }
19
+ const [total, comment, prefix] = mt;
20
+ let text = `${comment}\0${accum.length}d\x7F${wikitext.slice(total.length)}`, dt = prefix.split(';').length - 1;
21
+ // @ts-expect-error abstract class
22
+ new list_1.ListToken(prefix, config, accum);
23
+ if (!dt) {
24
+ return text;
25
+ }
26
+ let regex = /:+|-\{/gu, ex = regex.exec(text), lc = 0;
27
+ /**
28
+ * 创建`DdToken`
29
+ * @param syntax `:`
30
+ * @param index 起点
31
+ */
32
+ const dd = (syntax, index) => {
33
+ // @ts-expect-error abstract class
34
+ new dd_1.DdToken(syntax, config, accum);
35
+ return `${text.slice(0, index)}\0${accum.length - 1}d\x7F${text.slice(index + syntax.length)}`;
36
+ };
37
+ while (ex && dt) {
38
+ const { 0: syntax, index } = ex;
39
+ if (syntax.startsWith(':')) {
40
+ if (syntax.length >= dt) {
41
+ return dd(syntax.slice(0, dt), index);
42
+ }
43
+ dt -= syntax.length;
44
+ regex.lastIndex = index + 4 + String(accum.length).length;
45
+ text = dd(syntax, index);
46
+ }
47
+ else if (syntax === '-{') {
48
+ if (!lc) {
49
+ const { lastIndex } = regex;
50
+ regex = /-\{|\}-/gu;
51
+ regex.lastIndex = lastIndex;
52
+ }
53
+ lc++;
54
+ }
55
+ else {
56
+ lc--;
57
+ if (!lc) {
58
+ const { lastIndex } = regex;
59
+ regex = /:+|-\{/gu;
60
+ regex.lastIndex = lastIndex;
61
+ }
62
+ }
63
+ ex = regex.exec(text);
64
+ }
65
+ return text;
66
+ };
67
+ exports.parseList = parseList;
68
+ constants_1.parsers['parseList'] = __filename;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseMagicLinks = void 0;
4
+ const string_1 = require("../util/string");
5
+ const constants_1 = require("../util/constants");
6
+ const index_1 = require("../index");
7
+ const magicLink_1 = require("../src/magicLink");
8
+ /**
9
+ * 解析自由外链
10
+ * @param wikitext
11
+ * @param config
12
+ * @param accum
13
+ */
14
+ const parseMagicLinks = (wikitext, config = index_1.default.getConfig(), accum = []) => {
15
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
16
+ /(^|[^\p{L}\d_])((?:\[[\da-f:.]+\]|[^[\]<>"\t\n\p{Zs}])(?:[^[\]<>"\0\t\n\p{Zs}]|\0\d+c\x7F)*)/giu;
17
+ const regex = new RegExp(`(^|[^\\p{L}\\d_])(?:${config.protocol})(${string_1.extUrlCharFirst}${string_1.extUrlChar})`, 'giu');
18
+ return wikitext.replace(regex, (m, lead, p1) => {
19
+ let trail = '', url = lead ? m.slice(1) : m;
20
+ const m2 = /&(?:lt|gt|nbsp|#x0*(?:3[ce]|a0)|#0*(?:6[02]|160));/iu.exec(url);
21
+ if (m2) {
22
+ trail = url.slice(m2.index);
23
+ url = url.slice(0, m2.index);
24
+ }
25
+ const sep = new RegExp(`[,;.:!?${url.includes('(') ? '' : ')'}]+$`, 'u'), sepChars = sep.exec(url);
26
+ if (sepChars) {
27
+ let correction = 0;
28
+ if (sepChars[0].startsWith(';') && /&(?:[a-z]+|#x[\da-f]+|#\d+)$/iu.test(url.slice(0, sepChars.index))) {
29
+ correction = 1;
30
+ }
31
+ trail = `${url.slice(sepChars.index + correction)}${trail}`;
32
+ url = url.slice(0, sepChars.index + correction);
33
+ }
34
+ if (trail.length >= p1.length) {
35
+ return m;
36
+ }
37
+ // @ts-expect-error abstract class
38
+ new magicLink_1.MagicLinkToken(url, false, config, accum);
39
+ return `${lead}\0${accum.length - 1}w\x7F${trail}`;
40
+ });
41
+ };
42
+ exports.parseMagicLinks = parseMagicLinks;
43
+ constants_1.parsers['parseMagicLinks'] = __filename;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseQuotes = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const index_1 = require("../index");
6
+ const quote_1 = require("../src/nowiki/quote");
7
+ /**
8
+ * 解析单引号
9
+ * @param wikitext
10
+ * @param config
11
+ * @param accum
12
+ */
13
+ const parseQuotes = (wikitext, config = index_1.default.getConfig(), accum = []) => {
14
+ const arr = wikitext.split(/('{2,})/u), { length } = arr;
15
+ if (length === 1) {
16
+ return wikitext;
17
+ }
18
+ let nBold = 0, nItalic = 0, firstSingle, firstMulti, firstSpace;
19
+ for (let i = 1; i < length; i += 2) {
20
+ const len = arr[i].length;
21
+ switch (len) {
22
+ case 2:
23
+ nItalic++;
24
+ break;
25
+ case 4:
26
+ arr[i - 1] += `'`;
27
+ arr[i] = `'''`;
28
+ // fall through
29
+ case 3:
30
+ nBold++;
31
+ if (firstSingle !== undefined) {
32
+ break;
33
+ }
34
+ else if (arr[i - 1].endsWith(' ')) {
35
+ if (firstMulti === undefined && firstSpace === undefined) {
36
+ firstSpace = i;
37
+ }
38
+ }
39
+ else if (arr[i - 1].slice(-2, -1) === ' ') {
40
+ firstSingle = i;
41
+ }
42
+ else {
43
+ firstMulti ??= i;
44
+ }
45
+ break;
46
+ default:
47
+ arr[i - 1] += `'`.repeat(len - 5);
48
+ arr[i] = `'''''`;
49
+ nItalic++;
50
+ nBold++;
51
+ }
52
+ }
53
+ if (nItalic % 2 === 1 && nBold % 2 === 1) {
54
+ const i = firstSingle ?? firstMulti ?? firstSpace;
55
+ if (i !== undefined) {
56
+ arr[i] = `''`;
57
+ arr[i - 1] += `'`;
58
+ }
59
+ }
60
+ for (let i = 1; i < length; i += 2) {
61
+ // @ts-expect-error abstract class
62
+ new quote_1.QuoteToken(arr[i], config, accum);
63
+ arr[i] = `\0${accum.length - 1}q\x7F`;
64
+ }
65
+ return arr.join('');
66
+ };
67
+ exports.parseQuotes = parseQuotes;
68
+ constants_1.parsers['parseQuotes'] = __filename;
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseSelector = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const string_1 = require("../util/string");
6
+ const index_1 = require("../index");
7
+ const simplePseudos = new Set([
8
+ 'root',
9
+ 'first-child',
10
+ 'first-of-type',
11
+ 'last-child',
12
+ 'last-of-type',
13
+ 'only-child',
14
+ 'only-of-type',
15
+ 'empty',
16
+ 'parent',
17
+ 'header',
18
+ 'hidden',
19
+ 'visible',
20
+ 'only-whitespace',
21
+ 'any-link',
22
+ 'local-link',
23
+ 'invalid',
24
+ 'required',
25
+ 'optional',
26
+ ]);
27
+ const complexPseudos = [
28
+ 'is',
29
+ 'not',
30
+ 'nth-child',
31
+ 'nth-of-type',
32
+ 'nth-last-child',
33
+ 'nth-last-of-type',
34
+ 'contains',
35
+ 'has',
36
+ 'lang',
37
+ 'regex',
38
+ ];
39
+ const specialChars = [
40
+ ['[', '&lbrack;'],
41
+ [']', '&rbrack;'],
42
+ ['(', '&lpar;'],
43
+ [')', '&rpar;'],
44
+ ['"', '&quot;'],
45
+ [`'`, '&apos;'],
46
+ [':', '&colon;'],
47
+ ['\\', '&bsol;'],
48
+ ['&', '&amp;'],
49
+ ];
50
+ const pseudoRegex = new RegExp(`:(${complexPseudos.join('|')})$`, 'u'), regularRegex = /[[(,>+~]|\s+/u, attributeRegex = /^\s*(\w+)\s*(?:([~|^$*!]?=)\s*("[^"]*"|'[^']*'|[^\s[\]]+)(?:\s+(i))?\s*)?\]/u, functionRegex = /^(\s*"[^"]*"\s*|\s*'[^']*'\s*|[^()]*)\)/u, grouping = new Set([',', '>', '+', '~']), combinator = new Set(['>', '+', '~', '']);
51
+ /**
52
+ * 清理转义符号
53
+ * @param selector
54
+ */
55
+ const sanitize = (selector) => {
56
+ for (const [c, escaped] of specialChars) {
57
+ selector = selector.replace(new RegExp((0, string_1.escapeRegExp)(`\\${c}`), 'gu'), escaped);
58
+ }
59
+ return selector;
60
+ };
61
+ /**
62
+ * 还原转义符号
63
+ * @param selector
64
+ */
65
+ const desanitize = (selector) => {
66
+ for (const [c, escaped] of specialChars) {
67
+ selector = selector.replace(new RegExp(escaped, 'gu'), c);
68
+ }
69
+ return selector.trim();
70
+ };
71
+ /**
72
+ * 去除首尾的引号
73
+ * @param val 属性值或伪选择器函数的参数
74
+ */
75
+ const deQuote = (val) => /^(["']).*\1$/u.test(val) ? val.slice(1, -1) : val.trim();
76
+ /**
77
+ * 解析选择器
78
+ * @param selector
79
+ * @throws `SyntaxError` 非法的选择器
80
+ */
81
+ const parseSelector = (selector) => {
82
+ selector = selector.trim();
83
+ const stack = [[[]]];
84
+ let sanitized = sanitize(selector), regex = regularRegex, mt = regex.exec(sanitized), [condition] = stack, [step] = condition;
85
+ /**
86
+ * 解析简单伪选择器
87
+ * @param index 伪选择器的终点位置
88
+ * @throws `SyntaxError` 非法的选择器
89
+ */
90
+ const pushSimple = (index) => {
91
+ const str = sanitized.slice(0, index), pieces = str.trim().split(':'),
92
+ // eslint-disable-next-line unicorn/explicit-length-check
93
+ i = pieces.slice(1).findIndex(pseudo => simplePseudos.has(pseudo)) + 1 || pieces.length;
94
+ if (pieces.slice(i).some(pseudo => !simplePseudos.has(pseudo))) {
95
+ throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
96
+ }
97
+ step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
98
+ };
99
+ while (mt) {
100
+ let { 0: syntax, index } = mt;
101
+ if (syntax.trim() === '') {
102
+ index += syntax.length;
103
+ const char = sanitized[index];
104
+ syntax = grouping.has(char) ? char : '';
105
+ }
106
+ if (syntax === ',') { // 情形1:并列
107
+ pushSimple(index);
108
+ condition = [[]];
109
+ [step] = condition;
110
+ stack.push(condition);
111
+ }
112
+ else if (combinator.has(syntax)) { // 情形2:关系
113
+ pushSimple(index);
114
+ if (!step.some(Boolean)) {
115
+ throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
116
+ }
117
+ step.relation = syntax;
118
+ step = [];
119
+ condition.push(step);
120
+ }
121
+ else if (syntax === '[') { // 情形3:属性开启
122
+ pushSimple(index);
123
+ regex = attributeRegex;
124
+ }
125
+ else if (syntax.endsWith(']')) { // 情形4:属性闭合
126
+ mt[3] &&= desanitize(deQuote(mt[3]));
127
+ step.push(mt.slice(1));
128
+ regex = regularRegex;
129
+ }
130
+ else if (syntax === '(') { // 情形5:伪选择器开启
131
+ const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
132
+ if (!pseudoExec) {
133
+ throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
134
+ }
135
+ pushSimple(pseudoExec.index);
136
+ step.push(pseudoExec[1]); // 临时存放复杂伪选择器
137
+ regex = functionRegex;
138
+ }
139
+ else { // 情形6:伪选择器闭合
140
+ mt.push(step.pop());
141
+ mt[1] &&= deQuote(mt[1]);
142
+ step.push(mt.slice(1));
143
+ regex = regularRegex;
144
+ }
145
+ sanitized = sanitized.slice(index + syntax.length);
146
+ if (grouping.has(syntax)) {
147
+ sanitized = sanitized.trim();
148
+ }
149
+ mt = regex.exec(sanitized);
150
+ }
151
+ if (regex === regularRegex) {
152
+ pushSimple(Infinity);
153
+ const pseudos = new Set(stack.flat(2).filter((e) => typeof e === 'string' && e.startsWith(':')));
154
+ if (pseudos.size > 0) {
155
+ index_1.default.warn('检测到伪选择器,请确认是否需要将":"转义成"\\:"。', pseudos);
156
+ }
157
+ return stack;
158
+ }
159
+ throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
160
+ };
161
+ exports.parseSelector = parseSelector;
162
+ constants_1.parsers['parseSelector'] = __filename;
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTable = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const index_1 = require("../index");
6
+ const index_2 = require("../src/index");
7
+ const index_3 = require("../src/table/index");
8
+ const tr_1 = require("../src/table/tr");
9
+ const td_1 = require("../src/table/td");
10
+ const dd_1 = require("../src/nowiki/dd");
11
+ /**
12
+ * 判断是否为表格行或表格
13
+ * @param token 表格节点
14
+ */
15
+ const isTr = (token) => token.lastChild.constructor !== index_2.Token;
16
+ /**
17
+ * 解析表格,注意`tr`和`td`包含开头的换行
18
+ * @param {Token & {firstChild: AstText}} root 根节点
19
+ * @param config
20
+ * @param accum
21
+ */
22
+ const parseTable = ({ firstChild: { data }, type, name }, config = index_1.default.getConfig(), accum = []) => {
23
+ const stack = [], lines = data.split('\n');
24
+ let out = type === 'root' || type === 'parameter-value' || type === 'ext-inner' && name === 'poem'
25
+ ? ''
26
+ : `\n${lines.shift()}`, top;
27
+ /**
28
+ * 向表格中插入纯文本
29
+ * @param str 待插入的文本
30
+ * @param topToken 当前解析的表格或表格行
31
+ */
32
+ const push = (str, topToken) => {
33
+ if (!topToken) {
34
+ out += str;
35
+ return;
36
+ }
37
+ const { lastChild } = topToken;
38
+ if (isTr(topToken)) {
39
+ const token = new index_2.Token(str, config, accum);
40
+ token.type = 'table-inter';
41
+ token.setAttribute('stage', 3);
42
+ topToken.insertAt(token);
43
+ }
44
+ else {
45
+ lastChild.setText(String(lastChild) + str);
46
+ }
47
+ },
48
+ /** 取出最近的表格行 */
49
+ pop = () => top.type === 'td' ? stack.pop() : top;
50
+ for (const outLine of lines) {
51
+ top = stack.pop();
52
+ const [spaces] = /^(?:\s|\0\d+c\x7F)*/u.exec(outLine), line = outLine.slice(spaces.length), matchesStart = /^(:*)((?:\s|\0\d+c\x7F)*)(\{\||\{(?:\0\d+c\x7F)*\0\d+!\x7F|\0\d+\{\x7F)(.*)$/u
53
+ .exec(line);
54
+ if (matchesStart) {
55
+ while (top && top.type !== 'td') {
56
+ top = stack.pop();
57
+ }
58
+ const [, indent, moreSpaces, tableSyntax, attr] = matchesStart;
59
+ if (indent) {
60
+ // @ts-expect-error abstract class
61
+ new dd_1.DdToken(indent, config, accum);
62
+ }
63
+ push(`\n${spaces}${indent && `\0${accum.length - 1}d\x7F`}${moreSpaces}\0${accum.length}b\x7F`, top);
64
+ // @ts-expect-error abstract class
65
+ stack.push(...top ? [top] : [], new index_3.TableToken(tableSyntax, attr, config, accum));
66
+ continue;
67
+ }
68
+ else if (!top) {
69
+ out += `\n${outLine}`;
70
+ continue;
71
+ }
72
+ // eslint-disable-next-line @stylistic/operator-linebreak
73
+ const matches = /^(?:(\|\}|\0\d+!\x7F\}|\0\d+\}\x7F)|(\|-+|\0\d+!\x7F-+|\0\d+-\x7F-*)(?!-)|(!|(?:\||\0\d+!\x7F)\+?))(.*)$/u
74
+ .exec(line);
75
+ if (!matches) {
76
+ push(`\n${outLine}`, top);
77
+ stack.push(top);
78
+ continue;
79
+ }
80
+ const [, closing, row, cell, attr] = matches;
81
+ if (closing) {
82
+ while (top.type !== 'table') {
83
+ top = stack.pop();
84
+ }
85
+ top.close(`\n${spaces}${closing}`, true);
86
+ push(attr, stack[stack.length - 1]);
87
+ }
88
+ else if (row) {
89
+ top = pop();
90
+ if (top.type === 'tr') {
91
+ top = stack.pop();
92
+ }
93
+ // @ts-expect-error abstract class
94
+ const tr = new tr_1.TrToken(`\n${spaces}${row}`, attr, config, accum);
95
+ stack.push(top, tr);
96
+ top.insertAt(tr);
97
+ }
98
+ else {
99
+ top = pop();
100
+ const regex = cell === '!' ? /!!|(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu : /(?:\||\0\d+!\x7F){2}|\0\d+\+\x7F/gu;
101
+ let mt = regex.exec(attr), lastIndex = 0, lastSyntax = `\n${spaces}${cell}`;
102
+ /**
103
+ * 创建表格单元格
104
+ * @param tr 当前表格行
105
+ */
106
+ const createTd = (tr) => {
107
+ // @ts-expect-error abstract class
108
+ const td = new td_1.TdToken(lastSyntax, attr.slice(lastIndex, mt?.index), config, accum);
109
+ tr.insertAt(td);
110
+ return td;
111
+ };
112
+ while (mt) {
113
+ createTd(top);
114
+ ({ lastIndex } = regex);
115
+ [lastSyntax] = mt;
116
+ mt = regex.exec(attr);
117
+ }
118
+ stack.push(top, createTd(top));
119
+ }
120
+ }
121
+ return out.slice(1);
122
+ };
123
+ exports.parseTable = parseTable;
124
+ constants_1.parsers['parseTable'] = __filename;