wikiparser-node 1.4.2-b → 1.4.2

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 (162) hide show
  1. package/config/.schema.json +172 -0
  2. package/config/enwiki.json +814 -1
  3. package/config/llwiki.json +35 -1
  4. package/config/moegirl.json +44 -1
  5. package/config/zhwiki.json +466 -1
  6. package/dist/addon/table.d.ts +6 -0
  7. package/dist/addon/table.js +560 -0
  8. package/dist/base.d.ts +53 -0
  9. package/dist/index.d.ts +32 -0
  10. package/dist/index.js +211 -0
  11. package/dist/internal.d.ts +44 -0
  12. package/dist/lib/element.d.ts +154 -0
  13. package/dist/lib/element.js +643 -0
  14. package/dist/lib/node.d.ts +146 -0
  15. package/dist/lib/node.js +425 -0
  16. package/dist/lib/range.d.ts +104 -0
  17. package/dist/lib/range.js +385 -0
  18. package/dist/lib/ranges.d.ts +26 -0
  19. package/dist/lib/ranges.js +118 -0
  20. package/dist/lib/text.d.ts +62 -0
  21. package/dist/lib/text.js +306 -0
  22. package/dist/lib/title.d.ts +38 -0
  23. package/dist/lib/title.js +172 -0
  24. package/dist/mixin/attributesParent.d.ts +20 -0
  25. package/dist/mixin/attributesParent.js +89 -0
  26. package/dist/mixin/fixed.d.ts +5 -0
  27. package/dist/mixin/fixed.js +28 -0
  28. package/dist/mixin/flagsParent.d.ts +61 -0
  29. package/dist/mixin/flagsParent.js +72 -0
  30. package/dist/mixin/hidden.d.ts +5 -0
  31. package/dist/mixin/hidden.js +24 -0
  32. package/dist/mixin/magicLinkParent.d.ts +34 -0
  33. package/dist/mixin/magicLinkParent.js +43 -0
  34. package/dist/mixin/singleLine.d.ts +5 -0
  35. package/dist/mixin/singleLine.js +25 -0
  36. package/dist/mixin/sol.d.ts +8 -0
  37. package/dist/mixin/sol.js +41 -0
  38. package/dist/mixin/syntax.d.ts +6 -0
  39. package/dist/mixin/syntax.js +54 -0
  40. package/dist/parser/braces.js +131 -0
  41. package/dist/parser/commentAndExt.js +90 -0
  42. package/dist/parser/converter.js +41 -0
  43. package/dist/parser/externalLinks.js +32 -0
  44. package/dist/parser/hrAndDoubleUnderscore.js +41 -0
  45. package/dist/parser/html.js +39 -0
  46. package/dist/parser/links.js +97 -0
  47. package/dist/parser/list.js +68 -0
  48. package/dist/parser/magicLinks.js +43 -0
  49. package/dist/parser/quotes.js +68 -0
  50. package/dist/parser/selector.js +162 -0
  51. package/dist/parser/table.js +124 -0
  52. package/dist/src/arg.d.ts +58 -0
  53. package/dist/src/arg.js +196 -0
  54. package/dist/src/atom.d.ts +12 -0
  55. package/dist/src/atom.js +27 -0
  56. package/dist/src/attribute.d.ts +67 -0
  57. package/dist/src/attribute.js +422 -0
  58. package/dist/src/attributes.d.ts +109 -0
  59. package/dist/src/attributes.js +347 -0
  60. package/dist/src/converter.d.ts +28 -0
  61. package/dist/src/converter.js +85 -0
  62. package/dist/src/converterFlags.d.ts +87 -0
  63. package/dist/src/converterFlags.js +227 -0
  64. package/dist/src/converterRule.d.ts +77 -0
  65. package/dist/src/converterRule.js +210 -0
  66. package/dist/src/extLink.d.ts +38 -0
  67. package/dist/src/extLink.js +137 -0
  68. package/dist/src/gallery.d.ts +54 -0
  69. package/dist/src/gallery.js +127 -0
  70. package/dist/src/heading.d.ts +47 -0
  71. package/dist/src/heading.js +143 -0
  72. package/dist/src/hidden.d.ts +7 -0
  73. package/dist/src/hidden.js +23 -0
  74. package/dist/src/html.d.ts +62 -0
  75. package/dist/src/html.js +286 -0
  76. package/dist/src/imageParameter.d.ts +65 -0
  77. package/dist/src/imageParameter.js +247 -0
  78. package/dist/src/imagemap.d.ts +56 -0
  79. package/dist/src/imagemap.js +156 -0
  80. package/dist/src/imagemapLink.d.ts +28 -0
  81. package/dist/src/imagemapLink.js +44 -0
  82. package/dist/src/index.d.ts +143 -0
  83. package/dist/src/index.js +804 -0
  84. package/dist/src/link/base.d.ts +52 -0
  85. package/dist/src/link/base.js +212 -0
  86. package/dist/src/link/category.d.ts +13 -0
  87. package/dist/src/link/category.js +28 -0
  88. package/dist/src/link/file.d.ts +96 -0
  89. package/dist/src/link/file.js +266 -0
  90. package/dist/src/link/galleryImage.d.ts +31 -0
  91. package/dist/src/link/galleryImage.js +109 -0
  92. package/dist/src/link/index.d.ts +56 -0
  93. package/dist/src/link/index.js +134 -0
  94. package/dist/src/magicLink.d.ts +51 -0
  95. package/dist/src/magicLink.js +149 -0
  96. package/dist/src/nested.d.ts +43 -0
  97. package/dist/src/nested.js +91 -0
  98. package/dist/src/nowiki/base.d.ts +27 -0
  99. package/dist/src/nowiki/base.js +42 -0
  100. package/dist/src/nowiki/comment.d.ts +27 -0
  101. package/dist/src/nowiki/comment.js +77 -0
  102. package/dist/src/nowiki/dd.d.ts +8 -0
  103. package/dist/src/nowiki/dd.js +24 -0
  104. package/dist/src/nowiki/doubleUnderscore.d.ts +18 -0
  105. package/dist/src/nowiki/doubleUnderscore.js +51 -0
  106. package/dist/src/nowiki/hr.d.ts +8 -0
  107. package/dist/src/nowiki/hr.js +14 -0
  108. package/dist/src/nowiki/index.d.ts +16 -0
  109. package/dist/src/nowiki/index.js +20 -0
  110. package/dist/src/nowiki/list.d.ts +19 -0
  111. package/dist/src/nowiki/list.js +48 -0
  112. package/dist/src/nowiki/listBase.d.ts +5 -0
  113. package/dist/src/nowiki/listBase.js +11 -0
  114. package/dist/src/nowiki/noinclude.d.ts +10 -0
  115. package/dist/src/nowiki/noinclude.js +23 -0
  116. package/dist/src/nowiki/quote.d.ts +14 -0
  117. package/dist/src/nowiki/quote.js +68 -0
  118. package/dist/src/onlyinclude.d.ts +16 -0
  119. package/dist/src/onlyinclude.js +57 -0
  120. package/dist/src/paramTag/index.d.ts +37 -0
  121. package/dist/src/paramTag/index.js +68 -0
  122. package/dist/src/paramTag/inputbox.d.ts +8 -0
  123. package/dist/src/paramTag/inputbox.js +22 -0
  124. package/dist/src/parameter.d.ts +67 -0
  125. package/dist/src/parameter.js +213 -0
  126. package/dist/src/pre.d.ts +28 -0
  127. package/dist/src/pre.js +54 -0
  128. package/dist/src/syntax.d.ts +14 -0
  129. package/dist/src/syntax.js +35 -0
  130. package/dist/src/table/base.d.ts +27 -0
  131. package/dist/src/table/base.js +81 -0
  132. package/dist/src/table/index.d.ts +230 -0
  133. package/dist/src/table/index.js +390 -0
  134. package/dist/src/table/td.d.ts +88 -0
  135. package/dist/src/table/td.js +284 -0
  136. package/dist/src/table/tr.d.ts +32 -0
  137. package/dist/src/table/tr.js +57 -0
  138. package/dist/src/table/trBase.d.ts +53 -0
  139. package/dist/src/table/trBase.js +154 -0
  140. package/dist/src/tagPair/ext.d.ts +29 -0
  141. package/dist/src/tagPair/ext.js +153 -0
  142. package/dist/src/tagPair/include.d.ts +37 -0
  143. package/dist/src/tagPair/include.js +71 -0
  144. package/dist/src/tagPair/index.d.ts +27 -0
  145. package/dist/src/tagPair/index.js +79 -0
  146. package/dist/src/transclude.d.ts +167 -0
  147. package/dist/src/transclude.js +715 -0
  148. package/dist/util/constants.js +113 -0
  149. package/dist/util/debug.js +90 -0
  150. package/dist/util/diff.js +82 -0
  151. package/dist/util/lint.js +30 -0
  152. package/dist/util/string.js +53 -0
  153. package/errors/README +1 -0
  154. package/package.json +15 -30
  155. package/printed/README +1 -0
  156. package/bundle/bundle.min.js +0 -36
  157. package/extensions/dist/base.js +0 -64
  158. package/extensions/dist/editor.js +0 -159
  159. package/extensions/dist/highlight.js +0 -59
  160. package/extensions/dist/lint.js +0 -48
  161. package/extensions/editor.css +0 -63
  162. package/extensions/ui.css +0 -141
@@ -0,0 +1,31 @@
1
+ import Parser from '../../index';
2
+ import { Token } from '../index';
3
+ import type { Title } from '../../lib/title';
4
+ import type { LintError } from '../../base';
5
+ import type { AtomToken, ImageParameterToken } from '../../internal';
6
+ declare type GalleryTypes = 'gallery' | 'imagemap';
7
+ import {FileToken} from './file';
8
+ export declare abstract class GalleryImageToken extends FileToken {
9
+ #private;
10
+ type: `${GalleryTypes}-image`;
11
+ /** 图片链接 */
12
+ get link(): string | Title;
13
+ set link(value: string);
14
+ /**
15
+ * @param type 图片类型
16
+ * @param link 图片文件名
17
+ * @param text 图片参数
18
+ */
19
+ constructor(type: GalleryTypes, link: string, text?: string, config?: Parser.Config, accum?: Token[]);
20
+ /** @override */
21
+ lint(start?: number): LintError[];
22
+ /**
23
+ * @override
24
+ * @param token 待插入的子节点
25
+ * @param i 插入位置
26
+ * @throws `RangeError` 不可插入多余子节点
27
+ * @throws `TypeError` 不可插入文本节点
28
+ */
29
+ insertAt<T extends AtomToken | ImageParameterToken>(child: T, i?: number): T;
30
+ }
31
+ export {};
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GalleryImageToken = void 0;
4
+ const lint_1 = require("../../util/lint");
5
+ const constants_1 = require("../../util/constants");
6
+ const debug_1 = require("../../util/debug");
7
+ const singleLine_1 = require("../../mixin/singleLine");
8
+ const index_1 = require("../../index");
9
+ const index_2 = require("../index");
10
+ const file_1 = require("./file");
11
+ const imageParameter_1 = require("../imageParameter");
12
+ /** 图库图片 */
13
+ class GalleryImageToken extends (0, singleLine_1.singleLine)(file_1.FileToken) {
14
+ /* NOT FOR BROWSER */
15
+ /** 图片链接 */
16
+ get link() {
17
+ return this.type === 'imagemap-image' ? '' : super.link;
18
+ }
19
+ set link(value) {
20
+ if (this.type !== 'imagemap-image') {
21
+ super.link = value;
22
+ }
23
+ }
24
+ /* NOT FOR BROWSER END */
25
+ /**
26
+ * @param type 图片类型
27
+ * @param link 图片文件名
28
+ * @param text 图片参数
29
+ */
30
+ constructor(type, link, text, config = index_1.default.getConfig(), accum = []) {
31
+ let token;
32
+ if (text !== undefined) {
33
+ token = new index_2.Token(text, config, accum);
34
+ token.type = 'plain';
35
+ for (let n = 1; n < constants_1.MAX_STAGE; n++) {
36
+ token.parseOnce();
37
+ }
38
+ accum.splice(accum.indexOf(token), 1);
39
+ }
40
+ super(link, token?.toString(), config, accum);
41
+ this.setAttribute('bracket', false);
42
+ this.type = `${type}-image`;
43
+ }
44
+ /** @private */
45
+ getTitle() {
46
+ const imagemap = this.type === 'imagemap-image';
47
+ return this.normalizeTitle(String(this.firstChild), imagemap ? 0 : 6, true, !imagemap);
48
+ }
49
+ /** @private */
50
+ getAttribute(key) {
51
+ return key === 'padding' ? 0 : super.getAttribute(key);
52
+ }
53
+ /** @override */
54
+ lint(start = this.getAbsoluteIndex()) {
55
+ const errors = super.lint(start), { ns,
56
+ /* NOT FOR BROWSER */
57
+ interwiki, } = this.getAttribute('title');
58
+ if (interwiki || ns !== 6) {
59
+ errors.push((0, lint_1.generateForSelf)(this, { start }, 'invalid gallery image'));
60
+ }
61
+ return errors;
62
+ }
63
+ /**
64
+ * 设置`#title`
65
+ * @param title Title对象
66
+ */
67
+ #setName(title) {
68
+ this.setAttribute('title', title);
69
+ /* NOT FOR BROWSER */
70
+ this.setAttribute('name', title.title);
71
+ }
72
+ /** @private */
73
+ afterBuild() {
74
+ this.#setName(this.getTitle());
75
+ /* NOT FOR BROWSER */
76
+ const /** @implements */ linkListener = (e, data) => {
77
+ const { prevTarget } = e;
78
+ if (prevTarget?.type === 'link-target') {
79
+ const name = String(prevTarget), title = this.getTitle(), { interwiki, ns, valid } = title;
80
+ if (!valid) {
81
+ (0, debug_1.undo)(e, data);
82
+ throw new Error(`非法的图片文件名:${name}`);
83
+ }
84
+ else if (interwiki || ns !== 6) {
85
+ (0, debug_1.undo)(e, data);
86
+ throw new Error(`图片链接不可更改命名空间:${name}`);
87
+ }
88
+ this.#setName(title);
89
+ }
90
+ };
91
+ this.addEventListener(['remove', 'insert', 'replace', 'text'], linkListener);
92
+ }
93
+ /* NOT FOR BROWSER */
94
+ /**
95
+ * @override
96
+ * @param token 待插入的子节点
97
+ * @param i 插入位置
98
+ * @throws `RangeError` 不可插入多余子节点
99
+ * @throws `TypeError` 不可插入文本节点
100
+ */
101
+ insertAt(child, i) {
102
+ if (this.type === 'gallery-image' && child.type === 'image-parameter' && !imageParameter_1.galleryParams.has(child.name)) {
103
+ child.setAttribute('name', 'invalid');
104
+ }
105
+ return super.insertAt(child, i);
106
+ }
107
+ }
108
+ exports.GalleryImageToken = GalleryImageToken;
109
+ constants_1.classes['GalleryImageToken'] = __filename;
@@ -0,0 +1,56 @@
1
+ import { LinkBaseToken } from './base';
2
+ import type { LintError } from '../../base';
3
+ import type { Title } from '../../lib/title';
4
+ import type { Token, AtomToken } from '../../internal';
5
+ /**
6
+ * 内链
7
+ * @classdesc `{childNodes: [AtomToken, ?Token]}`
8
+ */
9
+ export declare abstract class LinkToken extends LinkBaseToken {
10
+ #private;
11
+ readonly type = "link";
12
+ readonly childNodes: readonly [AtomToken] | readonly [AtomToken, Token];
13
+ abstract get children(): [AtomToken] | [AtomToken, Token];
14
+ abstract get link(): Title;
15
+ abstract set link(link: Title);
16
+ /** 链接显示文字 */
17
+ get innerText(): string;
18
+ set innerText(text: string);
19
+ /** 是否链接到自身 */
20
+ get selfLink(): boolean;
21
+ set selfLink(selfLink: boolean);
22
+ /** @override */
23
+ get fragment(): string | undefined;
24
+ set fragment(fragment: string | undefined);
25
+ /** interwiki */
26
+ get interwiki(): string;
27
+ /** @throws `RangeError` 非法的跨维基前缀 */
28
+ set interwiki(interwiki: string);
29
+ /** @override */
30
+ lint(start?: number): LintError[];
31
+ /** @override */
32
+ setTarget(link: string): void;
33
+ /**
34
+ * 设置跨语言链接
35
+ * @param lang 语言前缀
36
+ * @param link 页面标题
37
+ * @throws `SyntaxError` 仅有fragment
38
+ */
39
+ setLangLink(lang: string, link: string): void;
40
+ /**
41
+ * 设置fragment
42
+ * @param fragment fragment
43
+ */
44
+ setFragment(fragment?: string): void;
45
+ /**
46
+ * 修改为到自身的链接
47
+ * @param fragment fragment
48
+ * @throws `RangeError` 空fragment
49
+ */
50
+ asSelfLink(fragment?: string | undefined): void;
51
+ /**
52
+ * 自动生成管道符后的链接文字
53
+ * @throws `Error` 带有"#"或"%"时不可用
54
+ */
55
+ pipeTrick(): void;
56
+ }
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LinkToken = void 0;
4
+ const lint_1 = require("../../util/lint");
5
+ const constants_1 = require("../../util/constants");
6
+ const base_1 = require("./base");
7
+ /**
8
+ * 内链
9
+ * @classdesc `{childNodes: [AtomToken, ?Token]}`
10
+ */
11
+ class LinkToken extends base_1.LinkBaseToken {
12
+ type = 'link';
13
+ /* NOT FOR BROWSER */
14
+ /** 链接显示文字 */
15
+ get innerText() {
16
+ return this.length > 1 ? this.lastChild.text() : this.firstChild.text().replace(/^\s*:/u, '');
17
+ }
18
+ set innerText(text) {
19
+ this.setLinkText(text);
20
+ }
21
+ /** 是否链接到自身 */
22
+ get selfLink() {
23
+ const { link: { title, fragment } } = this;
24
+ return !title && Boolean(fragment);
25
+ }
26
+ set selfLink(selfLink) {
27
+ if (selfLink) {
28
+ this.asSelfLink();
29
+ }
30
+ }
31
+ /** @override */
32
+ get fragment() {
33
+ return super.fragment;
34
+ }
35
+ set fragment(fragment) {
36
+ this.#setFragment(fragment);
37
+ }
38
+ /** interwiki */
39
+ get interwiki() {
40
+ return this.link.interwiki;
41
+ }
42
+ /** @throws `RangeError` 非法的跨维基前缀 */
43
+ set interwiki(interwiki) {
44
+ const { link: { prefix, main, fragment } } = this, link = `${interwiki}:${prefix}${main}${fragment === undefined ? '' : `#${fragment}`}`;
45
+ if (interwiki && !this.isInterwiki(link)) {
46
+ throw new RangeError(`${interwiki} 不是合法的跨维基前缀!`);
47
+ }
48
+ this.setTarget(link);
49
+ }
50
+ /* NOT FOR BROWSER END */
51
+ /** @override */
52
+ lint(start = this.getAbsoluteIndex()) {
53
+ const errors = super.lint(start);
54
+ if (this.closest('ext-link-text')) {
55
+ errors.push((0, lint_1.generateForSelf)(this, { start }, 'internal link in an external link'));
56
+ }
57
+ return errors;
58
+ }
59
+ /* NOT FOR BROWSER */
60
+ /** @override */
61
+ setTarget(link) {
62
+ super.setTarget(`${/^\s*[:#]/u.test(link) ? '' : ':'}${link}`);
63
+ }
64
+ /**
65
+ * 设置跨语言链接
66
+ * @param lang 语言前缀
67
+ * @param link 页面标题
68
+ * @throws `SyntaxError` 仅有fragment
69
+ */
70
+ setLangLink(lang, link) {
71
+ link = link.trim();
72
+ if (link.startsWith('#')) {
73
+ throw new SyntaxError('跨语言链接不能仅为fragment!');
74
+ }
75
+ super.setTarget(`${lang}${link.startsWith(':') ? '' : ':'}${link}`);
76
+ }
77
+ /**
78
+ * 设置fragment
79
+ * @param fragment fragment
80
+ * @param page 是否是其他页面
81
+ */
82
+ #setFragment(fragment, page = true) {
83
+ fragment &&= fragment.replace(/[<>[\]#|=]/gu, p => encodeURIComponent(p));
84
+ this.setTarget(`${page ? this.name : ''}${fragment === undefined ? '' : `#${fragment}`}`);
85
+ }
86
+ /**
87
+ * 设置fragment
88
+ * @param fragment fragment
89
+ */
90
+ setFragment(fragment) {
91
+ this.#setFragment(fragment);
92
+ }
93
+ /**
94
+ * 修改为到自身的链接
95
+ * @param fragment fragment
96
+ * @throws `RangeError` 空fragment
97
+ */
98
+ asSelfLink(fragment = this.fragment) {
99
+ if (!fragment?.trim()) {
100
+ throw new RangeError('asSelfLink 方法必须指定非空的 fragment!');
101
+ }
102
+ this.#setFragment(fragment, false);
103
+ }
104
+ /**
105
+ * 自动生成管道符后的链接文字
106
+ * @throws `Error` 带有"#"或"%"时不可用
107
+ */
108
+ pipeTrick() {
109
+ const linkText = this.firstChild.text();
110
+ if (linkText.includes('#') || linkText.includes('%')) {
111
+ throw new Error('Pipe trick 不能用于带有"#"或"%"的场合!');
112
+ }
113
+ const m1 = /^:?(?:[ \w\x80-\xFF-]+:)?([^(]+)\(.+\)$/u.exec(linkText);
114
+ if (m1) {
115
+ this.setLinkText(m1[1].trim());
116
+ return;
117
+ }
118
+ const m2 = /^:?(?:[ \w\x80-\xFF-]+:)?([^(]+)(.+)$/u.exec(linkText);
119
+ if (m2) {
120
+ this.setLinkText(m2[1].trim());
121
+ return;
122
+ }
123
+ // eslint-disable-next-line es-x/no-regexp-lookbehind-assertions
124
+ const m3 = /^:?(?:[ \w\x80-\xFF-]+:)?(.+?)(?:(?<!\()\(.+\))?(?:, |,|، )./u
125
+ .exec(linkText);
126
+ if (m3) {
127
+ this.setLinkText(m3[1].trim());
128
+ return;
129
+ }
130
+ this.setLinkText(linkText);
131
+ }
132
+ }
133
+ exports.LinkToken = LinkToken;
134
+ constants_1.classes['LinkToken'] = __filename;
@@ -0,0 +1,51 @@
1
+ import Parser from '../index';
2
+ import { Token } from './index';
3
+ import type { LintError } from '../base';
4
+ import type { AstText, CommentToken, IncludeToken, NoincludeToken, TranscludeToken, AstNodes } from '../internal';
5
+ export declare abstract class MagicLinkToken extends Token {
6
+ type: 'free-ext-link' | 'ext-link-url';
7
+ readonly childNodes: readonly (AstText | CommentToken | IncludeToken | NoincludeToken | TranscludeToken)[];
8
+ abstract get children(): (CommentToken | IncludeToken | NoincludeToken | TranscludeToken)[];
9
+ abstract get firstChild(): AstText | TranscludeToken;
10
+ abstract get firstElementChild(): CommentToken | IncludeToken | NoincludeToken | TranscludeToken | undefined;
11
+ abstract get lastChild(): AstText | CommentToken | IncludeToken | NoincludeToken | TranscludeToken;
12
+ abstract get lastElementChild(): CommentToken | IncludeToken | NoincludeToken | TranscludeToken | undefined;
13
+ /** 协议 */
14
+ get protocol(): string | undefined;
15
+ /** @throws `Error` 特殊外链无法更改协议n */
16
+ set protocol(value: string | undefined);
17
+ /** 和内链保持一致 */
18
+ get link(): string;
19
+ set link(url: string);
20
+ /**
21
+ * @param url 网址
22
+ * @param doubleSlash 是否接受"//"作为协议
23
+ */
24
+ constructor(url?: string, doubleSlash?: boolean, config?: Parser.Config, accum?: Token[]);
25
+ /** @override */
26
+ lint(start?: number): LintError[];
27
+ /** @override */
28
+ cloneNode(): this;
29
+ /**
30
+ * @override
31
+ * @param token 待插入的节点
32
+ * @param i 插入位置
33
+ */
34
+ insertAt(token: string, i?: number): AstText;
35
+ insertAt<T extends AstNodes>(token: T, i?: number): T;
36
+ /**
37
+ * 获取网址
38
+ * @throws `Error` 非标准协议
39
+ */
40
+ getUrl(): URL;
41
+ /**
42
+ * 设置外链目标
43
+ * @param url 含协议的网址
44
+ */
45
+ setTarget(url: string): void;
46
+ /** 是否是模板或魔术字参数 */
47
+ isParamValue(): boolean;
48
+ /** 转义 `=` */
49
+ escape(): void;
50
+ }
51
+ export {};
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MagicLinkToken = void 0;
4
+ const lint_1 = require("../util/lint");
5
+ const debug_1 = require("../util/debug");
6
+ const constants_1 = require("../util/constants");
7
+ const string_1 = require("../util/string");
8
+ const syntax_1 = require("../mixin/syntax");
9
+ const index_1 = require("../index");
10
+ const index_2 = require("./index");
11
+ /**
12
+ * 自由外链
13
+ * @classdesc `{childNodes: ...AstText|CommentToken|IncludeToken|NoincludeToken}`
14
+ */
15
+ class MagicLinkToken extends (0, syntax_1.syntax)(index_2.Token) {
16
+ /* NOT FOR BROWSER */
17
+ /** 协议 */
18
+ get protocol() {
19
+ return this.getAttribute('pattern').exec(this.text())?.[0];
20
+ }
21
+ /** @throws `Error` 特殊外链无法更改协议n */
22
+ set protocol(value) {
23
+ if (typeof value !== 'string') {
24
+ this.typeError('protocol', 'String');
25
+ }
26
+ const { link } = this, pattern = this.getAttribute('pattern');
27
+ if (!pattern.test(link)) {
28
+ throw new Error(`特殊外链无法更改协议:${link}`);
29
+ }
30
+ this.setTarget(link.replace(pattern, value));
31
+ }
32
+ /** 和内链保持一致 */
33
+ get link() {
34
+ const map = { '!': '|', '=': '=' };
35
+ return (0, string_1.text)(this.childNodes.map(child => {
36
+ const { type, name } = child;
37
+ return type === 'magic-word' && name in map ? map[name] : child;
38
+ }));
39
+ }
40
+ set link(url) {
41
+ this.setTarget(url);
42
+ }
43
+ /* NOT FOR BROWSER END */
44
+ /**
45
+ * @param url 网址
46
+ * @param doubleSlash 是否接受"//"作为协议
47
+ */
48
+ constructor(url, doubleSlash = false, config = index_1.default.getConfig(), accum = []) {
49
+ super(url, config, accum, {
50
+ 'Stage-1': '1:', '!ExtToken': '', AstText: ':', TranscludeToken: ':',
51
+ });
52
+ this.type = doubleSlash ? 'ext-link-url' : 'free-ext-link';
53
+ /* NOT FOR BROWSER */
54
+ this.setAttribute('pattern', new RegExp(`^(?:${config.protocol}${doubleSlash ? '|//' : ''})`, 'iu'));
55
+ }
56
+ /** @override */
57
+ lint(start = this.getAbsoluteIndex()) {
58
+ const errors = super.lint(start), source = `[,;。:!?()]+${this.type === 'ext-link-url' ? '|\\|+' : ''}`, regex = new RegExp(source, 'u'), regexGlobal = new RegExp(source, 'gu');
59
+ let rect;
60
+ for (const child of this.childNodes) {
61
+ const { type, data } = child;
62
+ if (type !== 'text' || !regex.test(data)) {
63
+ continue;
64
+ }
65
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
66
+ const refError = (0, lint_1.generateForChild)(child, rect, '', 'warning');
67
+ regexGlobal.lastIndex = 0;
68
+ for (let mt = regexGlobal.exec(data); mt; mt = regexGlobal.exec(data)) {
69
+ const { index, 0: s } = mt, lines = data.slice(0, index).split('\n'), top = lines.length, left = lines[top - 1].length, startIndex = refError.startIndex + index, startLine = refError.startLine + top - 1, startCol = top === 1 ? refError.startCol + left : left;
70
+ errors.push({
71
+ ...refError,
72
+ message: index_1.default.msg('$1 in URL', s.startsWith('|') ? '"|"' : 'full-width punctuation'),
73
+ startIndex,
74
+ endIndex: startIndex + s.length,
75
+ startLine,
76
+ endLine: startLine,
77
+ startCol,
78
+ endCol: startCol + s.length,
79
+ });
80
+ }
81
+ }
82
+ return errors;
83
+ }
84
+ /* NOT FOR BROWSER */
85
+ /** @override */
86
+ cloneNode() {
87
+ const cloned = this.cloneChildNodes();
88
+ return debug_1.Shadow.run(() => {
89
+ // @ts-expect-error abstract class
90
+ const token = new MagicLinkToken(undefined, this.type === 'ext-link-url', this.getAttribute('config'));
91
+ token.append(...cloned);
92
+ token.afterBuild();
93
+ return token;
94
+ });
95
+ }
96
+ insertAt(token, i) {
97
+ if (typeof token !== 'string') {
98
+ const { type, name } = token;
99
+ if (type === 'template') {
100
+ this.constructorError('不可插入模板');
101
+ }
102
+ else if (!debug_1.Shadow.running && type === 'magic-word' && name !== '!' && name !== '=') {
103
+ this.constructorError('不可插入 `{{!}}` 或 `{{=}}` 以外的魔术字');
104
+ }
105
+ }
106
+ return super.insertAt(token, i);
107
+ }
108
+ /**
109
+ * 获取网址
110
+ * @throws `Error` 非标准协议
111
+ */
112
+ getUrl() {
113
+ let { link } = this;
114
+ if (link.startsWith('//')) {
115
+ link = `https:${link}`;
116
+ }
117
+ try {
118
+ return new URL(link);
119
+ }
120
+ catch (e) {
121
+ if (e instanceof TypeError && e.message === 'Invalid URL') {
122
+ throw new Error(`非标准协议的外部链接:${link}`);
123
+ }
124
+ throw e;
125
+ }
126
+ }
127
+ /**
128
+ * 设置外链目标
129
+ * @param url 含协议的网址
130
+ */
131
+ setTarget(url) {
132
+ const { childNodes } = index_1.default.parse(url, this.getAttribute('include'), 2, this.getAttribute('config'));
133
+ this.replaceChildren(...childNodes);
134
+ }
135
+ /** 是否是模板或魔术字参数 */
136
+ isParamValue() {
137
+ return this.closest('parameter')?.getValue() === this.text();
138
+ }
139
+ /** 转义 `=` */
140
+ escape() {
141
+ for (const child of this.childNodes) {
142
+ if (child.type === 'text') {
143
+ child.escape();
144
+ }
145
+ }
146
+ }
147
+ }
148
+ exports.MagicLinkToken = MagicLinkToken;
149
+ constants_1.classes['MagicLinkToken'] = __filename;
@@ -0,0 +1,43 @@
1
+ import Parser from '../index';
2
+ import { Token } from './index';
3
+ import { ExtToken } from './tagPair/ext';
4
+ import { NoincludeToken } from './nowiki/noinclude';
5
+ import { CommentToken } from './nowiki/comment';
6
+ import type { LintError } from '../base';
7
+ import type { AttributesToken } from './attributes';
8
+ /**
9
+ * 嵌套式的扩展标签
10
+ * @classdesc `{childNodes: ...ExtToken|NoincludeToken|CommentToken}`
11
+ */
12
+ export declare abstract class NestedToken extends Token {
13
+ #private;
14
+ readonly type = "ext-inner";
15
+ readonly name: string;
16
+ readonly childNodes: readonly (ExtToken | NoincludeToken | CommentToken)[];
17
+ abstract get children(): (ExtToken | NoincludeToken | CommentToken)[];
18
+ abstract get firstChild(): ExtToken | NoincludeToken | CommentToken | undefined;
19
+ abstract get firstElementChild(): ExtToken | NoincludeToken | CommentToken | undefined;
20
+ abstract get lastChild(): ExtToken | NoincludeToken | CommentToken | undefined;
21
+ abstract get lastElementChild(): ExtToken | NoincludeToken | CommentToken | undefined;
22
+ abstract get nextSibling(): undefined;
23
+ abstract get nextElementSibling(): undefined;
24
+ abstract get previousSibling(): AttributesToken;
25
+ abstract get previousElementSibling(): AttributesToken;
26
+ abstract get parentNode(): ExtToken | undefined;
27
+ abstract get parentElement(): ExtToken | undefined;
28
+ /**
29
+ * @param regex 内层正则
30
+ * @param tags 内层标签名
31
+ */
32
+ constructor(wikitext: string | undefined, regex: RegExp, tags: readonly string[], config?: Parser.Config, accum?: Token[]);
33
+ /** @override */
34
+ lint(start?: number): LintError[];
35
+ /**
36
+ * @override
37
+ * @param token 待插入的子节点
38
+ * @param i 插入位置
39
+ */
40
+ insertAt<T extends Token>(token: T, i?: number): T;
41
+ /** @override */
42
+ cloneNode(): this;
43
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NestedToken = void 0;
4
+ const lint_1 = require("../util/lint");
5
+ const debug_1 = require("../util/debug");
6
+ const constants_1 = require("../util/constants");
7
+ const index_1 = require("../index");
8
+ const index_2 = require("./index");
9
+ const ext_1 = require("./tagPair/ext");
10
+ const noinclude_1 = require("./nowiki/noinclude");
11
+ const comment_1 = require("./nowiki/comment");
12
+ /**
13
+ * 嵌套式的扩展标签
14
+ * @classdesc `{childNodes: ...ExtToken|NoincludeToken|CommentToken}`
15
+ */
16
+ class NestedToken extends index_2.Token {
17
+ type = 'ext-inner';
18
+ /* NOT FOR BROWSER */
19
+ #tags;
20
+ #regex;
21
+ /**
22
+ * @param regex 内层正则
23
+ * @param tags 内层标签名
24
+ */
25
+ constructor(wikitext, regex, tags, config = index_1.default.getConfig(), accum = []) {
26
+ wikitext = wikitext?.replace(regex, (comment, name, attr, inner, closing) => {
27
+ const str = `\0${accum.length + 1}${name ? 'e' : 'c'}\x7F`;
28
+ if (name) {
29
+ // @ts-expect-error abstract class
30
+ new ext_1.ExtToken(name, attr, inner, closing, config, accum);
31
+ }
32
+ else {
33
+ const closed = comment.endsWith('-->');
34
+ // @ts-expect-error abstract class
35
+ new comment_1.CommentToken(comment.slice(4, closed ? -3 : undefined), closed, config, accum);
36
+ }
37
+ return str;
38
+ }).replace(/(^|\0\d+[ce]\x7F)([^\0]+)(?=$|\0\d+[ce]\x7F)/gu, (_, lead, substr) => {
39
+ // @ts-expect-error abstract class
40
+ new noinclude_1.NoincludeToken(substr, config, accum);
41
+ return `${lead}\0${accum.length}c\x7F`;
42
+ });
43
+ super(wikitext, config, accum, {
44
+ NoincludeToken: ':', ExtToken: ':',
45
+ });
46
+ /* NOT FOR BROWSER */
47
+ this.#tags = [...tags];
48
+ this.#regex = regex;
49
+ }
50
+ /** @override */
51
+ lint(start = this.getAbsoluteIndex()) {
52
+ let rect;
53
+ return [
54
+ ...super.lint(start),
55
+ ...this.childNodes.filter(child => {
56
+ if (child.type === 'ext' || child.type === 'comment') {
57
+ return false;
58
+ }
59
+ const str = String(child).trim();
60
+ return str && !/^<!--.*-->$/su.test(str);
61
+ }).map(child => {
62
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
63
+ return (0, lint_1.generateForChild)(child, rect, index_1.default.msg('invalid content in <$1>', this.name));
64
+ }),
65
+ ];
66
+ }
67
+ /* NOT FOR BROWSER */
68
+ /**
69
+ * @override
70
+ * @param token 待插入的子节点
71
+ * @param i 插入位置
72
+ */
73
+ insertAt(token, i) {
74
+ if (typeof token !== 'string' && token.type === 'ext' && !this.#tags.includes(token.name)) {
75
+ this.constructorError(`只能以 ${this.#tags.join(' 或 ')} 标签作为子节点`);
76
+ }
77
+ return super.insertAt(token, i);
78
+ }
79
+ /** @override */
80
+ cloneNode() {
81
+ const cloned = this.cloneChildNodes(), config = this.getAttribute('config');
82
+ return debug_1.Shadow.run(() => {
83
+ // @ts-expect-error abstract class
84
+ const token = new NestedToken(undefined, this.#regex, this.#tags, config);
85
+ token.append(...cloned);
86
+ return token;
87
+ });
88
+ }
89
+ }
90
+ exports.NestedToken = NestedToken;
91
+ constants_1.classes['NestedToken'] = __filename;