wikiparser-node 1.31.0 → 1.32.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.
Files changed (90) hide show
  1. package/README.md +1 -0
  2. package/bundle/bundle-es8.min.js +29 -30
  3. package/bundle/bundle-lsp.min.js +29 -29
  4. package/bundle/bundle.min.js +23 -23
  5. package/dist/addon/attribute.js +166 -0
  6. package/dist/addon/link.js +92 -0
  7. package/dist/addon/table.js +12 -3
  8. package/dist/addon/token.js +5 -303
  9. package/dist/addon/transclude.js +9 -6
  10. package/dist/base.d.mts +9 -1
  11. package/dist/base.d.ts +9 -1
  12. package/dist/bin/config.js +1 -1
  13. package/dist/index.d.ts +24 -5
  14. package/dist/index.js +69 -89
  15. package/dist/internal.d.ts +4 -0
  16. package/dist/lib/document.d.ts +9 -7
  17. package/dist/lib/document.js +91 -66
  18. package/dist/lib/element.d.ts +7 -7
  19. package/dist/lib/element.js +48 -50
  20. package/dist/lib/lintConfig.js +1 -1
  21. package/dist/lib/lsp.js +120 -56
  22. package/dist/lib/node.js +20 -14
  23. package/dist/lib/text.js +2 -2
  24. package/dist/lib/title.d.ts +11 -0
  25. package/dist/lib/title.js +37 -13
  26. package/dist/mixin/elementLike.js +2 -3
  27. package/dist/mixin/syntax.js +13 -7
  28. package/dist/parser/commentAndExt.js +3 -3
  29. package/dist/parser/selector.js +16 -41
  30. package/dist/render/expand.js +216 -0
  31. package/dist/render/extension.js +141 -0
  32. package/dist/render/html.js +91 -0
  33. package/dist/{addon → render}/magicWords.js +35 -1
  34. package/dist/render/syntaxhighlight.js +415 -0
  35. package/dist/src/arg.js +1 -1
  36. package/dist/src/attribute.d.ts +5 -0
  37. package/dist/src/attribute.js +58 -103
  38. package/dist/src/attributes.d.ts +5 -0
  39. package/dist/src/attributes.js +15 -50
  40. package/dist/src/converter.js +4 -2
  41. package/dist/src/converterFlags.js +8 -6
  42. package/dist/src/converterRule.js +19 -15
  43. package/dist/src/extLink.js +1 -1
  44. package/dist/src/heading.d.ts +1 -1
  45. package/dist/src/heading.js +5 -3
  46. package/dist/src/imageParameter.d.ts +0 -1
  47. package/dist/src/imageParameter.js +33 -24
  48. package/dist/src/index.d.ts +5 -5
  49. package/dist/src/index.js +33 -21
  50. package/dist/src/link/base.js +16 -34
  51. package/dist/src/link/category.d.ts +7 -0
  52. package/dist/src/link/category.js +101 -37
  53. package/dist/src/link/file.js +19 -6
  54. package/dist/src/link/galleryImage.js +1 -2
  55. package/dist/src/link/index.d.ts +1 -1
  56. package/dist/src/link/index.js +10 -31
  57. package/dist/src/multiLine/index.js +1 -1
  58. package/dist/src/nowiki/comment.js +1 -1
  59. package/dist/src/nowiki/doubleUnderscore.js +4 -2
  60. package/dist/src/nowiki/index.js +34 -31
  61. package/dist/src/nowiki/listBase.d.ts +2 -1
  62. package/dist/src/nowiki/listBase.js +22 -8
  63. package/dist/src/nowiki/quote.d.ts +1 -1
  64. package/dist/src/nowiki/quote.js +1 -1
  65. package/dist/src/onlyinclude.js +1 -1
  66. package/dist/src/parameter.js +1 -1
  67. package/dist/src/redirect.js +1 -1
  68. package/dist/src/table/base.js +4 -2
  69. package/dist/src/table/index.d.ts +0 -5
  70. package/dist/src/table/index.js +6 -15
  71. package/dist/src/table/td.js +9 -5
  72. package/dist/src/tag/index.js +1 -1
  73. package/dist/src/tagPair/ext.js +14 -38
  74. package/dist/src/tagPair/index.js +6 -4
  75. package/dist/src/tagPair/translate.js +2 -2
  76. package/dist/src/transclude.d.ts +0 -6
  77. package/dist/src/transclude.js +16 -33
  78. package/dist/util/constants.js +5 -1
  79. package/dist/util/debug.js +72 -5
  80. package/dist/util/diff.js +17 -15
  81. package/dist/util/html.js +4 -3
  82. package/dist/util/selector.js +37 -0
  83. package/dist/util/sharable.d.mts +4 -1
  84. package/dist/util/sharable.js +7 -7
  85. package/dist/util/sharable.mjs +7 -7
  86. package/dist/util/string.js +31 -12
  87. package/extensions/dist/base.js +1 -1
  88. package/i18n/zh-hans.json +29 -29
  89. package/i18n/zh-hant.json +30 -30
  90. package/package.json +16 -13
package/dist/base.d.mts CHANGED
@@ -6,7 +6,7 @@ export interface Config {
6
6
  readonly nsid: Record<string, number>;
7
7
  readonly variable: string[];
8
8
  readonly functionHook: string[];
9
- readonly parserFunction: [Record<string, string>, Record<string, string> | string[], string[], string[]];
9
+ readonly parserFunction: [Record<string, string>, Record<string, string>, string[], string[]];
10
10
  readonly doubleUnderscore: [string[], string[], Record<string, string>?, Record<string, string>?];
11
11
  readonly protocol: string;
12
12
  readonly interwiki: string[];
@@ -345,5 +345,13 @@ export interface Parser {
345
345
  * @since v1.16.1
346
346
  */
347
347
  createLanguageService(uri?: object): LanguageService;
348
+ /**
349
+ * Linter
350
+ * @param include whether to be transcluded / 是否嵌入
351
+ * @param page page name / 页面名称
352
+ * @since v1.32.0
353
+ */
354
+ lint(wikitext: string, include?: boolean, config?: Config, page?: string): LintError[];
355
+ lint(wikitext: string, page: string, include?: boolean, config?: Config): LintError[];
348
356
  }
349
357
  export {};
package/dist/base.d.ts CHANGED
@@ -6,7 +6,7 @@ export interface Config {
6
6
  readonly nsid: Record<string, number>;
7
7
  readonly variable: string[];
8
8
  readonly functionHook: string[];
9
- readonly parserFunction: [Record<string, string>, Record<string, string> | string[], string[], string[]];
9
+ readonly parserFunction: [Record<string, string>, Record<string, string>, string[], string[]];
10
10
  readonly doubleUnderscore: [string[], string[], Record<string, string>?, Record<string, string>?];
11
11
  readonly protocol: string;
12
12
  readonly interwiki: string[];
@@ -345,5 +345,13 @@ export interface Parser {
345
345
  * @since v1.16.1
346
346
  */
347
347
  createLanguageService(uri?: object): LanguageService;
348
+ /**
349
+ * Linter
350
+ * @param include whether to be transcluded / 是否嵌入
351
+ * @param page page name / 页面名称
352
+ * @since v1.32.0
353
+ */
354
+ lint(wikitext: string, include?: boolean, config?: Config, page?: string): LintError[];
355
+ lint(wikitext: string, page: string, include?: boolean, config?: Config): LintError[];
348
356
  }
349
357
  export {};
@@ -77,7 +77,7 @@ const mw = {
77
77
  },
78
78
  },
79
79
  };
80
- const pkg = "wikiparser-node", version = "1.31.0";
80
+ const pkg = "wikiparser-node", version = "1.32.0";
81
81
  let mwConfig;
82
82
  /**
83
83
  * Get the parser configuration for a Wikimedia Foundation project.
package/dist/index.d.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import { LintConfiguration } from './lib/lintConfig';
2
+ import { Title } from './lib/title';
2
3
  import type { Config, ConfigData, LintError, LintConfig, TokenTypes, Parser as ParserBase, Stage, AST } from './base';
3
- import type { Title, TitleOptions } from './lib/title';
4
+ import type { TitleOptions } from './lib/title';
4
5
  import type { LanguageService, QuickFixData } from './lib/lsp';
5
- import type { Token, TranscludeToken, ExtToken } from './internal';
6
+ import type { Token, FunctionHook, TagHook } from './internal';
6
7
  import type { AstRange } from './lib/range';
7
- declare type FunctionHook = (token: TranscludeToken, context?: TranscludeToken) => string;
8
- declare type TagHook = (token: ExtToken) => string;
9
8
  declare interface Parser extends ParserBase {
10
9
  default: Parser;
11
10
  /** @since v1.5.1 */
@@ -45,6 +44,16 @@ declare interface Parser extends ParserBase {
45
44
  * @since v1.16.1
46
45
  */
47
46
  createLanguageService(uri?: object): LanguageService;
47
+ /**
48
+ * print in HTML
49
+ *
50
+ * 以HTML格式打印
51
+ * @param include whether to be transcluded / 是否嵌入
52
+ * @param page page name / 页面名称
53
+ * @since v1.32.0
54
+ */
55
+ print(wikitext: string, include?: boolean, config?: Config, page?: string): string;
56
+ print(wikitext: string, page: string, include?: boolean, config?: Config): string;
48
57
  /**
49
58
  * get the name of a WMF site from a URL
50
59
  *
@@ -63,6 +72,16 @@ declare interface Parser extends ParserBase {
63
72
  * @since v1.18.4
64
73
  */
65
74
  fetchConfig(site: string, url: string, user?: string): Promise<Config>;
75
+ /**
76
+ * Generate HTML
77
+ *
78
+ * 生成HTML
79
+ * @param include whether to be transcluded / 是否嵌入
80
+ * @param page page name / 页面名称
81
+ * @since v1.32.0
82
+ */
83
+ toHtml(wikitext: string, include?: boolean, config?: Config, page?: string): string;
84
+ toHtml(wikitext: string, page: string, include?: boolean, config?: Config): string;
66
85
  /**
67
86
  * Define how to expand a parser function
68
87
  *
@@ -87,7 +106,7 @@ declare interface Parser extends ParserBase {
87
106
  * 调用一个解析器函数
88
107
  * @param name parser function name / 解析器函数名
89
108
  * @param args arguments / 参数
90
- * @since v1.30.1
109
+ * @since v1.31.0
91
110
  */
92
111
  callParserFunction(name: string, ...args: string[]): string;
93
112
  callParserFunction(name: string, args: string[] | Record<string, string>): string;
package/dist/index.js CHANGED
@@ -8,18 +8,17 @@ const debug_1 = require("./util/debug");
8
8
  const constants_1 = require("./util/constants");
9
9
  const string_1 = require("./util/string");
10
10
  const lintConfig_1 = require("./lib/lintConfig");
11
+ const title_1 = require("./lib/title");
11
12
  /* NOT FOR BROWSER */
12
- const common_1 = require("@bhsd/common");
13
13
  const redirectMap_1 = require("./lib/redirectMap");
14
14
  /* NOT FOR BROWSER END */
15
15
  /* NOT FOR BROWSER ONLY */
16
16
  const fs_1 = __importDefault(require("fs"));
17
17
  const path_1 = __importDefault(require("path"));
18
- const common_2 = require("@bhsd/common");
18
+ const common_1 = require("@bhsd/common");
19
19
  const diff_1 = require("./util/diff");
20
- const config_1 = __importDefault(require("./bin/config"));
21
20
  /* NOT FOR BROWSER ONLY */
22
- const re = new RegExp(String.raw `^https?:\/\/([^./]+)\.(${common_2.wmf})\.org`, 'iu');
21
+ const re = new RegExp(String.raw `^https?:\/\/([^./]+)\.(${common_1.wmf})\.org`, 'iu');
23
22
  /**
24
23
  * 从根路径require
25
24
  * @param file 文件名
@@ -32,13 +31,20 @@ const rootRequire = (file, dir) => require(path_1.default.isAbsolute(file)
32
31
  let viewOnly = false;
33
32
  /* NOT FOR BROWSER */
34
33
  const promises = [Promise.resolve()];
35
- /^(zh|en)\s*:/diu; // eslint-disable-line @typescript-eslint/no-unused-expressions
36
- const getInterwikiRegex = (0, common_1.getRegex)(interwiki => new RegExp(String.raw `^(${interwiki.join('|')})\s*:`, 'diu'));
37
34
  let redirectMap = new redirectMap_1.RedirectMap(), now;
38
35
  /* NOT FOR BROWSER END */
39
36
  let lintConfig = (() => {
40
37
  LINT: return new lintConfig_1.LintConfiguration();
41
38
  })(), i18n;
39
+ /**
40
+ * 判断参数顺序
41
+ * @param includeOrPage include or page
42
+ * @param configOrInclude config or include
43
+ * @param pageOrConfig page or config
44
+ */
45
+ const getParams = (includeOrPage, configOrInclude, pageOrConfig) => typeof includeOrPage === 'string'
46
+ ? [Boolean(configOrInclude), pageOrConfig, includeOrPage]
47
+ : [Boolean(includeOrPage), configOrInclude, pageOrConfig];
42
48
  const Parser = {
43
49
  config: 'default',
44
50
  /** @implements */
@@ -81,8 +87,6 @@ const Parser = {
81
87
  templates: new redirectMap_1.RedirectMap(undefined, false),
82
88
  warning: true,
83
89
  debugging: false,
84
- functionHooks: new Map(),
85
- tagHooks: new Map(),
86
90
  /** @implements */
87
91
  get now() {
88
92
  return now ?? new Date();
@@ -101,6 +105,9 @@ const Parser = {
101
105
  /* NOT FOR BROWSER ONLY */
102
106
  configPaths: [],
103
107
  /* NOT FOR BROWSER ONLY END */
108
+ /* PRINT ONLY */
109
+ internal: false,
110
+ /* PRINT ONLY END */
104
111
  /** @implements */
105
112
  getConfig(config) {
106
113
  /* NOT FOR BROWSER ONLY */
@@ -118,9 +125,7 @@ const Parser = {
118
125
  this.config = rootRequire(this.config, 'config');
119
126
  }
120
127
  /* istanbul ignore if */
121
- if (this.config.doubleUnderscore.length < 3
122
- || Array.isArray(this.config.parserFunction[1])
123
- || !('functionHook' in this.config)) {
128
+ if (this.config.doubleUnderscore.length < 3 || !('functionHook' in this.config)) {
124
129
  (0, diff_1.error)(`The schema (${path_1.default.join(__dirname, '..', 'config', '.schema.json')}) of parser configuration is updated.`);
125
130
  }
126
131
  return this.getConfig();
@@ -136,13 +141,7 @@ const Parser = {
136
141
  }
137
142
  if (ext.includes('translate') && !variable.includes('translationlanguage')) {
138
143
  variable.push('translationlanguage');
139
- /* istanbul ignore if */
140
- if (Array.isArray(parserFunction[1])) {
141
- parserFunction[1].push('TRANSLATIONLANGUAGE');
142
- }
143
- else {
144
- parserFunction[1]['TRANSLATIONLANGUAGE'] = 'translationlanguage';
145
- }
144
+ parserFunction[1]['TRANSLATIONLANGUAGE'] = 'translationlanguage';
146
145
  }
147
146
  /* NOT FOR BROWSER */
148
147
  if (conversionTable) {
@@ -164,10 +163,9 @@ const Parser = {
164
163
  },
165
164
  /** @implements */
166
165
  normalizeTitle(title, defaultNs = 0, include, config = Parser.getConfig(), opt) {
167
- const { Title } = require('./lib/title');
168
166
  let titleObj;
169
167
  if (opt?.halfParsed) {
170
- titleObj = new Title(title, defaultNs, config, opt);
168
+ titleObj = new title_1.Title(title, defaultNs, config, opt);
171
169
  }
172
170
  else {
173
171
  const { Token } = require('./src/index');
@@ -176,7 +174,7 @@ const Parser = {
176
174
  root.type = 'root';
177
175
  root.pageName = opt?.page;
178
176
  root.parseOnce(0, include).parseOnce();
179
- const t = new Title(root.firstChild.toString(), defaultNs, config, opt);
177
+ const t = new title_1.Title(root.firstChild.toString(), defaultNs, config, opt);
180
178
  root.build();
181
179
  for (const key of ['main', 'fragment']) {
182
180
  const str = t[key];
@@ -191,7 +189,7 @@ const Parser = {
191
189
  }
192
190
  }
193
191
  return t;
194
- });
192
+ }, this);
195
193
  }
196
194
  /* NOT FOR BROWSER */
197
195
  titleObj.conversionTable = this.conversionTable;
@@ -278,55 +276,27 @@ const Parser = {
278
276
  return this.parse(wikitext, include, maxStage, ref.getAttribute('config'), ref.pageName);
279
277
  },
280
278
  /** @implements */
281
- async partialParse(wikitext, watch, include, config = Parser.getConfig()) {
282
- LSP: {
283
- const { Token } = require('./src/index');
284
- const set = typeof setImmediate === 'function' ? setImmediate : /* istanbul ignore next */ setTimeout, { running } = debug_1.Shadow;
285
- debug_1.Shadow.running = true;
286
- const token = new Token((0, string_1.tidy)(wikitext), config);
287
- token.type = 'root';
288
- let i = 0;
289
- try {
290
- await new Promise(resolve => {
291
- const /** @ignore */ check = () => {
292
- if (watch() === wikitext) {
293
- i++;
294
- set(parseOnce, 0);
295
- }
296
- else {
297
- resolve();
298
- }
299
- },
300
- /** @ignore */ parseOnce = () => {
301
- if (i === constants_1.MAX_STAGE + 1) {
302
- token.afterBuild();
303
- resolve();
304
- }
305
- else {
306
- token[i === constants_1.MAX_STAGE ? 'build' : 'parseOnce'](i, include);
307
- check();
308
- }
309
- };
310
- set(parseOnce, 0);
311
- });
312
- }
313
- catch (e) /* istanbul ignore next */ {
314
- debug_1.Shadow.running = running;
315
- throw e;
316
- }
317
- debug_1.Shadow.running = running;
318
- return token;
319
- }
320
- },
321
- /** @implements */
322
279
  createLanguageService(uri = {}) {
323
280
  LSP: {
324
- const mod = require('./lib/lsp');
325
- const { LanguageService, tasks } = mod;
281
+ const { LanguageService, tasks } = require('./lib/lsp');
326
282
  this.viewOnly = true;
327
283
  return tasks.get(uri) ?? new LanguageService(uri);
328
284
  }
329
285
  },
286
+ /** @implements */
287
+ lint(wikitext, includeOrPage, configOrInclude, pageOrConfig) {
288
+ LINT: {
289
+ const [include, config, page] = getParams(includeOrPage, configOrInclude, pageOrConfig);
290
+ return debug_1.Shadow.internal(() => this.parse(wikitext, include, undefined, config, page).lint(), this);
291
+ }
292
+ },
293
+ /** @implements */
294
+ print(wikitext, includeOrPage, configOrInclude, pageOrConfig) {
295
+ PRINT: {
296
+ const [include, config, page] = getParams(includeOrPage, configOrInclude, pageOrConfig);
297
+ return debug_1.Shadow.internal(() => this.parse(wikitext, include, undefined, config, page).print(), this);
298
+ }
299
+ },
330
300
  /* NOT FOR BROWSER ONLY */
331
301
  /** @implements */
332
302
  getWMFSite(url) {
@@ -341,21 +311,27 @@ const Parser = {
341
311
  /* istanbul ignore next */
342
312
  /** @implements */
343
313
  async fetchConfig(site, url, user) {
344
- return this.getConfig(await (0, config_1.default)(site, url, user, false, true));
314
+ const { default: fetchConfig } = require('./bin/config');
315
+ return this.getConfig(await fetchConfig(site, url, user, false, true));
345
316
  },
346
317
  /* NOT FOR BROWSER ONLY END */
347
318
  /* NOT FOR BROWSER */
348
319
  /** @implements */
320
+ toHtml(wikitext, includeOrPage, configOrInclude, pageOrConfig) {
321
+ const [include, config, page] = getParams(includeOrPage, configOrInclude, pageOrConfig);
322
+ return debug_1.Shadow.internal(() => this.parse(wikitext, include, undefined, config, page).toHtml(), this);
323
+ },
324
+ /** @implements */
349
325
  setFunctionHook(name, hook) {
350
- this.functionHooks.set(name, hook);
326
+ constants_1.functionHooks.set((0, debug_1.getCanonicalName)(name, this.getConfig().parserFunction)[0], hook);
351
327
  },
352
328
  /** @implements */
353
329
  setHook(name, hook) {
354
- this.tagHooks.set(name, hook);
330
+ constants_1.tagHooks.set(name.toLowerCase(), hook);
355
331
  },
356
332
  /** @implements */
357
333
  callParserFunction(name, arg, ...args) {
358
- const { expandMagicWord } = require('./addon/magicWords'), { getCanonicalName } = require('./src/transclude');
334
+ const { expandMagicWord } = require('./render/magicWords');
359
335
  if (typeof arg === 'string') {
360
336
  args.unshift(arg);
361
337
  }
@@ -371,7 +347,25 @@ const Parser = {
371
347
  args.push(`${key}=${value}`);
372
348
  }
373
349
  }
374
- const [, , , canonicalName] = getCanonicalName(name, this.getConfig().parserFunction), result = expandMagicWord((canonicalName || name.toLowerCase()), args);
350
+ const { parserFunction } = this.getConfig(), [lcName, canonicalName] = (0, debug_1.getCanonicalName)(name, parserFunction);
351
+ let result;
352
+ if (constants_1.functionHooks.has(lcName)) {
353
+ if (!canonicalName) {
354
+ const [insensitive, sensitive] = parserFunction, entry = Object.entries(sensitive).find(([, v]) => v === lcName)
355
+ || Object.entries(insensitive).find(([, v]) => v === lcName);
356
+ /* istanbul ignore if */
357
+ if (!entry) {
358
+ throw new RangeError(`Unable to resolve parser function: ${name}`);
359
+ }
360
+ [name] = entry;
361
+ }
362
+ const { firstChild, length } = debug_1.Shadow.internal(() => this.parse(`{{${name}:${args.join('|')}}}`, false, 2), this);
363
+ result = length === 1 && firstChild.is('magic-word')
364
+ && constants_1.functionHooks.get(lcName)(firstChild);
365
+ }
366
+ else {
367
+ result = expandMagicWord(lcName, args);
368
+ }
375
369
  /* istanbul ignore if */
376
370
  if (result === false) {
377
371
  throw new RangeError(`Unable to resolve parser function: ${name}`);
@@ -382,26 +376,14 @@ const Parser = {
382
376
  warn(msg, ...args) {
383
377
  /* istanbul ignore if */
384
378
  if (this.warning) {
385
- try {
386
- const chalk = require('chalk');
387
- console.warn(chalk.yellow(msg), ...args);
388
- }
389
- catch {
390
- console.warn(msg, ...args);
391
- }
379
+ console.warn((0, diff_1.loadChalk)()?.yellow(msg) ?? msg, ...args);
392
380
  }
393
381
  },
394
382
  /** @implements */
395
383
  debug(msg, ...args) {
396
384
  /* istanbul ignore if */
397
385
  if (this.debugging) {
398
- try {
399
- const chalk = require('chalk');
400
- console.debug(chalk.blue(msg), ...args);
401
- }
402
- catch {
403
- console.debug(msg, ...args);
404
- }
386
+ console.debug((0, diff_1.loadChalk)()?.blue(msg) ?? msg, ...args);
405
387
  }
406
388
  },
407
389
  error: diff_1.error,
@@ -438,10 +420,8 @@ const Parser = {
438
420
  this.info('已重新加载Parser');
439
421
  },
440
422
  /** @implements */
441
- isInterwiki(title, { interwiki } = Parser.getConfig()) {
442
- return interwiki.length > 0
443
- ? getInterwikiRegex(interwiki).exec(title.replaceAll('_', ' ').replace(/^\s*:?\s*/u, ''))
444
- : null;
423
+ isInterwiki(title, config = Parser.getConfig()) {
424
+ return (0, string_1.isInterwiki)(title, config);
445
425
  },
446
426
  /* istanbul ignore next */
447
427
  /** @implements */
@@ -467,7 +447,7 @@ const Parser = {
467
447
  fs_1.default.unlinkSync(file);
468
448
  fs_1.default.unlinkSync(`${file}.err`);
469
449
  fs_1.default.unlinkSync(`${file}.json`);
470
- });
450
+ }, this);
471
451
  },
472
452
  };
473
453
  const def = {
@@ -48,3 +48,7 @@ export type { CommentedToken } from './src/commented';
48
48
  export type { TranslateToken } from './src/tagPair/translate';
49
49
  export type { TvarToken } from './src/tag/tvar';
50
50
  export type { ListRangeToken } from './src/nowiki/listBase';
51
+ import type { TranscludeToken } from './src/transclude';
52
+ import type { ExtToken } from './src/tagPair/ext';
53
+ export type FunctionHook = (token: TranscludeToken, context?: TranscludeToken) => string;
54
+ export type TagHook = (token: ExtToken) => string;
@@ -1,7 +1,9 @@
1
1
  import type { Position } from 'vscode-languageserver-types';
2
2
  import type { TextDocument } from 'vscode-languageserver-textdocument';
3
- import type { JSONDocument } from 'vscode-json-languageservice';
4
- import type { Stylesheet } from 'vscode-css-languageservice';
3
+ import type { JSONDocument, LanguageService as JSONLanguageService } from 'vscode-json-languageservice';
4
+ import type { Stylesheet, LanguageService as CSSLanguageService } from 'vscode-css-languageservice';
5
+ import type { IHTMLDataProvider } from 'vscode-html-languageservice';
6
+ import type { PublicApi } from 'stylelint';
5
7
  import type { Token } from '../internal';
6
8
  export interface TexvcLocation {
7
9
  offset: number;
@@ -26,12 +28,12 @@ declare interface Texvcjs {
26
28
  };
27
29
  };
28
30
  }
29
- export declare const texvcjs: Texvcjs | undefined;
31
+ export declare const loadTexvcjs: () => Texvcjs | null;
30
32
  export declare const jsonTags: string[];
31
- export declare const jsonLSP: import("vscode-json-languageservice").LanguageService | undefined;
32
- export declare const cssLSP: import("vscode-css-languageservice").LanguageService | undefined;
33
- export declare const htmlData: import("vscode-html-languageservice").IHTMLDataProvider | undefined;
34
- export declare const stylelint: Promise<import("stylelint").PublicApi | undefined>;
33
+ export declare const loadJsonLSP: () => JSONLanguageService | null;
34
+ export declare const loadCssLSP: () => CSSLanguageService | null;
35
+ export declare const loadHtmlData: () => IHTMLDataProvider | null;
36
+ export declare const loadStylelint: () => Promise<PublicApi | null>;
35
37
  /** embedded document */
36
38
  declare class EmbeddedDocument implements TextDocument {
37
39
  #private;
@@ -3,87 +3,112 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.EmbeddedCSSDocument = exports.EmbeddedJSONDocument = exports.stylelint = exports.htmlData = exports.cssLSP = exports.jsonLSP = exports.jsonTags = exports.texvcjs = void 0;
6
+ exports.EmbeddedCSSDocument = exports.EmbeddedJSONDocument = exports.loadStylelint = exports.loadHtmlData = exports.loadCssLSP = exports.loadJsonLSP = exports.jsonTags = exports.loadTexvcjs = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const common_1 = require("@bhsd/common");
9
9
  /* NOT FOR BROWSER */
10
10
  const constants_1 = require("../util/constants");
11
- exports.texvcjs = (() => {
11
+ let texcvjs;
12
+ const loadTexvcjs = () => {
12
13
  NPM: {
14
+ if (texcvjs === undefined) {
15
+ try {
16
+ texcvjs = require('mathoid-texvcjs');
17
+ }
18
+ catch {
19
+ /* istanbul ignore next */
20
+ texcvjs = null;
21
+ }
22
+ }
23
+ return texcvjs;
24
+ }
25
+ };
26
+ exports.loadTexvcjs = loadTexvcjs;
27
+ exports.jsonTags = ['templatedata', 'mapframe', 'maplink'];
28
+ let jsonLSP;
29
+ const loadJsonLSP = () => {
30
+ if (jsonLSP === undefined) {
13
31
  try {
14
- return require('mathoid-texvcjs');
32
+ jsonLSP = require('vscode-json-languageservice')
33
+ .getLanguageService({
34
+ /** @implements */
35
+ async schemaRequestService(uri) {
36
+ return (await fetch(uri)).text();
37
+ },
38
+ });
39
+ const dir = path_1.default.join('..', '..', 'data', 'ext');
40
+ jsonLSP.configure({
41
+ schemas: exports.jsonTags.map((tag) => {
42
+ const uri = path_1.default.join(dir, tag);
43
+ try {
44
+ const schema = require(tag === 'maplink' ? path_1.default.join(dir, 'mapframe') : uri);
45
+ return {
46
+ uri,
47
+ fileMatch: [tag],
48
+ schema,
49
+ };
50
+ }
51
+ catch {
52
+ /* istanbul ignore next */
53
+ return false;
54
+ }
55
+ }).filter(schema => schema !== false),
56
+ });
15
57
  }
16
58
  catch {
17
59
  /* istanbul ignore next */
18
- return undefined;
60
+ jsonLSP = null;
19
61
  }
20
62
  }
21
- })();
22
- exports.jsonTags = ['templatedata', 'mapframe', 'maplink'];
23
- exports.jsonLSP = (() => {
24
- try {
25
- const lsp = require('vscode-json-languageservice')
26
- .getLanguageService({
27
- /** @implements */
28
- async schemaRequestService(uri) {
29
- return (await fetch(uri)).text();
30
- },
31
- }), dir = path_1.default.join('..', '..', 'data', 'ext');
32
- lsp.configure({
33
- schemas: exports.jsonTags.map((tag) => {
34
- const uri = path_1.default.join(dir, tag);
35
- try {
36
- const schema = require(tag === 'maplink' ? path_1.default.join(dir, 'mapframe') : uri);
37
- return {
38
- uri,
39
- fileMatch: [tag],
40
- schema,
41
- };
42
- }
43
- catch {
44
- /* istanbul ignore next */
45
- return false;
46
- }
47
- }).filter(Boolean),
48
- });
49
- return lsp;
50
- }
51
- catch {
52
- /* istanbul ignore next */
53
- return undefined;
54
- }
55
- })();
56
- exports.cssLSP = (() => {
57
- try {
58
- return require('vscode-css-languageservice')
59
- .getCSSLanguageService();
60
- }
61
- catch {
62
- /* istanbul ignore next */
63
- return undefined;
64
- }
65
- })();
66
- exports.htmlData = (() => {
67
- try {
68
- return require('vscode-html-languageservice')
69
- .getDefaultHTMLDataProvider();
70
- }
71
- catch {
72
- /* istanbul ignore next */
73
- return undefined;
63
+ return jsonLSP;
64
+ };
65
+ exports.loadJsonLSP = loadJsonLSP;
66
+ let cssLSP;
67
+ const loadCssLSP = () => {
68
+ if (cssLSP === undefined) {
69
+ try {
70
+ cssLSP = require('vscode-css-languageservice')
71
+ .getCSSLanguageService();
72
+ }
73
+ catch {
74
+ /* istanbul ignore next */
75
+ cssLSP = null;
76
+ }
74
77
  }
75
- })();
76
- exports.stylelint = (async () => {
77
- NPM: {
78
+ return cssLSP;
79
+ };
80
+ exports.loadCssLSP = loadCssLSP;
81
+ let htmlData;
82
+ const loadHtmlData = () => {
83
+ if (htmlData === undefined) {
78
84
  try {
79
- return (await import('stylelint')).default;
85
+ htmlData = require('vscode-html-languageservice')
86
+ .getDefaultHTMLDataProvider();
80
87
  }
81
88
  catch {
82
89
  /* istanbul ignore next */
83
- return undefined;
90
+ htmlData = null;
84
91
  }
85
92
  }
86
- })();
93
+ return htmlData;
94
+ };
95
+ exports.loadHtmlData = loadHtmlData;
96
+ let stylelint;
97
+ const loadStylelint = () => {
98
+ NPM: {
99
+ stylelint ??= (async () => {
100
+ try {
101
+ return (await import('stylelint')).default;
102
+ }
103
+ catch {
104
+ /* istanbul ignore next */
105
+ return null;
106
+ }
107
+ })();
108
+ return stylelint;
109
+ }
110
+ };
111
+ exports.loadStylelint = loadStylelint;
87
112
  /** embedded document */
88
113
  class EmbeddedDocument {
89
114
  uri = '';
@@ -136,7 +161,7 @@ class EmbeddedJSONDocument extends EmbeddedDocument {
136
161
  constructor(root, token) {
137
162
  super('json', root, token);
138
163
  this.uri = token.name;
139
- this.jsonDoc = exports.jsonLSP.parseJSONDocument(this);
164
+ this.jsonDoc = (0, exports.loadJsonLSP)().parseJSONDocument(this);
140
165
  }
141
166
  }
142
167
  exports.EmbeddedJSONDocument = EmbeddedJSONDocument;
@@ -149,11 +174,11 @@ class EmbeddedCSSDocument extends EmbeddedDocument {
149
174
  constructor(root, token) {
150
175
  const { type, tag } = token.parentNode;
151
176
  super('css', root, token, `${type === 'ext-attr' ? 'div' : tag}{`, '}');
152
- this.styleSheet = exports.cssLSP.parseStylesheet(this);
177
+ this.styleSheet = (0, exports.loadCssLSP)().parseStylesheet(this);
153
178
  }
154
179
  getContent() {
155
180
  return (0, common_1.sanitizeInlineStyle)(super.getContent());
156
181
  }
157
182
  }
158
183
  exports.EmbeddedCSSDocument = EmbeddedCSSDocument;
159
- constants_1.classes['EmbeddedCSSDocument'] = __filename;
184
+ constants_1.classes['EmbeddedDocument'] = __filename;