wikiparser-node 1.35.1 → 1.36.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.
package/dist/index.js CHANGED
@@ -24,10 +24,17 @@ const re = new RegExp(String.raw `^https?:\/\/([^./]+)\.(${common_1.wmf})\.org`,
24
24
  * 从根路径require
25
25
  * @param file 文件名
26
26
  * @param dir 子路径
27
+ * @throws {RangeError} 仅支持JSON文件
27
28
  */
28
- const rootRequire = (file, dir) => require(path_1.default.isAbsolute(file)
29
- ? /* c8 ignore next */ file
30
- : path_1.default.join('..', file.includes('/') ? '' : dir, file));
29
+ const rootRequire = (file, dir) => {
30
+ const fullPath = require.resolve(path_1.default.isAbsolute(file)
31
+ ? /* c8 ignore next */ file
32
+ : path_1.default.join('..', file.includes('/') ? '' : dir, file));
33
+ if (!fullPath.endsWith('.json')) {
34
+ throw new RangeError('Only JSON files are supported!');
35
+ }
36
+ return require(fullPath);
37
+ };
31
38
  /* NOT FOR BROWSER ONLY END */
32
39
  let viewOnly = false;
33
40
  /* NOT FOR BROWSER */
@@ -333,7 +340,6 @@ const Parser = {
333
340
  },
334
341
  /** @implements */
335
342
  callParserFunction(name, arg, ...args) {
336
- const { expandMagicWord } = require('./render/magicWords');
337
343
  if (typeof arg === 'string') {
338
344
  args.unshift(arg);
339
345
  }
@@ -349,9 +355,9 @@ const Parser = {
349
355
  args.push(`${key}=${value}`);
350
356
  }
351
357
  }
352
- const { parserFunction } = this.getConfig(), [lcName, canonicalName] = (0, debug_1.getCanonicalName)(name, parserFunction);
358
+ const { parserFunction } = this.getConfig(), [lcName, canonicalName] = (0, debug_1.getCanonicalName)(name, parserFunction), custom = constants_1.functionHooks.has(lcName);
353
359
  let result;
354
- if (constants_1.functionHooks.has(lcName)) {
360
+ if (custom) {
355
361
  if (!canonicalName) {
356
362
  const [insensitive, sensitive] = parserFunction, entry = Object.entries(sensitive).find(([, v]) => v === lcName)
357
363
  || Object.entries(insensitive).find(([, v]) => v === lcName);
@@ -366,11 +372,12 @@ const Parser = {
366
372
  && constants_1.functionHooks.get(lcName)(firstChild);
367
373
  }
368
374
  else {
375
+ const { expandMagicWord } = require('./render/magicWords');
369
376
  result = expandMagicWord(lcName, args);
370
377
  }
371
378
  /* c8 ignore next 3 */
372
379
  if (result === false) {
373
- throw new RangeError(`Unable to resolve parser function: ${name}`);
380
+ throw new RangeError(`Unable to resolve ${custom ? 'custom' : 'built-in'} parser function: ${name}`);
374
381
  }
375
382
  return result;
376
383
  },
@@ -438,8 +445,15 @@ const Parser = {
438
445
  if (!main) {
439
446
  throw new RangeError(`找不到对应时间戳的错误记录:${date}`);
440
447
  }
441
- const file = path_1.default.join(dir, main), wikitext = fs_1.default.readFileSync(file, 'utf8');
442
- const { stage, include, config, page } = require(`${file}.json`), { Token } = require('./src/index');
448
+ const { Token } = require('./src/index');
449
+ const file = path_1.default.join(dir, main), wikitext = fs_1.default.readFileSync(file, 'utf8'), { stage = constants_1.MAX_STAGE, include, config, page } = (() => {
450
+ try {
451
+ return require(`${file}.json`);
452
+ }
453
+ catch {
454
+ return {};
455
+ }
456
+ })();
443
457
  debug_1.Shadow.run(() => {
444
458
  const halfParsed = stage < constants_1.MAX_STAGE, token = new Token(halfParsed ? wikitext : (0, string_1.tidy)(wikitext), config);
445
459
  token.type = 'root';
@@ -452,8 +466,8 @@ const Parser = {
452
466
  token.parse(undefined, include);
453
467
  }
454
468
  fs_1.default.unlinkSync(file);
455
- fs_1.default.unlinkSync(`${file}.err`);
456
- fs_1.default.unlinkSync(`${file}.json`);
469
+ fs_1.default.rmSync(`${file}.err`, { force: true });
470
+ fs_1.default.rmSync(`${file}.json`, { force: true });
457
471
  }, this);
458
472
  },
459
473
  /* c8 ignore stop */
@@ -239,14 +239,12 @@ const defaultLintRuleConfig = {
239
239
  ],
240
240
  'invalid-math': 2,
241
241
  };
242
- Object.freeze(defaultLintRuleConfig);
243
242
  const defaultLintConfig = {
244
243
  configurationComment: 'lint',
245
244
  ignoreDisables: false,
246
245
  fix: true,
247
246
  computeEditInfo: true,
248
247
  };
249
- Object.freeze(defaultLintConfig);
250
248
  /**
251
249
  * 验证错误级别是否符合规范
252
250
  * @param severity 错误级别
@@ -1,4 +1,4 @@
1
- import type { LintError, AstNode as AstNodeBase, TokenTypes } from '../base';
1
+ import type { AstNode as AstNodeBase, TokenTypes, LintError } from '../base';
2
2
  import type { NodeLike } from '../mixin/nodeLike';
3
3
  import type { AstText, Token } from '../internal';
4
4
  export type AstNodes = AstText | Token;
package/dist/lib/text.js CHANGED
@@ -54,8 +54,8 @@ const cached_1 = require("../mixin/cached");
54
54
  /* NOT FOR BROWSER END */
55
55
  const sp = /* #__PURE__ */ (() => String.raw `[${string_1.zs}\t]*`)(), source = /* #__PURE__ */ (() => String.raw `<(?:/[^\S\n]*)?([a-z]\w*)|\{+|\}+|\[{2,}|\[(?![^[]*?\])|((?:^|\])[^[]*?)\]+|\n={2,}`)();
56
56
  // eslint-disable-next-line @typescript-eslint/no-unused-expressions
57
- /https?[:/]\/+|(?:rfc|pmid)(?=[-::]?\s*\d)|isbn(?=[-::]?\s*(?:\d(?:\s*|-)){6})/giu;
58
- const errorSyntax = /* #__PURE__ */ (() => new RegExp(String.raw `${source}|https?[:/]/+|(?:rfc|pmid)(?=[-::]?${sp}\d)|isbn(?=[-::]?${sp}(?:\d(?:${sp}|-)){6})`, 'giu'))();
57
+ /\bhttps?[:/]\/+|\b(?:rfc|pmid)(?=[-::]?\s*\d)|\bisbn(?=[-::]?\s*(?:\d(?:\s*|-)){6})/giu;
58
+ const errorSyntax = /* #__PURE__ */ (() => new RegExp(String.raw `${source}|\bhttps?[:/]/+|\b(?:rfc|pmid)(?=[-::]?${sp}\d)|\bisbn(?=[-::]?${sp}(?:\d(?:${sp}|-)){6})`, 'giu'))();
59
59
  // eslint-disable-next-line @typescript-eslint/no-unused-expressions
60
60
  /<(?:\/[^\S\n]*)?([a-z]\w*)|\{+|\}+|\[{2,}|\[(?![^[]*?\])|((?:^|\])[^[]*?)\]+|\n={2,}/giu;
61
61
  const errorSyntaxUrl = /* #__PURE__ */ new RegExp(source, 'giu'), noLinkTypes = new Set(['attr-value', 'ext-link-text', 'link-text']), regexes = {
package/dist/lib/title.js CHANGED
@@ -300,10 +300,10 @@ class Title {
300
300
  if (!valid || ns !== 6 || interwiki) {
301
301
  throw new Error('Title.getFileUrl method is only for files!');
302
302
  }
303
- const { expandMagicWord } = require('../render/magicWords');
304
303
  if (height) {
305
304
  width ||= 1e4;
306
305
  }
306
+ const { expandMagicWord } = require('../render/magicWords');
307
307
  return expandMagicWord('filepath', [main, `${width || ''}${height ? `x${height}` : ''}`]);
308
308
  }
309
309
  /** @private */
@@ -1,7 +1,7 @@
1
1
  import Parser from '../index';
2
2
  import { AstElement } from '../lib/element';
3
3
  import { AstText } from '../lib/text';
4
- import type { LintError, TokenTypes } from '../base';
4
+ import type { TokenTypes, LintError } from '../base';
5
5
  import type { Title, TitleOptions } from '../lib/title';
6
6
  import type { AstNodes, IncludeToken, HtmlToken, ExtToken, CommentToken } from '../internal';
7
7
  import { Ranges } from '../lib/ranges';
@@ -1,5 +1,5 @@
1
1
  import { NowikiBaseToken } from './base';
2
- import type { LintError, Config } from '../../base';
2
+ import type { Config, LintError } from '../../base';
3
3
  import type { Token } from '../../internal';
4
4
  /**
5
5
  * invisible HTML comment
@@ -1,6 +1,6 @@
1
1
  import { Token } from './index';
2
2
  import type { Config, LintError, AST } from '../base';
3
- import type { AtomToken, SyntaxToken, TranscludeToken } from '../internal';
3
+ import type { AtomToken, TranscludeToken, SyntaxToken } from '../internal';
4
4
  /**
5
5
  * template or magic word parameter
6
6
  *
@@ -1,5 +1,5 @@
1
1
  import { TagPairToken } from './index';
2
- import type { LintError, Config } from '../../base';
2
+ import type { Config, LintError } from '../../base';
3
3
  import type { AstText, Token } from '../../internal';
4
4
  /**
5
5
  * `<includeonly>`, `<noinclude>` or `<onlyinclude>`
@@ -42,6 +42,13 @@ export declare abstract class TranscludeToken extends Token {
42
42
  /** whether to contain duplicated parameters / 是否存在重复参数 */
43
43
  get duplication(): boolean;
44
44
  set duplication(duplication: boolean);
45
+ /**
46
+ * number of anonymous parameters
47
+ *
48
+ * 匿名参数的个数
49
+ * @since v1.36.0
50
+ */
51
+ get anonCount(): number;
45
52
  /**
46
53
  * @param title 模板标题或魔术字
47
54
  * @param parts 参数各部分
@@ -67,18 +74,6 @@ export declare abstract class TranscludeToken extends Token {
67
74
  * @param i position to be inserted at / 插入位置
68
75
  */
69
76
  insertAt<T extends ParameterToken>(token: T, i?: number): T;
70
- /**
71
- * Get all parameters
72
- *
73
- * 获取所有参数
74
- */
75
- getAllArgs(): ParameterToken[];
76
- /**
77
- * Get all anonymous parameters
78
- *
79
- * 获取所有匿名参数
80
- */
81
- getAnonArgs(): ParameterToken[];
82
77
  /**
83
78
  * Get parameters with the specified name
84
79
  *
@@ -86,8 +81,9 @@ export declare abstract class TranscludeToken extends Token {
86
81
  * @param key parameter name / 参数名
87
82
  * @param exact whether to match anonymosity / 是否匹配匿名性
88
83
  * @param copy whether to return a copy / 是否返回一个备份
84
+ * @param init whether to initialize during parsing / 是否在解析时进行初始化
89
85
  */
90
- getArgs(key: string | number, exact?: boolean, copy?: boolean): Set<ParameterToken>;
86
+ getArgs(key: string | number, exact?: boolean, copy?: boolean, init?: boolean): Set<ParameterToken>;
91
87
  /**
92
88
  * Get duplicated parameters
93
89
  *
@@ -120,6 +116,18 @@ export declare abstract class TranscludeToken extends Token {
120
116
  * @param i position of the child node / 移除位置
121
117
  */
122
118
  removeAt(i: number): ParameterToken;
119
+ /**
120
+ * Get all parameters
121
+ *
122
+ * 获取所有参数
123
+ */
124
+ getAllArgs(): ParameterToken[];
125
+ /**
126
+ * Get all anonymous parameters
127
+ *
128
+ * 获取所有匿名参数
129
+ */
130
+ getAnonArgs(): ParameterToken[];
123
131
  /**
124
132
  * Check if there is a parameter with the specified name
125
133
  *
@@ -83,6 +83,7 @@ let TranscludeToken = (() => {
83
83
  #colon = ':';
84
84
  #raw = false;
85
85
  #args = new Map();
86
+ #anonCount = 0;
86
87
  #title;
87
88
  /* NOT FOR BROWSER */
88
89
  #keys = new Set();
@@ -121,6 +122,15 @@ let TranscludeToken = (() => {
121
122
  this.fixDuplication();
122
123
  }
123
124
  }
125
+ /**
126
+ * number of anonymous parameters
127
+ *
128
+ * 匿名参数的个数
129
+ * @since v1.36.0
130
+ */
131
+ get anonCount() {
132
+ return this.#anonCount;
133
+ }
124
134
  /* NOT FOR BROWSER END */
125
135
  /**
126
136
  * @param title 模板标题或魔术字
@@ -392,23 +402,44 @@ let TranscludeToken = (() => {
392
402
  return errors;
393
403
  }
394
404
  }
395
- /**
396
- * 处理匿名参数更改
397
- * @param addedToken 新增的参数
398
- */
399
- #handleAnonArgChange(addedToken) {
400
- const args = this.getAnonArgs(), added = typeof addedToken !== 'number';
405
+ #handleAnonArgChange(addedToken, append) {
401
406
  /* NOT FOR BROWSER */
402
- const maxAnon = String(args.length + (added ? 0 : 1));
403
- if (added) {
407
+ const removing = typeof addedToken === 'number';
408
+ /* NOT FOR BROWSER END */
409
+ /* eslint-disable @stylistic/operator-linebreak */
410
+ this.#anonCount +=
411
+ removing ?
412
+ -1 :
413
+ 1;
414
+ /* eslint-enable @stylistic/operator-linebreak */
415
+ /* NOT FOR BROWSER */
416
+ const maxAnon = String(this.#anonCount + (removing ? 1 : 0));
417
+ if (!removing) {
404
418
  this.#keys.add(maxAnon);
405
419
  }
406
420
  else if (!this.hasArg(maxAnon, true)) {
407
421
  this.#keys.delete(maxAnon);
408
422
  }
423
+ let args;
409
424
  /* NOT FOR BROWSER END */
410
- for (let i = added ? args.indexOf(addedToken) : addedToken - 1; i < args.length; i++) {
411
- const token = args[i], { name } = token, newName = String(i + 1);
425
+ let i;
426
+ if (append) {
427
+ i = this.#anonCount - 1;
428
+ /* NOT FOR BROWSER */
429
+ }
430
+ else {
431
+ args = this.getAnonArgs();
432
+ i = removing ? addedToken - 1 : args.indexOf(addedToken);
433
+ }
434
+ for (; i < this.#anonCount; i++) {
435
+ /* NOT FOR BROWSER END */
436
+ const token =
437
+ /* eslint-disable @stylistic/operator-linebreak, unicorn/no-negated-condition */
438
+ !append ?
439
+ args[i] :
440
+ addedToken,
441
+ /* eslint-enable @stylistic/operator-linebreak, unicorn/no-negated-condition */
442
+ { name } = token, newName = String(i + 1);
412
443
  if (name !== newName || token === addedToken) {
413
444
  token.setAttribute('name', newName);
414
445
  this.getArgs(newName, false, false).add(token);
@@ -427,7 +458,7 @@ let TranscludeToken = (() => {
427
458
  insertAt(token, i = this.length) {
428
459
  super.insertAt(token, i);
429
460
  if (token.anon) {
430
- this.#handleAnonArgChange(token);
461
+ this.#handleAnonArgChange(token, i === this.length - 1);
431
462
  }
432
463
  else if (token.name) {
433
464
  this.getArgs(token.name, false, false).add(token);
@@ -436,22 +467,6 @@ let TranscludeToken = (() => {
436
467
  }
437
468
  return token;
438
469
  }
439
- /**
440
- * Get all parameters
441
- *
442
- * 获取所有参数
443
- */
444
- getAllArgs() {
445
- return this.childNodes.filter((0, debug_1.isToken)('parameter'));
446
- }
447
- /**
448
- * Get all anonymous parameters
449
- *
450
- * 获取所有匿名参数
451
- */
452
- getAnonArgs() {
453
- return this.getAllArgs().filter(({ anon }) => anon);
454
- }
455
470
  /**
456
471
  * Get parameters with the specified name
457
472
  *
@@ -459,8 +474,9 @@ let TranscludeToken = (() => {
459
474
  * @param key parameter name / 参数名
460
475
  * @param exact whether to match anonymosity / 是否匹配匿名性
461
476
  * @param copy whether to return a copy / 是否返回一个备份
477
+ * @param init whether to initialize during parsing / 是否在解析时进行初始化
462
478
  */
463
- getArgs(key, exact, copy = true) {
479
+ getArgs(key, exact, copy = true, init = true) {
464
480
  const keyStr = String(key)
465
481
  .replace(/^[ \t\n\0\v]+|([^ \t\n\0\v])[ \t\n\0\v]+$/gu, '$1');
466
482
  let args;
@@ -468,7 +484,7 @@ let TranscludeToken = (() => {
468
484
  args = this.#args.get(keyStr);
469
485
  }
470
486
  else {
471
- args = new Set(this.getAllArgs().filter(({ name }) => keyStr === name));
487
+ args = new Set(init ? [] : this.getAllArgs().filter(({ name }) => keyStr === name));
472
488
  this.#args.set(keyStr, args);
473
489
  }
474
490
  /* NOT FOR BROWSER */
@@ -618,6 +634,23 @@ let TranscludeToken = (() => {
618
634
  }
619
635
  return token;
620
636
  }
637
+ /**
638
+ * Get all parameters
639
+ *
640
+ * 获取所有参数
641
+ */
642
+ getAllArgs() {
643
+ const { childNodes } = this, i = childNodes.findIndex(({ type }) => type === 'parameter');
644
+ return i === -1 ? [] : childNodes.slice(i);
645
+ }
646
+ /**
647
+ * Get all anonymous parameters
648
+ *
649
+ * 获取所有匿名参数
650
+ */
651
+ getAnonArgs() {
652
+ return this.getAllArgs().filter(({ anon }) => anon);
653
+ }
621
654
  /**
622
655
  * Check if there is a parameter with the specified name
623
656
  *
@@ -626,7 +659,7 @@ let TranscludeToken = (() => {
626
659
  * @param exact whether to match anonymosity / 是否匹配匿名性
627
660
  */
628
661
  hasArg(key, exact) {
629
- return this.getArgs(key, exact, false).size > 0;
662
+ return this.getArgs(key, exact, false, false).size > 0;
630
663
  }
631
664
  /**
632
665
  * Get the effective parameter with the specified name
@@ -1,6 +1,6 @@
1
1
  (() => {
2
2
  var _a;
3
- const version = '1.35.1', src = (_a = document.currentScript) === null || _a === void 0 ? void 0 : _a.src, file = /\/extensions\/dist\/base\.(?:min\.)?js$/u, CDN = src && file.test(src)
3
+ const version = '1.36.0', src = (_a = document.currentScript) === null || _a === void 0 ? void 0 : _a.src, file = /\/extensions\/dist\/base\.(?:min\.)?js$/u, CDN = src && file.test(src)
4
4
  ? src.replace(file, '')
5
5
  : `https://fastly.jsdelivr.net/npm/wikiparser-node@${version}`;
6
6
  const workerJS = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "1.35.1",
3
+ "version": "1.36.0",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -62,6 +62,7 @@
62
62
  "test:unit": "mocha dist/test/test.js",
63
63
  "test:clonenode": "CLONENODE=1 npm run test:unit",
64
64
  "test:parser": "LC_ALL=en.UTF-8 mocha dist/test/parserTests.js",
65
+ "test:perf": "mocha --reporter spec dist/test/perf.js",
65
66
  "test": "npm run test:unit && npm run test:clonenode && npm run test:parser",
66
67
  "test:parseronly": "LSP=0 npm run test:parser",
67
68
  "test:ci": "LSP=0 npm test",
@@ -93,43 +94,43 @@
93
94
  "mathoid-texvcjs": "^0.6.0",
94
95
  "prism-wiki": "^1.3.0",
95
96
  "prismjs": "^1.30.0",
96
- "stylelint": "^17.1.1",
97
- "vscode-css-languageservice": "^6.3.9",
98
- "vscode-html-languageservice": "^5.6.1",
99
- "vscode-json-languageservice": "^5.7.1"
97
+ "stylelint": "^17.4.0",
98
+ "vscode-css-languageservice": "^6.3.10",
99
+ "vscode-html-languageservice": "^5.6.2",
100
+ "vscode-json-languageservice": "^5.7.2"
100
101
  },
101
102
  "devDependencies": {
102
- "@bhsd/code-standard": "^2.0.0",
103
- "@bhsd/nodejs": "^0.1.0",
103
+ "@bhsd/code-standard": "^2.1.0",
104
+ "@bhsd/nodejs": "^0.1.1",
104
105
  "@bhsd/test-util": "^0.3.0",
105
- "@codemirror/lint": "^6.9.3",
106
+ "@codemirror/lint": "^6.9.4",
106
107
  "@eslint/markdown": "^7.5.1",
107
- "@stylistic/eslint-plugin": "^5.7.1",
108
+ "@stylistic/eslint-plugin": "^5.9.0",
108
109
  "@types/color-name": "^2.0.0",
109
110
  "@types/color-rgba": "^2.1.3",
110
111
  "@types/mocha": "^10.0.10",
111
- "@types/node": "^24.10.11",
112
- "@types/prismjs": "^1.26.5",
112
+ "@types/node": "^24.11.0",
113
+ "@types/prismjs": "^1.26.6",
113
114
  "@typescript-eslint/eslint-plugin": "^8.54.0",
114
115
  "@typescript-eslint/parser": "^8.54.0",
115
- "c8": "^10.1.3",
116
+ "c8": "^11.0.0",
116
117
  "codejar-async": "^4.3.0",
117
118
  "color-rgba": "^3.0.0",
118
119
  "diff2html-cli": "^5.2.15",
119
120
  "esbuild": "^0.27.3",
120
- "eslint": "^9.39.2",
121
+ "eslint": "^9.39.3",
121
122
  "eslint-plugin-eslint-comments": "^3.2.0",
122
- "eslint-plugin-jsdoc": "^62.5.3",
123
- "eslint-plugin-jsonc": "^2.21.0",
124
- "eslint-plugin-n": "^17.23.2",
123
+ "eslint-plugin-jsdoc": "^62.7.1",
124
+ "eslint-plugin-jsonc": "^3.1.1",
125
+ "eslint-plugin-n": "^17.24.0",
125
126
  "eslint-plugin-promise": "^7.2.1",
126
127
  "eslint-plugin-regexp": "^3.0.0",
127
- "eslint-plugin-unicorn": "^62.0.0",
128
- "markdownlint-cli2": "^0.20.0",
128
+ "eslint-plugin-unicorn": "^63.0.0",
129
+ "markdownlint-cli2": "^0.21.0",
129
130
  "mocha": "^11.7.5",
130
131
  "monaco-editor": "^0.55.1",
131
132
  "typescript": "^5.9.3",
132
- "v8r": "^5.1.0",
133
+ "v8r": "^6.0.0",
133
134
  "vscode-languageserver-textdocument": "^1.0.12"
134
135
  },
135
136
  "engines": {