wikiparser-node 0.0.0 → 0.0.1

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.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
package/parser/links.js CHANGED
@@ -35,8 +35,13 @@ const parseLinks = (firstChild, config = Parser.getConfig(), accum = []) => {
35
35
  s += `[[${x}`;
36
36
  continue;
37
37
  }
38
- const page = link.includes('%') ? decodeURIComponent(link) : link,
39
- force = link.trim().startsWith(':');
38
+ let page = link;
39
+ if (link.includes('%')) {
40
+ try {
41
+ page = decodeURIComponent(link);
42
+ } catch {}
43
+ }
44
+ const force = link.trim().startsWith(':');
40
45
  if (force && mightBeImg) {
41
46
  s += `[[${x}`;
42
47
  continue;
package/parser/table.js CHANGED
@@ -8,7 +8,8 @@ const /** @type {Parser} */ Parser = require('..');
8
8
  * @param {accum} accum
9
9
  */
10
10
  const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = []) => {
11
- const TableToken = require('../src/table'),
11
+ const Token = require('../src'),
12
+ TableToken = require('../src/table'),
12
13
  TrToken = require('../src/table/tr'),
13
14
  TdToken = require('../src/table/td'),
14
15
  /** @type {TrToken[]} */ stack = [],
@@ -19,13 +20,13 @@ const parseTable = ({firstChild, type}, config = Parser.getConfig(), accum = [])
19
20
  out += str;
20
21
  return;
21
22
  }
22
- const {lastElementChild, lastChild} = top;
23
- if (top instanceof TdToken) {
23
+ const {lastElementChild} = top;
24
+ if (lastElementChild.isPlain()) {
24
25
  lastElementChild.setText(lastElementChild.firstChild + str, 0);
25
- } else if (typeof lastChild === 'string') {
26
- top.setText(lastChild + str, 2);
27
26
  } else {
28
- top.appendChild(str);
27
+ const token = new Token(str, config, true, accum);
28
+ token.type = 'table-inter';
29
+ top.appendChild(token.setAttribute('stage', 3));
29
30
  }
30
31
  };
31
32
  for (const outLine of lines) {
package/src/extLink.js CHANGED
@@ -52,7 +52,7 @@ class ExtLinkToken extends Token {
52
52
  }
53
53
 
54
54
  toString() {
55
- return `[${super.toString(this.#space)}]`;
55
+ return `[${this.firstElementChild.toString()}${this.#space}${this.children[1]?.toString() ?? ''}]`;
56
56
  }
57
57
 
58
58
  getPadding() {
@@ -19,10 +19,9 @@ class ImageParameterToken extends Token {
19
19
  * @param {string} value
20
20
  */
21
21
  static #validate(key, value, config = Parser.getConfig()) {
22
- value = value.trim();
22
+ value = value.replace(/\x00\d+t\x7f/g, '').trim();
23
23
  if (key === 'width') {
24
- const mt = value.match(/^(\d*)(?:x(\d*))?$/);
25
- return Number(mt?.[1]) > 0 || Number(mt?.[2]) > 0;
24
+ return /^\d*(?:x\d*)?$/.test(value);
26
25
  } else if (['alt', 'class', 'manualthumb', 'frameless', 'framed', 'thumbnail'].includes(key)) {
27
26
  return true;
28
27
  } else if (key === 'link') {
@@ -68,7 +67,7 @@ class ImageParameterToken extends Token {
68
67
  super(mt[2], config, true, accum, {'Stage-2': ':', '!HeadingToken': ':'});
69
68
  this.#syntax = `${mt[1]}${param[0]}${mt[3]}`;
70
69
  }
71
- this.setAttribute('name', param[1]).setAttribute('stage', 7);
70
+ this.setAttribute('name', param[1]).setAttribute('stage', Parser.MAX_STAGE);
72
71
  return;
73
72
  }
74
73
  }
@@ -50,7 +50,7 @@ class Layout extends Array {
50
50
 
51
51
  /**
52
52
  * 表格
53
- * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?(string|Token), ...TdToken, ...TrToken, ?SyntaxToken]}`
53
+ * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?Token, ...TdToken, ...TrToken, ?SyntaxToken]}`
54
54
  */
55
55
  class TableToken extends TrToken {
56
56
  type = 'table';
@@ -65,7 +65,7 @@ class TableToken extends TrToken {
65
65
  constructor(syntax, attr = '', config = Parser.getConfig(), accum = []) {
66
66
  super(syntax, attr, config, accum, TableToken.openingPattern);
67
67
  this.setAttribute('acceptable', {
68
- String: 2, Token: 2, SyntaxToken: [0, -1], AttributeToken: 1, TdToken: '2:', TrToken: '2:',
68
+ Token: 2, SyntaxToken: [0, -1], AttributeToken: 1, TdToken: '2:', TrToken: '2:',
69
69
  });
70
70
  }
71
71
 
package/src/table/tr.js CHANGED
@@ -9,7 +9,7 @@ const attributeParent = require('../../mixin/attributeParent'),
9
9
 
10
10
  /**
11
11
  * 表格行,含开头的换行,不含结尾的换行
12
- * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?(string|Token), ...TdToken]}`
12
+ * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?Token, ...TdToken]}`
13
13
  */
14
14
  class TrToken extends attributeParent(Token, 1) {
15
15
  type = 'tr';
@@ -21,7 +21,7 @@ class TrToken extends attributeParent(Token, 1) {
21
21
  * @param {accum} accum
22
22
  */
23
23
  constructor(syntax, attr = '', config = Parser.getConfig(), accum = [], pattern = TrToken.openingPattern) {
24
- super(undefined, config, true, accum, {String: 2, Token: 2, SyntaxToken: 0, AttributeToken: 1, TdToken: '2:'});
24
+ super(undefined, config, true, accum, {Token: 2, SyntaxToken: 0, AttributeToken: 1, TdToken: '2:'});
25
25
  this.append(
26
26
  new SyntaxToken(syntax, pattern, 'table-syntax', config, accum, {
27
27
  'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':',