wikiparser-node 0.2.1 → 0.2.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
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,13 @@ 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
+ const [, indent, moreSpaces, tableSyntax, attr] = matchesStart;
40
+ if (indent) {
41
+ new DdToken(indent, config, accum);
42
+ }
43
+ push(`\n${spaces}${indent && `\x00${accum.length - 1}d\x7f`}${moreSpaces}\x00${accum.length}b\x7f`, top);
40
44
  const table = new TableToken(tableSyntax, attr, config, accum);
41
45
  stack.push(...top ? [top] : [], table);
42
46
  continue;
package/src/attribute.js CHANGED
@@ -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) {
@@ -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
  /**
@@ -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
  /**
package/src/extLink.js CHANGED
@@ -116,7 +116,7 @@ class ExtLinkToken extends Token {
116
116
  if (this.childNodes.length === 1) {
117
117
  this.appendChild(lastChild);
118
118
  } else {
119
- this.lastElementChild.replaceWith(lastChild);
119
+ this.lastElementChild.safeReplaceWith(lastChild);
120
120
  }
121
121
  this.#space ||= ' ';
122
122
  }
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
 
@@ -37,6 +37,7 @@ class CategoryToken extends LinkToken {
37
37
  }
38
38
  };
39
39
  this.addEventListener(['remove', 'insert', 'replace', 'text'], categoryListener);
40
+ return this;
40
41
  }
41
42
 
42
43
  #updateSortkey() {
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) {
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
  }
@@ -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() {
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
@@ -1,5 +0,0 @@
1
- {
2
- "stage": 3,
3
- "include": false,
4
- "config": "./config/default"
5
- }