wikiparser-node 1.18.1 → 1.18.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 (86) hide show
  1. package/README.md +7 -2
  2. package/bin/config.js +2 -2
  3. package/bundle/bundle-es7.min.js +29 -29
  4. package/bundle/bundle-lsp.min.js +95 -29
  5. package/bundle/bundle.min.js +26 -26
  6. package/config/.schema.json +13 -0
  7. package/config/default.json +460 -4
  8. package/config/enwiki.json +111 -0
  9. package/config/jawiki.json +954 -0
  10. package/config/llwiki.json +117 -0
  11. package/config/minimum.json +10 -0
  12. package/config/moegirl.json +104 -0
  13. package/config/zhwiki.json +109 -0
  14. package/dist/addon/token.js +10 -9
  15. package/dist/addon/transclude.js +1 -3
  16. package/dist/base.d.mts +5 -0
  17. package/dist/base.d.ts +5 -0
  18. package/dist/base.js +1 -0
  19. package/dist/base.mjs +2 -1
  20. package/dist/bin/config.js +82 -59
  21. package/dist/index.d.ts +5 -1
  22. package/dist/index.js +68 -73
  23. package/dist/lib/document.d.ts +6 -6
  24. package/dist/lib/document.js +43 -36
  25. package/dist/lib/element.d.ts +6 -0
  26. package/dist/lib/element.js +543 -466
  27. package/dist/lib/lsp.d.ts +1 -0
  28. package/dist/lib/lsp.js +84 -56
  29. package/dist/lib/redirectMap.d.ts +7 -0
  30. package/dist/lib/redirectMap.js +31 -0
  31. package/dist/lib/text.d.ts +2 -2
  32. package/dist/lib/text.js +383 -325
  33. package/dist/lib/title.d.ts +23 -4
  34. package/dist/lib/title.js +17 -5
  35. package/dist/mixin/noEscape.d.ts +4 -0
  36. package/dist/mixin/noEscape.js +22 -0
  37. package/dist/mixin/readOnly.d.ts +4 -0
  38. package/dist/mixin/readOnly.js +26 -0
  39. package/dist/parser/braces.js +29 -15
  40. package/dist/parser/commentAndExt.js +5 -16
  41. package/dist/parser/links.js +1 -1
  42. package/dist/parser/quotes.js +1 -1
  43. package/dist/parser/redirect.js +1 -3
  44. package/dist/src/arg.js +253 -202
  45. package/dist/src/attribute.d.ts +0 -5
  46. package/dist/src/attribute.js +3 -7
  47. package/dist/src/converter.js +213 -162
  48. package/dist/src/gallery.js +1 -2
  49. package/dist/src/heading.js +5 -6
  50. package/dist/src/imageParameter.js +16 -16
  51. package/dist/src/imagemap.js +3 -2
  52. package/dist/src/index.d.ts +1 -1
  53. package/dist/src/index.js +722 -694
  54. package/dist/src/link/base.js +292 -241
  55. package/dist/src/link/file.js +13 -17
  56. package/dist/src/link/galleryImage.js +1 -1
  57. package/dist/src/link/redirectTarget.js +1 -2
  58. package/dist/src/magicLink.d.ts +0 -6
  59. package/dist/src/magicLink.js +5 -15
  60. package/dist/src/nested.js +7 -7
  61. package/dist/src/nowiki/base.js +2 -1
  62. package/dist/src/nowiki/index.js +4 -3
  63. package/dist/src/onlyinclude.js +95 -44
  64. package/dist/src/parameter.d.ts +0 -6
  65. package/dist/src/parameter.js +1 -13
  66. package/dist/src/redirect.js +6 -6
  67. package/dist/src/syntax.d.ts +4 -1
  68. package/dist/src/syntax.js +4 -1
  69. package/dist/src/table/base.d.ts +0 -5
  70. package/dist/src/table/base.js +2 -9
  71. package/dist/src/table/index.js +6 -3
  72. package/dist/src/table/td.d.ts +1 -0
  73. package/dist/src/table/td.js +2 -3
  74. package/dist/src/table/trBase.js +3 -1
  75. package/dist/src/tagPair/index.js +2 -1
  76. package/dist/src/transclude.js +706 -647
  77. package/dist/util/debug.js +10 -3
  78. package/dist/util/lint.js +31 -30
  79. package/dist/util/string.js +4 -5
  80. package/extensions/dist/base.js +8 -6
  81. package/extensions/dist/lint.js +1 -1
  82. package/extensions/dist/lsp.js +0 -5
  83. package/extensions/es7/base.js +8 -6
  84. package/extensions/es7/lint.js +1 -1
  85. package/extensions/ui.css +1 -1
  86. package/package.json +3 -4
package/dist/lib/lsp.d.ts CHANGED
@@ -20,6 +20,7 @@ export declare const isAttr: ({ type, parentNode, length, firstChild }: Token, s
20
20
  /** VSCode-style language service */
21
21
  export declare class LanguageService implements LanguageServiceBase {
22
22
  #private;
23
+ /** @since v1.17.1 */
23
24
  include: boolean;
24
25
  lilypond: string;
25
26
  /** @param uri 任务标识 */
package/dist/lib/lsp.js CHANGED
@@ -26,12 +26,12 @@ const cssRules = {
26
26
  'block-no-empty': null,
27
27
  'property-no-unknown': null,
28
28
  }, jsonSelector = document_1.jsonTags.map(s => `ext-inner#${s}`).join(), scores = new Map();
29
+ let colors;
29
30
  /* NOT FOR BROWSER ONLY END */
30
31
  exports.tasks = new WeakMap();
31
32
  const refTags = new Set(['ref']), referencesTags = new Set(['ref', 'references']), nameAttrs = new Set(['name', 'extends', 'follow']), groupAttrs = new Set(['group']), renameTypes = new Set([
32
33
  'arg-name',
33
34
  'template-name',
34
- 'magic-word-name',
35
35
  'link-target',
36
36
  'parameter-key',
37
37
  ]), referenceTypes = new Set([
@@ -41,8 +41,11 @@ const refTags = new Set(['ref']), referencesTags = new Set(['ref', 'references']
41
41
  'image-parameter',
42
42
  'heading-title',
43
43
  'heading',
44
+ 'magic-word-name',
44
45
  ...renameTypes,
45
46
  ]), plainTypes = new Set(['text', 'comment', 'noinclude', 'include']), cssSelector = ['ext', 'html', 'table'].map(s => `${s}-attr#style`).join();
47
+ /^(?:http:\/\/|\/\/)/iu; // eslint-disable-line @typescript-eslint/no-unused-expressions
48
+ const getLinkRegex = (0, common_1.getRegex)(protocol => new RegExp(`^(?:${protocol}|//)`, 'iu'));
46
49
  /**
47
50
  * Check if a token is a plain attribute.
48
51
  * @param token
@@ -276,6 +279,7 @@ class LanguageService {
276
279
  #config;
277
280
  #include;
278
281
  #completionConfig;
282
+ /** @since v1.17.1 */
279
283
  include = true;
280
284
  /** @private */
281
285
  config;
@@ -291,11 +295,11 @@ class LanguageService {
291
295
  exports.tasks.set(uri, this);
292
296
  Object.defineProperties(this, {
293
297
  config: { enumerable: false },
294
- /* NOT FOR BROWSER ONLY */
295
298
  data: {
296
299
  value: require(path_1.default.join('..', '..', 'data', 'signatures')),
297
300
  enumerable: false,
298
301
  },
302
+ /* NOT FOR BROWSER ONLY */
299
303
  lilypondData: {
300
304
  value: require(path_1.default.join('..', '..', 'data', 'ext', 'score')),
301
305
  enumerable: false,
@@ -409,11 +413,14 @@ class LanguageService {
409
413
  async provideDocumentColors(rgba, text, hsl = true) {
410
414
  const root = await this.#queue(text);
411
415
  /* NOT FOR BROWSER ONLY */
412
- let colors;
416
+ /* eslint-disable require-atomic-updates */
413
417
  try {
414
- colors = new RegExp(String.raw `\b${Object.keys((await import('color-name')).default).join('|')}\b`, 'giu');
418
+ colors ??= new RegExp(String.raw `\b${Object.keys((await import('color-name')).default).join('|')}\b`, 'giu');
419
+ }
420
+ catch {
421
+ colors = false;
415
422
  }
416
- catch { }
423
+ /* eslint-enable require-atomic-updates */
417
424
  /* NOT FOR BROWSER ONLY END */
418
425
  return root.querySelectorAll('attr-value,parameter-value,arg-default').reverse()
419
426
  .flatMap(token => {
@@ -558,38 +565,46 @@ class LanguageService {
558
565
  return getCompletion(root.querySelectorAll('arg').filter(token => token.name && token !== cur)
559
566
  .map(({ name }) => name), 'Variable', match, position);
560
567
  }
561
- const [insensitive, sensitive] = this.config.parserFunction, isOld = Array.isArray(sensitive), colon = match.startsWith(':'), str = colon ? match.slice(1).trimStart() : match;
562
- return mt[2] === '[['
563
- ? getCompletion(// link
564
- root.querySelectorAll('link,file,category,redirect-target').filter(token => token !== cur).map(({ name }) => name), 'Folder', str, position)
565
- : [
566
- ...getCompletion(functions, 'Function', match, position, '', name => {
567
- if (!this.data) {
568
- return undefined;
569
- }
570
- else if (name in insensitive) {
571
- name = insensitive[name];
568
+ const [insensitive, sensitive] = this.config.parserFunction, isOld = Array.isArray(sensitive), next = curLine.charAt(character), colon = match.startsWith(':'), str = colon ? match.slice(1).trimStart() : match;
569
+ if (mt[2] === '[[') { // link
570
+ return getCompletion(root.querySelectorAll('link,file,category,redirect-target').filter(token => token !== cur).map(({ name }) => name), 'Folder', str, position);
571
+ }
572
+ // parser function or template
573
+ let words = functions;
574
+ if (next === ':') {
575
+ words = functions.filter(s => !s.endsWith(':'));
576
+ }
577
+ else if (next === ':') {
578
+ words = functions.filter(s => s.endsWith(':')).map(s => s.slice(0, -1));
579
+ }
580
+ return [
581
+ ...getCompletion(words, 'Function', match, position, '', name => {
582
+ if (!this.data) {
583
+ return undefined;
584
+ }
585
+ else if (name in insensitive) {
586
+ name = insensitive[name];
587
+ }
588
+ else if (!isOld && name in sensitive) {
589
+ name = sensitive[name];
590
+ }
591
+ return this.#getParserFunction(name.toLowerCase());
592
+ }),
593
+ ...match.startsWith('#')
594
+ ? []
595
+ : getCompletion(root.querySelectorAll('template').filter(token => token !== cur)
596
+ .map(token => {
597
+ const { name } = token;
598
+ if (colon) {
599
+ return name;
572
600
  }
573
- else if (!isOld && name in sensitive) {
574
- name = sensitive[name];
601
+ const { ns } = token.getAttribute('title');
602
+ if (ns === 0) {
603
+ return `:${name}`;
575
604
  }
576
- return this.#getParserFunction(name.toLowerCase());
577
- }),
578
- ...match.startsWith('#')
579
- ? []
580
- : getCompletion(root.querySelectorAll('template').filter(token => token !== cur)
581
- .map(token => {
582
- const { name } = token;
583
- if (colon) {
584
- return name;
585
- }
586
- const { ns } = token.getAttribute('title');
587
- if (ns === 0) {
588
- return `:${name}`;
589
- }
590
- return ns === 10 ? name.slice(9) : name;
591
- }), 'Folder', str, position),
592
- ];
605
+ return ns === 10 ? name.slice(9) : name;
606
+ }), 'Folder', str, position),
607
+ ];
593
608
  }
594
609
  let type, parentNode;
595
610
  if (mt?.[7] === undefined) {
@@ -598,7 +613,7 @@ class LanguageService {
598
613
  }
599
614
  if (mt?.[6] !== undefined || type === 'image-parameter') { // image parameter
600
615
  const index = root.indexFromPos(line, character), match = mt?.[6]?.trimStart()
601
- ?? this.#text.slice(cur.getAbsoluteIndex(), index).trimStart(), equal = this.#text.charAt(index) === '=';
616
+ ?? this.#text.slice(cur.getAbsoluteIndex(), index).trimStart(), equal = this.#text[index] === '=';
602
617
  return [
603
618
  ...getCompletion(params, 'Property', match, position)
604
619
  .filter(({ label }) => !equal || !/[= ]$/u.test(label)),
@@ -634,7 +649,7 @@ class LanguageService {
634
649
  if (t === 'magic-word' && n !== 'invoke') {
635
650
  return undefined;
636
651
  }
637
- const key = this.#text.slice(cur.getAbsoluteIndex(), root.indexFromPos(line, character)).trimStart(), [module, func] = t === 'magic-word' ? transclusion.getModule() : [];
652
+ const key = this.#text.slice(cur.getAbsoluteIndex(), root.indexFromPos(line, character)).trimStart(), [mod, func] = t === 'magic-word' ? transclusion.getModule() : [];
638
653
  return key
639
654
  ? getCompletion(root.querySelectorAll('parameter').filter(token => {
640
655
  if (token === parentNode
@@ -647,7 +662,7 @@ class LanguageService {
647
662
  return true;
648
663
  }
649
664
  const [m, f] = token.parentNode.getModule();
650
- return m === module && f === func;
665
+ return m === mod && f === func;
651
666
  }).map(({ name }) => name), 'Variable', key, position, type === 'parameter-value' ? '=' : '')
652
667
  : undefined;
653
668
  /* NOT FOR BROWSER ONLY */
@@ -673,7 +688,7 @@ class LanguageService {
673
688
  }
674
689
  const before = this.#text.slice(cur.getAbsoluteIndex(), root.indexFromPos(line, character)), comment = before.lastIndexOf('%');
675
690
  if (comment !== -1
676
- && (before.charAt(comment + 1) === '{' || !before.slice(comment).includes('\n'))) {
691
+ && (before[comment + 1] === '{' || !before.slice(comment).includes('\n'))) {
677
692
  return undefined;
678
693
  }
679
694
  const word = /\\?\b(?:\w|\b(?:->?|\.)|\bly:)+$/u.exec(curLine.slice(0, character))?.[0];
@@ -890,13 +905,18 @@ class LanguageService {
890
905
  */
891
906
  async provideLinks(text) {
892
907
  this.config ??= index_1.default.getConfig();
893
- /^(?:http:\/\/|\/\/)/iu; // eslint-disable-line @typescript-eslint/no-unused-expressions
894
- const { articlePath, protocol } = this.config, absolute = articlePath?.includes('//'), protocolRegex = new RegExp(`^(?:${protocol}|//)`, 'iu');
908
+ const { articlePath, protocol } = this.config, absolute = articlePath?.includes('//'), protocolRegex = getLinkRegex(protocol);
895
909
  return (await this.#queue(text))
896
- .querySelectorAll(`magic-link,ext-link-url,free-ext-link,attr-value,image-parameter#link${absolute ? ',link-target,template-name,invoke-module' : ''}`)
910
+ .querySelectorAll(`magic-link,ext-link-url,free-ext-link,attr-value,image-parameter#link${absolute ? ',link-target,template-name,invoke-module,magic-word#filepath,magic-word#widget' : ''}`)
897
911
  .reverse()
898
912
  .map((token) => {
899
- const { type, parentNode, firstChild, lastChild, childNodes, length } = token, { name, tag } = parentNode;
913
+ let name;
914
+ if (token.is('magic-word')) {
915
+ ({ name } = token);
916
+ token = token.childNodes[1].lastChild; // eslint-disable-line no-param-reassign
917
+ }
918
+ const { type, parentNode, firstChild, lastChild, childNodes, length } = token, { tag } = parentNode;
919
+ name ??= parentNode.name;
900
920
  if (!(type !== 'attr-value'
901
921
  || name === 'src' && ['templatestyles', 'img'].includes(tag)
902
922
  || name === 'cite' && ['blockquote', 'del', 'ins', 'q'].includes(tag))
@@ -927,21 +947,26 @@ class LanguageService {
927
947
  else if (type === 'template-name') {
928
948
  target = parentNode.getAttribute('title').getUrl(articlePath);
929
949
  }
930
- else if (['link-target', 'invoke-module'].includes(type)
950
+ else if (['link-target', 'invoke-module', 'parameter-value'].includes(type)
931
951
  || type === 'attr-value' && name === 'src' && tag === 'templatestyles'
932
952
  || type === 'image-parameter' && !protocolRegex.test(target)) {
933
953
  if (!absolute || target.startsWith('/')) {
934
954
  return false;
935
955
  }
936
956
  let ns = 0;
937
- if (type === 'attr-value') {
938
- ns = 10;
939
- }
940
- else if (type === 'invoke-module') {
941
- ns = 828;
957
+ switch (type) {
958
+ case 'attr-value':
959
+ ns = 10;
960
+ break;
961
+ case 'invoke-module':
962
+ ns = 828;
963
+ break;
964
+ case 'parameter-value':
965
+ ns = name === 'filepath' ? 6 : 274;
966
+ // no default
942
967
  }
943
- const title = index_1.default.normalizeTitle(target, ns, false, this.config, true);
944
- /* istanbul ignore if */
968
+ const title = index_1.default
969
+ .normalizeTitle(target, ns, false, this.config, { temporary: true });
945
970
  if (!title.valid) {
946
971
  return false;
947
972
  }
@@ -1091,23 +1116,26 @@ class LanguageService {
1091
1116
  offsetNode = offsetNode.parentNode;
1092
1117
  }
1093
1118
  const { type, parentNode, length, name } = offsetNode;
1094
- let info, f, range;
1119
+ let info, f, colon, range;
1095
1120
  if (offsetNode.is('double-underscore') && offset > 0) {
1096
1121
  info = this.#getBehaviorSwitch(offsetNode.name);
1097
1122
  }
1098
1123
  else if (type === 'magic-word-name') {
1099
1124
  info = this.#getParserFunction(parentNode.name);
1100
1125
  f = offsetNode.toString(true).trim();
1126
+ colon = parentNode.getAttribute('colon');
1101
1127
  }
1102
1128
  else if (offsetNode.is('magic-word') && !offsetNode.modifier && length === 1
1103
1129
  && (offset > 0 || root.posFromIndex(offsetNode.getAbsoluteIndex()).left === position.character)) {
1104
1130
  info = this.#getParserFunction(name);
1105
1131
  f = offsetNode.firstChild.toString(true).trim();
1132
+ colon = offsetNode.getAttribute('colon');
1106
1133
  }
1107
1134
  else if ((offsetNode.is('magic-word') || offsetNode.is('template'))
1108
1135
  && offsetNode.modifier && offset >= 2 && offsetNode.getRelativeIndex(0) > offset) {
1109
1136
  f = offsetNode.modifier.trim().slice(0, -1);
1110
1137
  info = this.#getParserFunction(f.toLowerCase());
1138
+ colon = ':';
1111
1139
  if (info) {
1112
1140
  const aIndex = offsetNode.getAbsoluteIndex();
1113
1141
  range = {
@@ -1158,7 +1186,7 @@ class LanguageService {
1158
1186
  contents: {
1159
1187
  kind: 'markdown',
1160
1188
  value: (info.signatures
1161
- ? `${info.signatures.map(params => `- **{{ ${f}${params.length === 0 ? '**' : ':** '}${params.map(({ label, const: c }) => c ? label : `*${label}*`).join(' **|** ')} **}}**`).join('\n')}\n\n`
1189
+ ? `${info.signatures.map(params => `- **{{ ${f}${params.length === 0 ? '**' : `${colon}** `}${params.map(({ label, const: c }) => c ? label : `*${label}*`).join(' **|** ')} **}}**`).join('\n')}\n\n`
1162
1190
  : '')
1163
1191
  + info.description,
1164
1192
  },
@@ -1200,10 +1228,10 @@ class LanguageService {
1200
1228
  break;
1201
1229
  }
1202
1230
  }
1203
- const f = firstChild.toString(true).trim();
1231
+ const f = firstChild.toString(true).trim(), colon = lastChild.getAttribute('colon');
1204
1232
  return {
1205
1233
  signatures: candidates.map((params) => ({
1206
- label: `{{${f}${params.length === 0 ? '' : ':'}${params.map(({ label }) => label).join('|')}}}`,
1234
+ label: `{{${f}${params.length === 0 ? '' : colon}${params.map(({ label }) => label).join('|')}}}`,
1207
1235
  parameters: params.map(({ label, const: c }) => ({
1208
1236
  label,
1209
1237
  ...c ? { documentation: 'Predefined parameter' } : undefined,
@@ -1315,7 +1343,7 @@ class LanguageService {
1315
1343
  this.config = index_1.default.getConfig(config);
1316
1344
  }
1317
1345
  catch {
1318
- this.config = index_1.default.getConfig(await (0, config_1.default)(site, `${mt[0]}/w`));
1346
+ this.config = index_1.default.getConfig(await (0, config_1.default)(site, `${mt[0]}/w`, false, true));
1319
1347
  }
1320
1348
  Object.assign(this.config, { articlePath: `${mt[0]}/wiki/` });
1321
1349
  }
@@ -0,0 +1,7 @@
1
+ /** 重定向列表 */
2
+ export declare class RedirectMap extends Map<string, string> {
3
+ #private;
4
+ /** @ignore */
5
+ constructor(entries?: Iterable<[string, string]> | Record<string, string>, redirect?: boolean);
6
+ set(key: string, value: string): this;
7
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedirectMap = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ /**
6
+ * 快速规范化页面标题
7
+ * @param title 标题
8
+ */
9
+ const normalizeTitle = (title) => {
10
+ const Parser = require('../index');
11
+ return String(Parser.normalizeTitle(title, 0, false, undefined, { temporary: true }));
12
+ };
13
+ /** 重定向列表 */
14
+ class RedirectMap extends Map {
15
+ #redirect;
16
+ /** @ignore */
17
+ constructor(entries, redirect = true) {
18
+ super();
19
+ this.#redirect = redirect;
20
+ if (entries) {
21
+ for (const [k, v] of Symbol.iterator in entries ? entries : Object.entries(entries)) {
22
+ this.set(k, v);
23
+ }
24
+ }
25
+ }
26
+ set(key, value) {
27
+ return super.set(normalizeTitle(key), this.#redirect ? normalizeTitle(value) : value);
28
+ }
29
+ }
30
+ exports.RedirectMap = RedirectMap;
31
+ constants_1.classes['RedirectMap'] = __filename;
@@ -69,9 +69,9 @@ export declare class AstText extends AstNode {
69
69
  */
70
70
  splitText(offset: number): AstText;
71
71
  /**
72
- * Escape `=`
72
+ * Escape `=` and `|`
73
73
  *
74
- * 转义 `=`
74
+ * 转义 `=` 和 `|`
75
75
  */
76
76
  escape(): void;
77
77
  /**