wikiparser-node 0.2.1 → 0.2.3

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 (42) hide show
  1. package/config/default.json +8 -45
  2. package/config/moegirl.json +11 -52
  3. package/errors/2022-12-07T10:07:09.577Z +1 -0
  4. package/errors/2022-12-07T10:07:09.577Z.err +11 -0
  5. package/errors/{2022-07-04T22:30:41.785Z.json → 2022-12-07T10:07:09.577Z.json} +1 -1
  6. package/errors/2022-12-07T10:22:31.325Z +1 -0
  7. package/errors/2022-12-07T10:22:31.325Z.err +11 -0
  8. package/errors/2022-12-07T10:22:31.325Z.json +5 -0
  9. package/lib/element.js +8 -10
  10. package/mixin/hidden.js +0 -5
  11. package/package.json +1 -1
  12. package/parser/hrAndDoubleUnderscore.js +1 -1
  13. package/parser/list.js +3 -3
  14. package/parser/table.js +10 -3
  15. package/src/arg.js +0 -5
  16. package/src/attribute.js +17 -29
  17. package/src/converter.js +1 -6
  18. package/src/converterFlags.js +2 -6
  19. package/src/converterRule.js +1 -9
  20. package/src/extLink.js +1 -9
  21. package/src/gallery.js +31 -0
  22. package/src/heading.js +0 -5
  23. package/src/html.js +0 -5
  24. package/src/index.js +5 -6
  25. package/src/link/category.js +2 -5
  26. package/src/link/file.js +1 -0
  27. package/src/link/index.js +29 -6
  28. package/src/magicLink.js +5 -1
  29. package/src/nowiki/dd.js +4 -9
  30. package/src/nowiki/hr.js +0 -5
  31. package/src/nowiki/index.js +1 -1
  32. package/src/nowiki/quote.js +0 -5
  33. package/src/onlyinclude.js +1 -1
  34. package/src/parameter.js +0 -5
  35. package/src/syntax.js +0 -5
  36. package/src/table/td.js +1 -1
  37. package/src/tagPair/ext.js +4 -0
  38. package/src/tagPair/index.js +2 -11
  39. package/src/transclude.js +0 -4
  40. package/util/string.js +1 -1
  41. package/errors/2022-07-04T22:30:41.785Z +0 -1
  42. package/errors/2022-07-04T22:30:41.785Z.err +0 -11
@@ -646,65 +646,28 @@
646
646
  "wikt",
647
647
  "wiktionary",
648
648
  "zhwiki",
649
- "acronym",
650
- "c2find",
649
+ "cc",
651
650
  "cm",
652
- "dictionary",
653
- "drumcorpswiki",
654
- "dwjwiki",
655
- "elibre",
656
- "emacswiki",
657
651
  "enmoe",
658
- "enwiki",
659
- "foldoc",
660
- "foxwiki",
661
- "freebsdman",
662
- "gentoo-wiki",
663
- "hammondwiki",
664
- "hrwiki",
652
+ "enwp",
653
+ "fandom",
654
+ "ghia",
655
+ "huiji",
665
656
  "jamoe",
666
- "kmwiki",
657
+ "jawp",
667
658
  "library",
668
- "linuxwiki",
669
- "lojban",
670
- "lqwiki",
671
- "mcwiki",
672
- "meatball",
673
- "memoryalpha",
674
659
  "meta",
675
- "metawiki",
676
- "mozillawiki",
677
- "oeis",
678
- "openwiki",
679
660
  "phab",
680
661
  "pmid",
681
- "pythoninfo",
682
- "rfc",
683
- "s23wiki",
684
- "seattlewireless",
685
- "senseislibrary",
686
662
  "shoutwiki",
687
- "sourceforge",
688
- "squeak",
689
- "theopedia",
690
- "tmbw",
691
- "tmnet",
692
663
  "translatewiki",
693
- "twiki",
694
- "uncyclopedia",
695
- "unreal",
696
- "usemod",
697
- "wiki",
698
- "wikif1",
699
- "wikihow",
700
664
  "zhmoe",
701
665
  "zhmoegirl",
702
666
  "zhtest",
667
+ "zhwp",
703
668
  "文库",
704
- "维基中文",
705
- "维基日文",
706
- "维基英文",
707
669
  "萌娘共享",
670
+ "萌娘文库",
708
671
  "萌百"
709
672
  ],
710
673
  "img": {
@@ -578,20 +578,14 @@
578
578
  ],
579
579
  "protocol": "bitcoin:|ftp://|ftps://|geo:|git://|gopher://|http://|https://|irc://|ircs://|magnet:|mailto:|mms://|news:|nntp://|redis://|sftp://|sip:|sips:|sms:|ssh://|svn://|tel:|telnet://|urn:|worldwind://|xmpp:",
580
580
  "interwiki": [
581
- "arxiv",
582
581
  "commons",
583
- "doi",
584
- "imdb",
585
- "jawiki",
586
582
  "mediawikiwiki",
587
583
  "metawikimedia",
588
584
  "mw",
589
- "wikia",
590
585
  "wikibooks",
591
586
  "wikidata",
592
587
  "wikimedia",
593
588
  "wikinews",
594
- "wikiinfo",
595
589
  "wikipedia",
596
590
  "wikiquote",
597
591
  "wikisource",
@@ -600,66 +594,31 @@
600
594
  "wikivoyage",
601
595
  "wikt",
602
596
  "wiktionary",
603
- "zhwiki",
604
- "acronym",
605
- "c2find",
597
+ "cc",
606
598
  "cm",
607
- "dictionary",
608
- "drumcorpswiki",
609
- "dwjwiki",
610
- "elibre",
611
- "emacswiki",
599
+ "en",
612
600
  "enmoe",
613
- "enwiki",
614
- "foldoc",
615
- "foxwiki",
616
- "freebsdman",
617
- "gentoo-wiki",
618
- "hammondwiki",
619
- "hrwiki",
601
+ "enwp",
602
+ "fandom",
603
+ "ghia",
604
+ "huiji",
605
+ "ja",
620
606
  "jamoe",
621
- "kmwiki",
607
+ "jawp",
622
608
  "library",
623
- "linuxwiki",
624
- "lojban",
625
- "lqwiki",
626
- "mcwiki",
627
- "meatball",
628
- "memoryalpha",
629
609
  "meta",
630
- "metawiki",
631
- "mozillawiki",
632
- "oeis",
633
- "openwiki",
634
610
  "phab",
635
611
  "pmid",
636
- "pythoninfo",
637
- "rfc",
638
- "s23wiki",
639
- "seattlewireless",
640
- "senseislibrary",
641
612
  "shoutwiki",
642
- "sourceforge",
643
- "squeak",
644
- "theopedia",
645
- "tmbw",
646
- "tmnet",
647
613
  "translatewiki",
648
- "twiki",
649
- "uncyclopedia",
650
- "unreal",
651
- "usemod",
652
- "wiki",
653
- "wikif1",
654
- "wikihow",
614
+ "zh",
655
615
  "zhmoe",
656
616
  "zhmoegirl",
657
617
  "zhtest",
618
+ "zhwp",
658
619
  "文库",
659
- "维基中文",
660
- "维基日文",
661
- "维基英文",
662
620
  "萌娘共享",
621
+ "萌娘文库",
663
622
  "萌百"
664
623
  ],
665
624
  "img": {
@@ -0,0 +1 @@
1
+ <!---->:
@@ -0,0 +1,11 @@
1
+ Error: ListToken 不可删除元素!
2
+ at ListToken.removeAt (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/mixin/fixedToken.js:14:9)
3
+ at ListToken.replaceChildren (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/lib/element.js:232:9)
4
+ at ListToken.build (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:500:8)
5
+ at Token.build (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:504:11)
6
+ at Token.parse (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:533:19)
7
+ at /Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/index.js:135:11
8
+ at Object.run (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/index.js:33:19)
9
+ at Object.parse (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/index.js:125:8)
10
+ at REPL8:1:10
11
+ at Script.runInThisContext (node:vm:130:12)
@@ -1,5 +1,5 @@
1
1
  {
2
- "stage": 3,
2
+ "stage": 11,
3
3
  "include": false,
4
4
  "config": "./config/default"
5
5
  }
@@ -0,0 +1 @@
1
+ <!---->:
@@ -0,0 +1,11 @@
1
+ Error: ListToken 不可删除元素!
2
+ at ListToken.removeAt (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/mixin/fixedToken.js:14:9)
3
+ at ListToken.replaceChildren (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/lib/element.js:232:9)
4
+ at ListToken.build (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:500:8)
5
+ at Token.build (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:504:11)
6
+ at Token.parse (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:533:19)
7
+ at /Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/index.js:135:11
8
+ at Object.run (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/index.js:33:19)
9
+ at Object.parse (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/index.js:125:8)
10
+ at REPL68:1:10
11
+ at Script.runInThisContext (node:vm:130:12)
@@ -0,0 +1,5 @@
1
+ {
2
+ "stage": 11,
3
+ "include": false,
4
+ "config": "./config/default"
5
+ }
package/lib/element.js CHANGED
@@ -673,18 +673,11 @@ class AstElement extends AstNode {
673
673
  }
674
674
 
675
675
  /**
676
- * 不作为特殊语法的文字
677
- * @returns {[number, string][]}
676
+ * @returns {boolean}
678
677
  * @complexity `n`
679
678
  */
680
- plain() {
681
- const index = this.getAbsoluteIndex();
682
- return this.childNodes.flatMap((node, i) => {
683
- if (node instanceof AstElement) {
684
- return node.plain();
685
- }
686
- return node ? [[index + this.getRelativeIndex(i), node]] : [];
687
- });
679
+ get eof() {
680
+ return this.type === 'root' || !this.nextSibling && this.parentElement.eof;
688
681
  }
689
682
 
690
683
  /**
@@ -743,6 +736,11 @@ class AstElement extends AstNode {
743
736
  }
744
737
  Parser.info(`${indent}</${type}>`);
745
738
  }
739
+
740
+ /** @param {number} n */
741
+ getLine(n) {
742
+ return this.toString().split('\n', n + 1).at(-1);
743
+ }
746
744
  }
747
745
 
748
746
  Parser.classes.AstElement = __filename;
package/mixin/hidden.js CHANGED
@@ -11,11 +11,6 @@ const hidden = constructor => class extends constructor {
11
11
  text() {
12
12
  return '';
13
13
  }
14
-
15
- /** @returns {[number, string][]} */
16
- plain() {
17
- return [];
18
- }
19
14
  };
20
15
 
21
16
  Parser.mixins.hidden = __filename;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -10,7 +10,7 @@ const parseHrAndDoubleUnderscore = (firstChild, config = Parser.getConfig(), acc
10
10
  const HrToken = require('../src/nowiki/hr'),
11
11
  DoubleUnderscoreToken = require('../src/nowiki/doubleUnderscore'),
12
12
  {doubleUnderscore} = config;
13
- return firstChild.replace(/^-{4,}/mg, m => {
13
+ return firstChild.replace(/(?<=^(?:\x00\d+c\x7f)*)-{4,}/mg, m => {
14
14
  new HrToken(m.length, config, accum);
15
15
  return `\x00${accum.length - 1}r\x7f`;
16
16
  }).replace(new RegExp(`__(${doubleUnderscore.flat().join('|')})__`, 'ig'), /** @param {string} p1 */(m, p1) => {
package/parser/list.js CHANGED
@@ -7,13 +7,13 @@ const /** @type {Parser} */ Parser = require('..');
7
7
  * @param {accum} accum
8
8
  */
9
9
  const parseList = (text, config = Parser.getConfig(), accum = []) => {
10
- const mt = text.match(/^(?:[;:*#]|\x00\d+c\x7f)*[;:*#]/);
10
+ const mt = text.match(/^((?:\x00\d+c\x7f)*)([;:*#]+)/);
11
11
  if (!mt) {
12
12
  return text;
13
13
  }
14
14
  const ListToken = require('../src/nowiki/list'),
15
- [prefix] = mt;
16
- text = `\x00${accum.length}d\x7f${text.slice(prefix.length)}`;
15
+ [total, comment, prefix] = mt;
16
+ text = `${comment}\x00${accum.length}d\x7f${text.slice(total.length)}`;
17
17
  new ListToken(prefix, config, accum);
18
18
  let dt = prefix.split(';').length - 1;
19
19
  if (!dt) {
package/parser/table.js CHANGED
@@ -12,6 +12,7 @@ const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = [])
12
12
  TableToken = require('../src/table'),
13
13
  TrToken = require('../src/table/tr'),
14
14
  TdToken = require('../src/table/td'),
15
+ DdToken = require('../src/nowiki/dd'),
15
16
  /** @type {TrToken[]} */ stack = [],
16
17
  lines = firstChild.split('\n');
17
18
  let out = type === 'root' ? '' : `\n${lines.shift()}`;
@@ -33,10 +34,16 @@ const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = [])
33
34
  let top = stack.pop();
34
35
  const [spaces] = outLine.match(/^(?:\s|\x00\d+c\x7f)*/);
35
36
  const line = outLine.slice(spaces.length),
36
- matchesStart = line.match(/^(:*(?:\s|\x00\d+c\x7f)*)({\||{\x00\d+!\x7f|\x00\d+{\x7f)(.*)/);
37
+ matchesStart = line.match(/^(:*)((?:\s|\x00\d+c\x7f)*)({\||{\x00\d+!\x7f|\x00\d+{\x7f)(.*)/);
37
38
  if (matchesStart) {
38
- const [, indent, tableSyntax, attr] = matchesStart;
39
- push(`\n${spaces}${indent}\x00${accum.length}b\x7f`, top);
39
+ while (top && top.type !== 'td') {
40
+ top = stack.pop();
41
+ }
42
+ const [, indent, moreSpaces, tableSyntax, attr] = matchesStart;
43
+ if (indent) {
44
+ new DdToken(indent, config, accum);
45
+ }
46
+ push(`\n${spaces}${indent && `\x00${accum.length - 1}d\x7f`}${moreSpaces}\x00${accum.length}b\x7f`, top);
40
47
  const table = new TableToken(tableSyntax, attr, config, accum);
41
48
  stack.push(...top ? [top] : [], table);
42
49
  continue;
package/src/arg.js CHANGED
@@ -73,11 +73,6 @@ class ArgToken extends Token {
73
73
  return `{{{${text(this.children.slice(0, 2), '|')}}}}`;
74
74
  }
75
75
 
76
- /** @returns {[number, string][]} */
77
- plain() {
78
- return this.childNodes.length > 1 ? this.children[1].plain() : [];
79
- }
80
-
81
76
  /** @complexity `n` */
82
77
  removeRedundant() {
83
78
  Parser.run(() => {
package/src/attribute.js CHANGED
@@ -82,7 +82,7 @@ class AttributeToken extends Token {
82
82
  * @param {accum} accum
83
83
  */
84
84
  constructor(attr, type, name, config = Parser.getConfig(), accum = []) {
85
- super(attr, config, type !== 'ext-attr', accum, {[`Stage-${stages[type]}`]: ':'});
85
+ super(attr, config, true, accum, {[`Stage-${stages[type]}`]: ':'});
86
86
  this.type = type;
87
87
  this.setAttribute('name', name).#parseAttr();
88
88
  }
@@ -108,31 +108,24 @@ class AttributeToken extends Token {
108
108
  return super.getAttribute(key);
109
109
  }
110
110
 
111
- /** @complexity `n` */
112
- build() {
113
- super.build();
114
- if (this.type === 'ext-attr') {
115
- return this;
116
- }
117
- for (let [key, text] of this.#attr) {
118
- let built = false;
119
- if (key.includes('\x00')) {
120
- this.#attr.delete(key);
121
- key = this.buildFromStr(key).map(String).join('');
122
- built = true;
123
- }
124
- if (typeof text === 'string' && text.includes('\x00')) {
125
- text = this.buildFromStr(text).map(String).join('');
126
- built = true;
127
- }
128
- if (built) {
129
- this.#attr.set(key, text);
111
+ afterBuild() {
112
+ if (this.type !== 'ext-attr') {
113
+ for (let [key, text] of this.#attr) {
114
+ let built = false;
115
+ if (key.includes('\x00')) {
116
+ this.#attr.delete(key);
117
+ key = this.buildFromStr(key).map(String).join('');
118
+ built = true;
119
+ }
120
+ if (typeof text === 'string' && text.includes('\x00')) {
121
+ text = this.buildFromStr(text).map(String).join('');
122
+ built = true;
123
+ }
124
+ if (built) {
125
+ this.#attr.set(key, text);
126
+ }
130
127
  }
131
128
  }
132
- return this;
133
- }
134
-
135
- afterBuild() {
136
129
  const that = this,
137
130
  /** @type {AstListener} */ attributeListener = ({type, target}) => {
138
131
  if (type === 'text' || target !== that) {
@@ -260,11 +253,6 @@ class AttributeToken extends Token {
260
253
  return this.type === 'table-attr' ? normalizeSpace(str) : str;
261
254
  }
262
255
 
263
- /** @returns {[number, string][]} */
264
- plain() {
265
- return [];
266
- }
267
-
268
256
  /**
269
257
  * @param {number} i
270
258
  * @complexity `n`
package/src/converter.js CHANGED
@@ -19,7 +19,7 @@ class ConverterToken extends Token {
19
19
  * @param {accum} accum
20
20
  */
21
21
  constructor(flags, rules, config = Parser.getConfig(), accum = []) {
22
- super(undefined, config, false, accum);
22
+ super(undefined, config, true, accum);
23
23
  this.append(new ConverterFlagsToken(flags, config, accum));
24
24
  if (rules.length) {
25
25
  const [firstRule] = rules,
@@ -65,11 +65,6 @@ class ConverterToken extends Token {
65
65
  return `-{${flags.text()}|${text(rules, ';')}}-`;
66
66
  }
67
67
 
68
- /** @returns {[number, string][]} */
69
- plain() {
70
- return this.children.slice(1).flatMap(child => child.plain());
71
- }
72
-
73
68
  /** @this {ConverterToken & {firstChild: ConverterFlagsToken}} */
74
69
  getAllFlags() {
75
70
  return this.firstChild.getAllFlags();
@@ -17,7 +17,7 @@ class ConverterFlagsToken extends Token {
17
17
  * @param {accum} accum
18
18
  */
19
19
  constructor(flags, config = Parser.getConfig(), accum = []) {
20
- super(undefined, config, false, accum, {AtomToken: ':'});
20
+ super(undefined, config, true, accum, {AtomToken: ':'});
21
21
  this.append(...flags.map(flag => new AtomToken(flag, 'converter-flag', config, accum)));
22
22
  }
23
23
 
@@ -39,6 +39,7 @@ class ConverterFlagsToken extends Token {
39
39
  }
40
40
  };
41
41
  this.addEventListener(['remove', 'insert', 'text', 'replace'], converterFlagsListener);
42
+ return this;
42
43
  }
43
44
 
44
45
  /**
@@ -85,11 +86,6 @@ class ConverterFlagsToken extends Token {
85
86
  return super.text(';');
86
87
  }
87
88
 
88
- /** @returns {[number, string][]} */
89
- plain() {
90
- return [];
91
- }
92
-
93
89
  /**
94
90
  * @param {string} flag
95
91
  * @returns {AtomToken[]}
@@ -77,6 +77,7 @@ class ConverterRuleToken extends Token {
77
77
  }
78
78
  };
79
79
  this.addEventListener(['remove', 'insert', 'text', 'replace'], converterRuleListener);
80
+ return this;
80
81
  }
81
82
 
82
83
  /**
@@ -119,15 +120,6 @@ class ConverterRuleToken extends Token {
119
120
  return super.text(':');
120
121
  }
121
122
 
122
- /** @returns {[number, string][]} */
123
- plain() {
124
- const {firstElementChild, lastElementChild, unidirectional} = this;
125
- if (unidirectional) {
126
- return [...firstElementChild.plain(), ...lastElementChild.plain()];
127
- }
128
- return lastElementChild.plain();
129
- }
130
-
131
123
  noConvert() {
132
124
  for (let i = this.childNodes.length - 2; i >= 0; i--) {
133
125
  super.removeAt(i);
package/src/extLink.js CHANGED
@@ -77,14 +77,6 @@ class ExtLinkToken extends Token {
77
77
  return `[${super.text(' ').replaceAll('\n', ' ')}]`;
78
78
  }
79
79
 
80
- /**
81
- * @returns {[number, string][]}
82
- * @complexity `n`
83
- */
84
- plain() {
85
- return this.childNodes.length === 1 ? [] : this.lastElementChild.plain();
86
- }
87
-
88
80
  /** @this {ExtLinkToken & {firstElementChild: MagicLinkToken}} */
89
81
  getUrl() {
90
82
  return this.firstElementChild.getUrl();
@@ -116,7 +108,7 @@ class ExtLinkToken extends Token {
116
108
  if (this.childNodes.length === 1) {
117
109
  this.appendChild(lastChild);
118
110
  } else {
119
- this.lastElementChild.replaceWith(lastChild);
111
+ this.lastElementChild.safeReplaceWith(lastChild);
120
112
  }
121
113
  this.#space ||= ' ';
122
114
  }
package/src/gallery.js ADDED
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ const /** @type {Parser} */ Parser = require('..'),
4
+ Token = require('.'),
5
+ FileToken = require('./link/file');
6
+
7
+ /**
8
+ * 扩展标签
9
+ * @classdesc `{childNodes: [...FileToken]}`
10
+ */
11
+ class GalleryToken extends Token {
12
+ type = 'ext-inner';
13
+ name = 'gallery';
14
+
15
+ /** @param {accum} accum */
16
+ constructor(wikitext = '', config = Parser.getConfig(), accum = []) {
17
+ super(undefined, config, true, accum, {FileToken: ':'});
18
+ for (const line of wikitext.split('\n')) {
19
+ const [link, ...text] = line.split('|'),
20
+ title = Parser.normalizeTitle(link, 6, false, config);
21
+ this.appendChild(new FileToken(link, text.length ? text.join('|') : undefined, title, config, accum));
22
+ }
23
+ }
24
+
25
+ toString() {
26
+ return super.toString('\n');
27
+ }
28
+ }
29
+
30
+ Parser.classes.GalleryToken = __filename;
31
+ module.exports = GalleryToken;
package/src/heading.js CHANGED
@@ -63,11 +63,6 @@ class HeadingToken extends fixedToken(sol(Token)) {
63
63
  return `${this.prependNewLine()}${equals}${this.firstElementChild.text()}${equals}${this.appendNewLine()}`;
64
64
  }
65
65
 
66
- /** @returns {[number, string][]} */
67
- plain() {
68
- return this.firstElementChild.plain();
69
- }
70
-
71
66
  /** @param {number} n */
72
67
  setLevel(n) {
73
68
  if (typeof n !== 'number') {
package/src/html.js CHANGED
@@ -62,11 +62,6 @@ class HtmlToken extends attributeParent(fixedToken(Token)) {
62
62
  return `<${this.closing ? '/' : ''}${this.#tag}${super.text()}${this.selfClosing ? '/' : ''}>`;
63
63
  }
64
64
 
65
- /** @returns {[number, string][]} */
66
- plain() {
67
- return [];
68
- }
69
-
70
65
  /** @param {string} tag */
71
66
  replaceTag(tag) {
72
67
  const name = tag.toLowerCase();
package/src/index.js CHANGED
@@ -51,11 +51,8 @@ const {externalUse} = require('../util/debug'),
51
51
  class Token extends AstElement {
52
52
  type = 'root';
53
53
  /** 解析阶段,参见顶部注释。只对plain Token有意义。 */ #stage = 0;
54
- /** @type {ParserConfig} */ #config;
55
- /**
56
- * 这个数组起两个作用:1. 数组中的Token会在build时替换`/\x00\d+.\x7f/`标记;2. 数组中的Token会依次执行parseOnce和build方法。
57
- * @type {accum}
58
- */
54
+ #config;
55
+ /** 这个数组起两个作用:1. 数组中的Token会在build时替换`/\x00\d+.\x7f/`标记;2. 数组中的Token会依次执行parseOnce和build方法。 */
59
56
  #accum;
60
57
  /** @type {Record<string, Ranges>} */ #acceptable;
61
58
  #protectedChildren = new Ranges();
@@ -71,7 +68,9 @@ class Token extends AstElement {
71
68
  if (typeof wikitext === 'string') {
72
69
  this.appendChild(halfParsed ? wikitext : wikitext.replace(/[\x00\x7f]/g, ''));
73
70
  }
74
- this.setAttribute('config', config).setAttribute('accum', accum).setAttribute('acceptable', acceptable);
71
+ this.#config = config;
72
+ this.#accum = accum;
73
+ this.setAttribute('acceptable', acceptable);
75
74
  accum.push(this);
76
75
  }
77
76
 
@@ -12,6 +12,7 @@ class CategoryToken extends LinkToken {
12
12
  type = 'category';
13
13
  sortkey = '';
14
14
 
15
+ setLangLink = undefined;
15
16
  setFragment = undefined;
16
17
  asSelfLink = undefined;
17
18
  pipeTrick = undefined;
@@ -37,6 +38,7 @@ class CategoryToken extends LinkToken {
37
38
  }
38
39
  };
39
40
  this.addEventListener(['remove', 'insert', 'replace', 'text'], categoryListener);
41
+ return this;
40
42
  }
41
43
 
42
44
  #updateSortkey() {
@@ -73,11 +75,6 @@ class CategoryToken extends LinkToken {
73
75
  return `[[${this.firstElementChild.text()}]]`;
74
76
  }
75
77
 
76
- /** @returns {[number, string][]} */
77
- plain() {
78
- return [];
79
- }
80
-
81
78
  /** @param {string} text */
82
79
  setSortkey(text) {
83
80
  this.setLinkText(text);
package/src/link/file.js CHANGED
@@ -15,6 +15,7 @@ class FileToken extends LinkToken {
15
15
  /** @type {Set<string>} */ #keys = new Set();
16
16
  /** @type {Record<string, Set<ImageParameterToken>>} */ #args = {};
17
17
 
18
+ setLangLink = undefined;
18
19
  setFragment = undefined;
19
20
  asSelfLink = undefined;
20
21
  setLinkText = undefined;
package/src/link/index.js CHANGED
@@ -90,7 +90,8 @@ class LinkToken extends Token {
90
90
  }
91
91
 
92
92
  toString() {
93
- return `[[${super.toString('|')}]]`;
93
+ const str = super.toString('|');
94
+ return this.parentElement?.matches('ext-inner#gallery') ? str : `[[${str}]]`;
94
95
  }
95
96
 
96
97
  getPadding() {
@@ -105,11 +106,6 @@ class LinkToken extends Token {
105
106
  return `[[${super.text('|')}]]`;
106
107
  }
107
108
 
108
- /** @returns {[number, string][]} */
109
- plain() {
110
- return this.childNodes.length === 1 ? [] : this.lastElementChild.plain();
111
- }
112
-
113
109
  /** @param {string} link */
114
110
  setTarget(link) {
115
111
  link = String(link);
@@ -128,6 +124,33 @@ class LinkToken extends Token {
128
124
  this.firstElementChild.safeReplaceWith(firstChild);
129
125
  }
130
126
 
127
+ /**
128
+ * @param {string} lang
129
+ * @param {string} link
130
+ */
131
+ setLangLink(lang, link) {
132
+ if (typeof lang !== 'string') {
133
+ this.typeError('setLangLink', 'String');
134
+ }
135
+ link = String(link).trim();
136
+ if (link.startsWith('#')) {
137
+ throw new SyntaxError(`跨语言链接不能仅为fragment!`);
138
+ } else if (link.startsWith(':')) {
139
+ link = link.slice(1);
140
+ }
141
+ const root = Parser.parse(`[[${lang}:${link}]]`, this.getAttribute('include'), 6, this.getAttribute('config')),
142
+ /** @type {Token & {firstElementChild: LinkToken}} */ {childNodes: {length}, firstElementChild} = root;
143
+ if (length !== 1 || firstElementChild?.type !== 'link' || firstElementChild.childNodes.length !== 1
144
+ || firstElementChild.interwiki !== lang.toLowerCase()
145
+ ) {
146
+ throw new SyntaxError(`非法的跨语言链接目标:${lang}:${link}`);
147
+ }
148
+ const {firstChild} = firstElementChild;
149
+ root.destroy();
150
+ firstElementChild.destroy();
151
+ this.firstElementChild.safeReplaceWith(firstChild);
152
+ }
153
+
131
154
  /** @param {string} fragment */
132
155
  #setFragment(fragment, page = true) {
133
156
  fragment = String(fragment).replace(/[<>[]#|=!]/g, p => encodeURIComponent(p));
package/src/magicLink.js CHANGED
@@ -42,10 +42,14 @@ class MagicLinkToken extends Token {
42
42
  if (parameter?.getValue() === this.text()) {
43
43
  this.replaceWith(this.toString());
44
44
  }
45
+ return this;
45
46
  }
46
47
 
47
48
  getUrl() {
48
- const url = this.text();
49
+ let url = this.text();
50
+ if (url.startsWith('//')) {
51
+ url = `https:${url}`;
52
+ }
49
53
  try {
50
54
  return new URL(url);
51
55
  } catch (e) {
package/src/nowiki/dd.js CHANGED
@@ -9,10 +9,10 @@ const /** @type {Parser} */ Parser = require('../..'),
9
9
  */
10
10
  class DdToken extends NowikiToken {
11
11
  type = 'dd';
12
- dt;
13
- ul;
14
- ol;
15
- indent;
12
+ dt = false;
13
+ ul = false;
14
+ ol = false;
15
+ indent = 0;
16
16
 
17
17
  /** @param {string} str */
18
18
  #update(str) {
@@ -29,11 +29,6 @@ class DdToken extends NowikiToken {
29
29
  this.seal(['dt', 'ul', 'ol', 'indent']).#update(str);
30
30
  }
31
31
 
32
- /** @returns {[number, string][]} */
33
- plain() {
34
- return [];
35
- }
36
-
37
32
  /** @param {string} str */
38
33
  setText(str) {
39
34
  const src = this.type === 'dd' ? ':' : ';:*#';
package/src/nowiki/hr.js CHANGED
@@ -24,11 +24,6 @@ class HrToken extends sol(NowikiToken) {
24
24
  return Parser.run(() => new HrToken(this.firstChild.length, this.getAttribute('config')));
25
25
  }
26
26
 
27
- /** @returns {[number, string][]} */
28
- plain() {
29
- return [];
30
- }
31
-
32
27
  /** @param {string} str */
33
28
  setText(str) {
34
29
  if (!/^-{4,}$/.test(str)) {
@@ -16,7 +16,7 @@ class NowikiToken extends fixedToken(Token) {
16
16
  * @param {accum} accum
17
17
  */
18
18
  constructor(wikitext, config = Parser.getConfig(), accum = []) {
19
- super(wikitext, config, false, accum);
19
+ super(wikitext, config, true, accum);
20
20
  }
21
21
 
22
22
  /** @this {NowikiToken & {firstChild: string}} */
@@ -26,11 +26,6 @@ class QuoteToken extends NowikiToken {
26
26
  }
27
27
  return super.setText(str);
28
28
  }
29
-
30
- /** @returns {[number, string][]} */
31
- plain() {
32
- return [];
33
- }
34
29
  }
35
30
 
36
31
  Parser.classes.QuoteToken = __filename;
@@ -15,7 +15,7 @@ class OnlyincludeToken extends Token {
15
15
  * @param {accum} accum
16
16
  */
17
17
  constructor(inner, config = Parser.getConfig(), accum = []) {
18
- super(inner, config, false, accum);
18
+ super(inner, config, true, accum);
19
19
  }
20
20
 
21
21
  cloneNode() {
package/src/parameter.js CHANGED
@@ -85,11 +85,6 @@ class ParameterToken extends fixedToken(Token) {
85
85
  return this.anon ? this.lastElementChild.text() : super.text('=');
86
86
  }
87
87
 
88
- /** @returns {[number, string][]} */
89
- plain() {
90
- return this.lastElementChild.plain();
91
- }
92
-
93
88
  /**
94
89
  * @param {ParameterToken} token
95
90
  * @complexity `n`
package/src/syntax.js CHANGED
@@ -60,11 +60,6 @@ class SyntaxToken extends Token {
60
60
  return super.getAttribute(key);
61
61
  }
62
62
 
63
- /** @returns {[number, string][]} */
64
- plain() {
65
- return [];
66
- }
67
-
68
63
  /**
69
64
  * @param {...string|Token} elements
70
65
  * @complexity `n`
package/src/table/td.js CHANGED
@@ -152,7 +152,7 @@ class TdToken extends fixedToken(TrToken) {
152
152
  return this;
153
153
  }
154
154
 
155
- build() {
155
+ afterBuild() {
156
156
  if (this.#innerSyntax.includes('\x00')) {
157
157
  this.#innerSyntax = this.buildFromStr(this.#innerSyntax).map(String).join('');
158
158
  }
@@ -79,6 +79,10 @@ class ExtToken extends attributeParent(TagPairToken) {
79
79
  token.lastElementChild.safeReplaceWith(inner);
80
80
  return token;
81
81
  }
82
+
83
+ get innerText() {
84
+ return this.selfClosing ? '' : this.lastElementChild.text();
85
+ }
82
86
  }
83
87
 
84
88
  Parser.classes.ExtToken = __filename;
@@ -21,7 +21,7 @@ class TagPairToken extends fixedToken(Token) {
21
21
  * @param {accum} accum
22
22
  */
23
23
  constructor(name, attr, inner, closing, config = Parser.getConfig(), accum = []) {
24
- super(undefined, config);
24
+ super(undefined, config, true);
25
25
  this.setAttribute('name', name.toLowerCase()).#tags = [name, closing || name];
26
26
  this.selfClosing = closing === undefined;
27
27
  this.closed = closing !== '';
@@ -57,7 +57,7 @@ class TagPairToken extends fixedToken(Token) {
57
57
  }
58
58
  return selfClosing
59
59
  ? `<${opening}${String(firstChild)}/>`
60
- : `<${opening}${String(firstChild)}>${String(lastChild)}${closed ? `</${closing}>` : ''}`;
60
+ : `<${opening}${String(firstChild)}>${String(lastChild)}${this.closed ? `</${closing}>` : ''}`;
61
61
  }
62
62
 
63
63
  getPadding() {
@@ -76,15 +76,6 @@ class TagPairToken extends fixedToken(Token) {
76
76
  ? `<${opening}${typeof firstChild === 'string' ? firstChild : firstChild.text()}/>`
77
77
  : `<${opening}${super.text('>')}${closed ? `</${closing}>` : ''}`;
78
78
  }
79
-
80
- /** @returns {[number, string][]} */
81
- plain() {
82
- const {lastChild} = this;
83
- if (typeof lastChild === 'string') {
84
- return lastChild ? [[this.getAbsoluteIndex() + this.getRelativeIndex(1), lastChild]] : [];
85
- }
86
- return lastChild.plain();
87
- }
88
79
  }
89
80
 
90
81
  Parser.classes.TagPairToken = __filename;
package/src/transclude.js CHANGED
@@ -209,10 +209,6 @@ class TranscludeToken extends Token {
209
209
  }}}`;
210
210
  }
211
211
 
212
- plain() {
213
- return this.getAllArgs().flatMap(child => child.plain());
214
- }
215
-
216
212
  /**
217
213
  * @param {number|ParameterToken} addedToken
218
214
  * @complexity `n`
package/util/string.js CHANGED
@@ -70,6 +70,6 @@ const normalizeSpace = (token = '', separator = '') => {
70
70
  };
71
71
 
72
72
  const extUrlChar = '(?:[\\d.]+|\\[[\\da-f:.]+\\]|[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd])'
73
- + '[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]*';
73
+ + '(?:[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]|\\x00\\d+c\\x7f)*';
74
74
 
75
75
  module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, noWrap, normalizeSpace, extUrlChar};
@@ -1 +0,0 @@
1
- {|
@@ -1,11 +0,0 @@
1
- TypeError: normalizeSpace is not a function
2
- at AttributeToken.toString (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/attribute.js:249:44)
3
- at #parseAttr (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/attribute.js:58:21)
4
- at new AttributeToken (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/attribute.js:87:45)
5
- at new TrToken (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/table/tr.js:28:4)
6
- at new TableToken (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/table/index.js:66:3)
7
- at parseTable (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/parser/table.js:40:18)
8
- at #parseTable (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:553:16)
9
- at Token.parseOnce (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:431:21)
10
- at Token.parse (/Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/src/index.js:526:9)
11
- at /Users/mengxiwu/Documents/CCM-RP/node/wikiparser-node/index.js:134:11