wikiparser-node 1.2.0 → 1.3.0

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.
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- /* eslint operator-linebreak: [2, "before", {overrides: {"=": "after"}}] */
2
+ /* eslint @stylistic/operator-linebreak: [2, "before", {overrides: {"=": "after"}}] */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Layout = void 0;
5
5
  const assert = require("assert/strict");
package/dist/base.d.ts CHANGED
@@ -23,7 +23,6 @@ export interface LintError {
23
23
  readonly startCol: number;
24
24
  readonly endLine: number;
25
25
  readonly endCol: number;
26
- readonly excerpt: string;
27
26
  }
28
27
  /** 类似Node */
29
28
  export interface AstNode {
package/dist/lib/text.js CHANGED
@@ -4,7 +4,7 @@ exports.AstText = void 0;
4
4
  const constants_1 = require("../util/constants");
5
5
  const Parser = require("../index");
6
6
  const node_1 = require("./node");
7
- const errorSyntax = /https?:\/\/|\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, errorSyntaxUrl = /\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, disallowedTags = [
7
+ const errorSyntax = /https?[:/]\/+|\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, errorSyntaxUrl = /\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, disallowedTags = [
8
8
  'html',
9
9
  'base',
10
10
  'head',
@@ -94,6 +94,7 @@ class AstText extends node_1.AstNode {
94
94
  throw new Error('无法对孤立文本节点进行语法分析!');
95
95
  }
96
96
  const { type, name } = parentNode, nextType = nextSibling?.type, previousType = previousSibling?.type, errorRegex = type === 'free-ext-link' || type === 'ext-link-url' || type === 'image-parameter' && name === 'link'
97
+ || type === 'attr-value'
97
98
  ? errorSyntaxUrl
98
99
  : errorSyntax, errors = [...data.matchAll(errorRegex)], { ext, html } = this.getRootNode().getAttribute('config');
99
100
  if (errors.length > 0) {
@@ -105,7 +106,7 @@ class AstText extends node_1.AstNode {
105
106
  index += length;
106
107
  error = error.slice(length);
107
108
  }
108
- const startIndex = start + index, lines = data.slice(0, index).split('\n'), startLine = lines.length + top - 1, line = lines.at(-1), startCol = lines.length === 1 ? left + line.length : line.length, { 0: char, length } = error, endIndex = startIndex + length, end = char === '}' || char === ']' ? endIndex + 1 : startIndex + 49, rootStr = String(root), nextChar = rootStr[endIndex], previousChar = rootStr[startIndex - 1], severity = length > 1 && (char !== '<' || /[\s/>]/u.test(nextChar ?? ''))
109
+ const startIndex = start + index, lines = data.slice(0, index).split('\n'), startLine = lines.length + top - 1, line = lines.at(-1), startCol = lines.length === 1 ? left + line.length : line.length, { 0: char, length } = error, endIndex = startIndex + length, rootStr = String(root), nextChar = rootStr[endIndex], previousChar = rootStr[startIndex - 1], severity = length > 1 && (char !== '<' || /[\s/>]/u.test(nextChar ?? ''))
109
110
  || char === '{' && (nextChar === char || previousChar === '-')
110
111
  || char === '}' && (previousChar === char || nextChar === '-')
111
112
  || char === '[' && (nextChar === char || type === 'ext-link-text'
@@ -114,7 +115,7 @@ class AstText extends node_1.AstNode {
114
115
  || !data.slice(0, index).trim() && previousType === 'free-ext-link')
115
116
  ? 'error'
116
117
  : 'warning';
117
- return (char !== 'h' || index > 0) && (char !== '<' || tags.has(tag.toLowerCase())) && {
118
+ return (char !== '<' || tags.has(tag.toLowerCase())) && {
118
119
  message: Parser.msg('lonely "$1"', char === 'h' ? error : char),
119
120
  severity,
120
121
  startIndex,
@@ -123,7 +124,6 @@ class AstText extends node_1.AstNode {
123
124
  endLine: startLine,
124
125
  startCol,
125
126
  endCol: startCol + length,
126
- excerpt: rootStr.slice(Math.max(0, end - 50), end),
127
127
  };
128
128
  }).filter(Boolean);
129
129
  }
@@ -62,7 +62,7 @@ const parseTable = ({ firstChild: { data }, type, name }, config = Parser.getCon
62
62
  out += `\n${outLine}`;
63
63
  continue;
64
64
  }
65
- // eslint-disable-next-line operator-linebreak
65
+ // eslint-disable-next-line @stylistic/operator-linebreak
66
66
  const matches = /^(?:(\|\}|\0\d+!\x7F\}|\0\d+\}\x7F)|(\|-+|\0\d+!\x7F-+|\0\d+-\x7F-*)(?!-)|(!|(?:\||\0\d+!\x7F)\+?))(.*)$/u
67
67
  .exec(line);
68
68
  if (!matches) {
package/dist/src/arg.js CHANGED
@@ -79,12 +79,11 @@ class ArgToken extends index_1.Token {
79
79
  if (rest.length > 0) {
80
80
  const rect = { start, ...this.getRootNode().posFromIndex(start) };
81
81
  errors.push(...rest.map(child => {
82
- const error = (0, lint_1.generateForChild)(child, rect, 'invisible content inside triple braces'), { startIndex, startCol, excerpt, } = error;
82
+ const error = (0, lint_1.generateForChild)(child, rect, 'invisible content inside triple braces');
83
83
  return {
84
84
  ...error,
85
- startIndex: startIndex - 1,
86
- startCol: startCol - 1,
87
- excerpt: `|${excerpt}`,
85
+ startIndex: error.startIndex - 1,
86
+ startCol: error.startCol - 1,
88
87
  };
89
88
  }));
90
89
  }
@@ -271,12 +271,11 @@ class AttributeToken extends (0, fixed_1.fixed)(index_1.Token) {
271
271
  if (!balanced) {
272
272
  const root = this.getRootNode();
273
273
  rect = { start, ...root.posFromIndex(start) };
274
- const e = (0, lint_1.generateForChild)(lastChild, rect, 'unclosed quotes', 'warning'), startIndex = e.startIndex - 1;
274
+ const e = (0, lint_1.generateForChild)(lastChild, rect, 'unclosed quotes', 'warning');
275
275
  errors.push({
276
276
  ...e,
277
- startIndex,
277
+ startIndex: e.startIndex - 1,
278
278
  startCol: e.startCol - 1,
279
- excerpt: String(root).slice(startIndex, startIndex + 50),
280
279
  });
281
280
  }
282
281
  if (extAttrs[tag] && !extAttrs[tag].has(name)
@@ -151,10 +151,7 @@ class AttributesToken extends index_1.Token {
151
151
  const attr = childNodes[i];
152
152
  if (attr instanceof atom_1.AtomToken && attr.text().trim()) {
153
153
  rect ??= { start, ...this.getRootNode().posFromIndex(start) };
154
- errors.push({
155
- ...(0, lint_1.generateForChild)(attr, rect, 'containing invalid attribute'),
156
- excerpt: childNodes.slice(i).map(String).join('').slice(0, 50),
157
- });
154
+ errors.push((0, lint_1.generateForChild)(attr, rect, 'containing invalid attribute'));
158
155
  }
159
156
  else if (attr instanceof attribute_1.AttributeToken) {
160
157
  const { name } = attr;
@@ -75,8 +75,7 @@ class ConverterFlagsToken extends index_1.Token {
75
75
  const child = childNodes[i], flag = child.text().trim();
76
76
  if (flag && !variantFlags.has(flag) && !unknownFlags.has(flag)
77
77
  && (variantFlags.size > 0 || !validFlags.has(flag))) {
78
- const error = (0, lint_1.generateForChild)(child, rect, 'invalid conversion flag');
79
- errors.push({ ...error, excerpt: childNodes.slice(0, i + 1).map(String).join(';').slice(-50) });
78
+ errors.push((0, lint_1.generateForChild)(child, rect, 'invalid conversion flag'));
80
79
  }
81
80
  }
82
81
  return errors;
@@ -73,7 +73,6 @@ class GalleryToken extends index_1.Token {
73
73
  endLine: startLine,
74
74
  startCol,
75
75
  endCol: startCol + length,
76
- excerpt: String(child).slice(0, 50),
77
76
  });
78
77
  }
79
78
  else if (child.type !== 'noinclude' && child.type !== 'text') {
@@ -81,14 +81,13 @@ class HeadingToken extends (0, sol_1.sol)((0, fixed_1.fixed)(index_1.Token)) {
81
81
  /** @override */
82
82
  lint(start = this.getAbsoluteIndex()) {
83
83
  const errors = super.lint(start), { firstChild } = this, innerStr = String(firstChild);
84
- const equals = this.#equals, excerpt = `${equals}${innerStr}${equals}`.slice(0, 50);
85
84
  let refError;
86
85
  if (this.level === 1) {
87
- refError = { ...(0, lint_1.generateForChild)(firstChild, { start }, '<h1>'), excerpt };
86
+ refError = (0, lint_1.generateForChild)(firstChild, { start }, '<h1>');
88
87
  errors.push(refError);
89
88
  }
90
89
  if (innerStr.startsWith('=') || innerStr.endsWith('=')) {
91
- refError ??= { ...(0, lint_1.generateForChild)(firstChild, { start }, ''), excerpt };
90
+ refError ??= (0, lint_1.generateForChild)(firstChild, { start }, '');
92
91
  errors.push({ ...refError, message: Parser.msg('unbalanced "=" in a section header') });
93
92
  }
94
93
  if (this.closest('html-attrs, table-attrs')) {
package/dist/src/html.js CHANGED
@@ -96,20 +96,16 @@ class HtmlToken extends (0, attributesParent_1.attributesParent)((0, fixed_1.fix
96
96
  /** @override */
97
97
  lint(start = this.getAbsoluteIndex()) {
98
98
  const errors = super.lint(start);
99
- let wikitext;
100
99
  let refError;
101
100
  if (this.name === 'h1' && !this.closing) {
102
- wikitext = String(this.getRootNode());
103
101
  refError = (0, lint_1.generateForSelf)(this, { start }, '<h1>');
104
- errors.push({ ...refError, excerpt: wikitext.slice(start, start + 50) });
102
+ errors.push(refError);
105
103
  }
106
104
  if (this.closest('table-attrs')) {
107
- wikitext ??= String(this.getRootNode());
108
105
  refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
109
106
  errors.push({
110
107
  ...refError,
111
108
  message: Parser.msg('HTML tag in table attributes'),
112
- excerpt: wikitext.slice(Math.max(0, start - 25), start + 25),
113
109
  });
114
110
  }
115
111
  try {
@@ -118,17 +114,13 @@ class HtmlToken extends (0, attributesParent_1.attributesParent)((0, fixed_1.fix
118
114
  catch (e) {
119
115
  if (e instanceof SyntaxError) {
120
116
  const { message: errorMsg } = e;
121
- wikitext ??= String(this.getRootNode());
122
117
  refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
123
118
  const [msg] = errorMsg.split(':'), error = { ...refError, message: Parser.msg(msg) };
124
119
  if (msg === 'unclosed tag') {
125
120
  error.severity = 'warning';
126
- error.excerpt = wikitext.slice(start, start + 50);
127
121
  }
128
122
  else if (msg === 'unmatched closing tag') {
129
- const end = start + String(this).length;
130
123
  const ancestor = this.closest('magic-word');
131
- error.excerpt = wikitext.slice(Math.max(0, end - 50), end);
132
124
  if (ancestor && magicWords.has(ancestor.name)) {
133
125
  error.severity = 'warning';
134
126
  }
@@ -74,7 +74,6 @@ class MagicLinkToken extends (0, syntax_1.syntax)(index_1.Token) {
74
74
  endLine: startLine,
75
75
  startCol,
76
76
  endCol: startCol + s.length,
77
- excerpt: data.slice(Math.max(0, index - 25), index + 25),
78
77
  };
79
78
  }));
80
79
  }
@@ -14,10 +14,8 @@ class QuoteToken extends (0, syntax_1.syntax)(base_1.NowikiBaseToken, /^(?:'{5}|
14
14
  lint(start = this.getAbsoluteIndex()) {
15
15
  const { previousSibling, nextSibling } = this, message = Parser.msg('lonely "$1"', `'`), errors = [];
16
16
  let refError;
17
- let wikitext;
18
17
  if (previousSibling?.type === 'text' && previousSibling.data.endsWith(`'`)) {
19
18
  refError = (0, lint_1.generateForSelf)(this, { start }, message);
20
- wikitext = String(this.getRootNode());
21
19
  const { startIndex: endIndex, startLine: endLine, startCol: endCol } = refError, [{ length }] = /(?<!')'+$/u.exec(previousSibling.data), startIndex = start - length;
22
20
  errors.push({
23
21
  ...refError,
@@ -26,12 +24,10 @@ class QuoteToken extends (0, syntax_1.syntax)(base_1.NowikiBaseToken, /^(?:'{5}|
26
24
  startCol: endCol - length,
27
25
  endLine,
28
26
  endCol,
29
- excerpt: wikitext.slice(startIndex, startIndex + 50),
30
27
  });
31
28
  }
32
29
  if (nextSibling?.type === 'text' && nextSibling.data.startsWith(`'`)) {
33
30
  refError ??= (0, lint_1.generateForSelf)(this, { start }, message);
34
- wikitext ??= String(this.getRootNode());
35
31
  const { endIndex: startIndex, endLine: startLine, endCol: startCol } = refError, [{ length }] = /^'+/u.exec(nextSibling.data), endIndex = startIndex + length;
36
32
  errors.push({
37
33
  ...refError,
@@ -40,7 +36,6 @@ class QuoteToken extends (0, syntax_1.syntax)(base_1.NowikiBaseToken, /^(?:'{5}|
40
36
  startLine,
41
37
  startCol,
42
38
  endCol: startCol + length,
43
- excerpt: wikitext.slice(Math.max(0, endIndex - 50), endIndex),
44
39
  });
45
40
  }
46
41
  return errors;
@@ -107,7 +107,7 @@ class ParameterToken extends (0, fixed_1.fixed)(index_1.Token) {
107
107
  }
108
108
  /** @override */
109
109
  lint(start = this.getAbsoluteIndex()) {
110
- const errors = super.lint(start), { firstChild, lastChild, } = this, link = new RegExp(`https?://${string_1.extUrlCharFirst}${string_1.extUrlChar}$`, 'iu')
110
+ const errors = super.lint(start), { firstChild } = this, link = new RegExp(`https?://${string_1.extUrlCharFirst}${string_1.extUrlChar}$`, 'iu')
111
111
  .exec(firstChild.toString(new Set(['comment', 'noinclude', 'include'])))?.[0];
112
112
  if (link && new URL(link).search) {
113
113
  const e = (0, lint_1.generateForChild)(firstChild, { start }, 'unescaped query string in an anonymous parameter');
@@ -118,7 +118,6 @@ class ParameterToken extends (0, fixed_1.fixed)(index_1.Token) {
118
118
  startLine: e.endLine,
119
119
  startCol: e.endCol,
120
120
  endCol: e.endCol + 1,
121
- excerpt: `${String(firstChild).slice(-25)}=${String(lastChild).slice(0, 25)}`,
122
121
  });
123
122
  }
124
123
  return errors;
@@ -40,8 +40,7 @@ class TableToken extends trBase_1.TrBaseToken {
40
40
  lint(start = this.getAbsoluteIndex()) {
41
41
  const errors = super.lint(start);
42
42
  if (!this.closed) {
43
- const { firstChild, lastChild: tr, } = this, { lastChild } = tr, error = (0, lint_1.generateForChild)(firstChild, { start }, 'unclosed table');
44
- errors.push({ ...error, excerpt: String(lastChild?.type === 'td' ? lastChild : tr).slice(0, 50) });
43
+ errors.push((0, lint_1.generateForChild)(this.firstChild, { start }, 'unclosed table'));
45
44
  }
46
45
  return errors;
47
46
  }
@@ -38,7 +38,6 @@ class TrBaseToken extends base_1.TableBaseToken {
38
38
  startIndex: error.startIndex + 1,
39
39
  startLine: error.startLine + 1,
40
40
  startCol: 0,
41
- excerpt: error.excerpt.slice(1),
42
41
  });
43
42
  return errors;
44
43
  }
@@ -122,8 +122,8 @@ class ExtToken extends (0, attributesParent_1.attributesParent)(index_2.TagPairT
122
122
  lint(start = this.getAbsoluteIndex()) {
123
123
  const errors = super.lint(start);
124
124
  if (this.name !== 'nowiki' && this.closest('html-attrs, table-attrs')) {
125
- const root = this.getRootNode(), excerpt = String(root).slice(Math.max(0, start - 25), start + 25), rect = { start, ...root.posFromIndex(start) };
126
- errors.push({ ...(0, lint_1.generateForSelf)(this, rect, 'extension tag in HTML tag attributes'), excerpt });
125
+ const rect = { start, ...this.getRootNode().posFromIndex(start) };
126
+ errors.push((0, lint_1.generateForSelf)(this, rect, 'extension tag in HTML tag attributes'));
127
127
  }
128
128
  return errors;
129
129
  }
package/dist/util/lint.js CHANGED
@@ -10,17 +10,16 @@ const Parser = require("../index");
10
10
  * @param severity 严重程度
11
11
  */
12
12
  const generateForChild = (child, boundingRect, msg, severity = 'error') => {
13
- const index = child.getRelativeIndex(), { offsetHeight, offsetWidth, parentNode } = child, { top: offsetTop, left: offsetLeft } = parentNode.posFromIndex(index), { start } = boundingRect, { top, left } = 'top' in boundingRect ? boundingRect : child.getRootNode().posFromIndex(start), str = String(child), startIndex = start + index, startLine = top + offsetTop, startCol = offsetTop ? offsetLeft : left + offsetLeft;
13
+ const index = child.getRelativeIndex(), { offsetHeight, offsetWidth, parentNode } = child, { top: offsetTop, left: offsetLeft } = parentNode.posFromIndex(index), { start } = boundingRect, { top, left } = 'top' in boundingRect ? boundingRect : child.getRootNode().posFromIndex(start), startIndex = start + index, startLine = top + offsetTop, startCol = offsetTop ? offsetLeft : left + offsetLeft;
14
14
  return {
15
15
  message: Parser.msg(msg),
16
16
  severity,
17
17
  startIndex,
18
- endIndex: startIndex + str.length,
18
+ endIndex: startIndex + String(child).length,
19
19
  startLine,
20
20
  endLine: startLine + offsetHeight - 1,
21
21
  startCol,
22
22
  endCol: offsetHeight === 1 ? startCol + offsetWidth : offsetWidth,
23
- excerpt: str.slice(0, 50),
24
23
  };
25
24
  };
26
25
  exports.generateForChild = generateForChild;
@@ -32,17 +31,16 @@ exports.generateForChild = generateForChild;
32
31
  * @param severity 严重程度
33
32
  */
34
33
  const generateForSelf = (token, boundingRect, msg, severity = 'error') => {
35
- const { start } = boundingRect, { offsetHeight, offsetWidth } = token, str = String(token), { top, left } = 'top' in boundingRect ? boundingRect : token.getRootNode().posFromIndex(start);
34
+ const { start } = boundingRect, { offsetHeight, offsetWidth } = token, { top, left } = 'top' in boundingRect ? boundingRect : token.getRootNode().posFromIndex(start);
36
35
  return {
37
36
  message: Parser.msg(msg),
38
37
  severity,
39
38
  startIndex: start,
40
- endIndex: start + str.length,
39
+ endIndex: start + String(token).length,
41
40
  startLine: top,
42
41
  endLine: top + offsetHeight - 1,
43
42
  startCol: left,
44
43
  endCol: offsetHeight === 1 ? left + offsetWidth : offsetWidth,
45
- excerpt: str.slice(0, 50),
46
44
  };
47
45
  };
48
46
  exports.generateForSelf = generateForSelf;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -39,6 +39,7 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@cypress/request": "^3.0.1",
42
+ "@stylistic/eslint-plugin": "^1.5.3",
42
43
  "@types/node": "^20.9.0",
43
44
  "@types/request": "^2.48.12",
44
45
  "@typescript-eslint/eslint-plugin": "^6.12.0",