wikiparser-node 1.5.2-b → 1.5.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 (163) 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 +564 -0
  8. package/dist/base.d.ts +72 -0
  9. package/dist/base.js +38 -0
  10. package/dist/index.d.ts +30 -0
  11. package/dist/index.js +214 -0
  12. package/dist/internal.d.ts +44 -0
  13. package/dist/lib/element.d.ts +150 -0
  14. package/dist/lib/element.js +613 -0
  15. package/dist/lib/node.d.ts +146 -0
  16. package/dist/lib/node.js +428 -0
  17. package/dist/lib/range.d.ts +104 -0
  18. package/dist/lib/range.js +385 -0
  19. package/dist/lib/ranges.d.ts +26 -0
  20. package/dist/lib/ranges.js +118 -0
  21. package/dist/lib/text.d.ts +62 -0
  22. package/dist/lib/text.js +361 -0
  23. package/dist/lib/title.d.ts +38 -0
  24. package/dist/lib/title.js +166 -0
  25. package/dist/mixin/attributesParent.d.ts +50 -0
  26. package/dist/mixin/attributesParent.js +78 -0
  27. package/dist/mixin/fixed.d.ts +6 -0
  28. package/dist/mixin/fixed.js +30 -0
  29. package/dist/mixin/flagsParent.d.ts +72 -0
  30. package/dist/mixin/flagsParent.js +59 -0
  31. package/dist/mixin/hidden.d.ts +5 -0
  32. package/dist/mixin/hidden.js +24 -0
  33. package/dist/mixin/magicLinkParent.d.ts +40 -0
  34. package/dist/mixin/magicLinkParent.js +42 -0
  35. package/dist/mixin/singleLine.d.ts +6 -0
  36. package/dist/mixin/singleLine.js +26 -0
  37. package/dist/mixin/sol.d.ts +6 -0
  38. package/dist/mixin/sol.js +43 -0
  39. package/dist/mixin/syntax.d.ts +9 -0
  40. package/dist/mixin/syntax.js +50 -0
  41. package/dist/parser/braces.js +131 -0
  42. package/dist/parser/commentAndExt.js +90 -0
  43. package/dist/parser/converter.js +41 -0
  44. package/dist/parser/externalLinks.js +32 -0
  45. package/dist/parser/hrAndDoubleUnderscore.js +41 -0
  46. package/dist/parser/html.js +39 -0
  47. package/dist/parser/links.js +95 -0
  48. package/dist/parser/list.js +98 -0
  49. package/dist/parser/magicLinks.js +43 -0
  50. package/dist/parser/quotes.js +68 -0
  51. package/dist/parser/selector.js +162 -0
  52. package/dist/parser/table.js +124 -0
  53. package/dist/src/arg.d.ts +58 -0
  54. package/dist/src/arg.js +214 -0
  55. package/dist/src/atom.d.ts +12 -0
  56. package/dist/src/atom.js +27 -0
  57. package/dist/src/attribute.d.ts +71 -0
  58. package/dist/src/attribute.js +502 -0
  59. package/dist/src/attributes.d.ts +109 -0
  60. package/dist/src/attributes.js +356 -0
  61. package/dist/src/converter.d.ts +33 -0
  62. package/dist/src/converter.js +134 -0
  63. package/dist/src/converterFlags.d.ts +87 -0
  64. package/dist/src/converterFlags.js +242 -0
  65. package/dist/src/converterRule.d.ts +77 -0
  66. package/dist/src/converterRule.js +209 -0
  67. package/dist/src/extLink.d.ts +43 -0
  68. package/dist/src/extLink.js +186 -0
  69. package/dist/src/gallery.d.ts +54 -0
  70. package/dist/src/gallery.js +139 -0
  71. package/dist/src/heading.d.ts +47 -0
  72. package/dist/src/heading.js +190 -0
  73. package/dist/src/hidden.d.ts +7 -0
  74. package/dist/src/hidden.js +23 -0
  75. package/dist/src/html.d.ts +65 -0
  76. package/dist/src/html.js +358 -0
  77. package/dist/src/imageParameter.d.ts +65 -0
  78. package/dist/src/imageParameter.js +257 -0
  79. package/dist/src/imagemap.d.ts +56 -0
  80. package/dist/src/imagemap.js +150 -0
  81. package/dist/src/imagemapLink.d.ts +31 -0
  82. package/dist/src/imagemapLink.js +93 -0
  83. package/dist/src/index.d.ts +143 -0
  84. package/dist/src/index.js +813 -0
  85. package/dist/src/link/base.d.ts +52 -0
  86. package/dist/src/link/base.js +229 -0
  87. package/dist/src/link/category.d.ts +13 -0
  88. package/dist/src/link/category.js +28 -0
  89. package/dist/src/link/file.d.ts +98 -0
  90. package/dist/src/link/file.js +272 -0
  91. package/dist/src/link/galleryImage.d.ts +32 -0
  92. package/dist/src/link/galleryImage.js +157 -0
  93. package/dist/src/link/index.d.ts +56 -0
  94. package/dist/src/link/index.js +133 -0
  95. package/dist/src/magicLink.d.ts +59 -0
  96. package/dist/src/magicLink.js +223 -0
  97. package/dist/src/nested.d.ts +43 -0
  98. package/dist/src/nested.js +105 -0
  99. package/dist/src/nowiki/base.d.ts +31 -0
  100. package/dist/src/nowiki/base.js +93 -0
  101. package/dist/src/nowiki/comment.d.ts +25 -0
  102. package/dist/src/nowiki/comment.js +76 -0
  103. package/dist/src/nowiki/dd.d.ts +8 -0
  104. package/dist/src/nowiki/dd.js +24 -0
  105. package/dist/src/nowiki/doubleUnderscore.d.ts +18 -0
  106. package/dist/src/nowiki/doubleUnderscore.js +101 -0
  107. package/dist/src/nowiki/hr.d.ts +5 -0
  108. package/dist/src/nowiki/hr.js +63 -0
  109. package/dist/src/nowiki/index.d.ts +16 -0
  110. package/dist/src/nowiki/index.js +27 -0
  111. package/dist/src/nowiki/list.d.ts +16 -0
  112. package/dist/src/nowiki/list.js +97 -0
  113. package/dist/src/nowiki/listBase.d.ts +5 -0
  114. package/dist/src/nowiki/listBase.js +61 -0
  115. package/dist/src/nowiki/noinclude.d.ts +10 -0
  116. package/dist/src/nowiki/noinclude.js +23 -0
  117. package/dist/src/nowiki/quote.d.ts +14 -0
  118. package/dist/src/nowiki/quote.js +131 -0
  119. package/dist/src/onlyinclude.d.ts +16 -0
  120. package/dist/src/onlyinclude.js +57 -0
  121. package/dist/src/paramTag/index.d.ts +37 -0
  122. package/dist/src/paramTag/index.js +77 -0
  123. package/dist/src/paramTag/inputbox.d.ts +8 -0
  124. package/dist/src/paramTag/inputbox.js +22 -0
  125. package/dist/src/parameter.d.ts +70 -0
  126. package/dist/src/parameter.js +264 -0
  127. package/dist/src/pre.d.ts +28 -0
  128. package/dist/src/pre.js +55 -0
  129. package/dist/src/syntax.d.ts +20 -0
  130. package/dist/src/syntax.js +86 -0
  131. package/dist/src/table/base.d.ts +30 -0
  132. package/dist/src/table/base.js +132 -0
  133. package/dist/src/table/index.d.ts +233 -0
  134. package/dist/src/table/index.js +396 -0
  135. package/dist/src/table/td.d.ts +86 -0
  136. package/dist/src/table/td.js +350 -0
  137. package/dist/src/table/tr.d.ts +32 -0
  138. package/dist/src/table/tr.js +58 -0
  139. package/dist/src/table/trBase.d.ts +53 -0
  140. package/dist/src/table/trBase.js +152 -0
  141. package/dist/src/tagPair/ext.d.ts +32 -0
  142. package/dist/src/tagPair/ext.js +203 -0
  143. package/dist/src/tagPair/include.d.ts +35 -0
  144. package/dist/src/tagPair/include.js +74 -0
  145. package/dist/src/tagPair/index.d.ts +27 -0
  146. package/dist/src/tagPair/index.js +128 -0
  147. package/dist/src/transclude.d.ts +167 -0
  148. package/dist/src/transclude.js +717 -0
  149. package/dist/util/constants.js +28 -0
  150. package/dist/util/debug.js +88 -0
  151. package/dist/util/diff.js +83 -0
  152. package/dist/util/lint.js +31 -0
  153. package/dist/util/string.js +54 -0
  154. package/errors/README +1 -0
  155. package/package.json +14 -27
  156. package/printed/README +1 -0
  157. package/bundle/bundle.min.js +0 -37
  158. package/extensions/dist/base.js +0 -64
  159. package/extensions/dist/editor.js +0 -159
  160. package/extensions/dist/highlight.js +0 -59
  161. package/extensions/dist/lint.js +0 -60
  162. package/extensions/editor.css +0 -63
  163. package/extensions/ui.css +0 -141
@@ -0,0 +1,32 @@
1
+ import Parser from '../../index';
2
+ import { Token } from '../index';
3
+ import { FileToken } from './file';
4
+ import type { Title } from '../../lib/title';
5
+ import type { LintError } from '../../base';
6
+ import type { AtomToken, ImageParameterToken } from '../../internal';
7
+ declare type GalleryTypes = 'gallery' | 'imagemap';
8
+ /** 图库图片 */
9
+ export declare abstract class GalleryImageToken extends FileToken {
10
+ #private;
11
+ type: `${GalleryTypes}-image`;
12
+ /** 图片链接 */
13
+ get link(): string | Title;
14
+ set link(value: string);
15
+ /**
16
+ * @param type 图片类型
17
+ * @param link 图片文件名
18
+ * @param text 图片参数
19
+ */
20
+ constructor(type: GalleryTypes, link: string, text?: string, config?: Parser.Config, accum?: Token[]);
21
+ /** @override */
22
+ lint(start?: number): LintError[];
23
+ /**
24
+ * @override
25
+ * @param token 待插入的子节点
26
+ * @param i 插入位置
27
+ * @throws `RangeError` 不可插入多余子节点
28
+ * @throws `TypeError` 不可插入文本节点
29
+ */
30
+ insertAt<T extends AtomToken | ImageParameterToken>(child: T, i?: number): T;
31
+ }
32
+ export {};
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.GalleryImageToken = void 0;
38
+ const lint_1 = require("../../util/lint");
39
+ const constants_1 = require("../../util/constants");
40
+ const debug_1 = require("../../util/debug");
41
+ const singleLine_1 = require("../../mixin/singleLine");
42
+ const index_1 = require("../../index");
43
+ const index_2 = require("../index");
44
+ const file_1 = require("./file");
45
+ const imageParameter_1 = require("../imageParameter");
46
+ /** 图库图片 */
47
+ let GalleryImageToken = (() => {
48
+ let _classDecorators = [singleLine_1.singleLine];
49
+ let _classDescriptor;
50
+ let _classExtraInitializers = [];
51
+ let _classThis;
52
+ let _classSuper = file_1.FileToken;
53
+ var GalleryImageToken = class extends _classSuper {
54
+ static { _classThis = this; }
55
+ static {
56
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
57
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
58
+ GalleryImageToken = _classThis = _classDescriptor.value;
59
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
60
+ __runInitializers(_classThis, _classExtraInitializers);
61
+ }
62
+ /* NOT FOR BROWSER */
63
+ /** 图片链接 */
64
+ get link() {
65
+ return this.type === 'imagemap-image' ? '' : super.link;
66
+ }
67
+ set link(value) {
68
+ if (this.type !== 'imagemap-image') {
69
+ super.link = value;
70
+ }
71
+ }
72
+ /* NOT FOR BROWSER END */
73
+ /**
74
+ * @param type 图片类型
75
+ * @param link 图片文件名
76
+ * @param text 图片参数
77
+ */
78
+ constructor(type, link, text, config = index_1.default.getConfig(), accum = []) {
79
+ let token;
80
+ if (text !== undefined) {
81
+ token = new index_2.Token(text, config, accum);
82
+ token.type = 'plain';
83
+ for (let n = 1; n < constants_1.MAX_STAGE; n++) {
84
+ token.parseOnce();
85
+ }
86
+ accum.splice(accum.indexOf(token), 1);
87
+ }
88
+ super(link, token?.toString(), config, accum);
89
+ this.setAttribute('bracket', false);
90
+ this.type = `${type}-image`;
91
+ }
92
+ /** @private */
93
+ getTitle() {
94
+ const imagemap = this.type === 'imagemap-image';
95
+ return this.normalizeTitle(String(this.firstChild), imagemap ? 0 : 6, true, !imagemap);
96
+ }
97
+ /** @private */
98
+ getAttribute(key) {
99
+ return key === 'padding' ? 0 : super.getAttribute(key);
100
+ }
101
+ /** @override */
102
+ lint(start = this.getAbsoluteIndex()) {
103
+ const errors = super.lint(start), { ns, interwiki } = this.getAttribute('title');
104
+ if (interwiki || ns !== 6) {
105
+ errors.push((0, lint_1.generateForSelf)(this, { start }, 'invalid-gallery', 'invalid gallery image'));
106
+ }
107
+ return errors;
108
+ }
109
+ /**
110
+ * 设置`#title`
111
+ * @param title Title对象
112
+ */
113
+ #setName(title) {
114
+ this.setAttribute('title', title);
115
+ /* NOT FOR BROWSER */
116
+ this.setAttribute('name', title.title);
117
+ }
118
+ /** @private */
119
+ afterBuild() {
120
+ this.#setName(this.getTitle());
121
+ /* NOT FOR BROWSER */
122
+ const /** @implements */ linkListener = (e, data) => {
123
+ const { prevTarget } = e;
124
+ if (prevTarget?.type === 'link-target') {
125
+ const name = String(prevTarget), title = this.getTitle(), { interwiki, ns, valid } = title;
126
+ if (!valid) {
127
+ (0, debug_1.undo)(e, data);
128
+ throw new Error(`非法的图片文件名:${name}`);
129
+ }
130
+ else if (interwiki || ns !== 6) {
131
+ (0, debug_1.undo)(e, data);
132
+ throw new Error(`图片链接不可更改命名空间:${name}`);
133
+ }
134
+ this.#setName(title);
135
+ }
136
+ };
137
+ this.addEventListener(['remove', 'insert', 'replace', 'text'], linkListener);
138
+ }
139
+ /* NOT FOR BROWSER */
140
+ /**
141
+ * @override
142
+ * @param token 待插入的子节点
143
+ * @param i 插入位置
144
+ * @throws `RangeError` 不可插入多余子节点
145
+ * @throws `TypeError` 不可插入文本节点
146
+ */
147
+ insertAt(child, i) {
148
+ if (this.type === 'gallery-image' && child.type === 'image-parameter' && !imageParameter_1.galleryParams.has(child.name)) {
149
+ child.setAttribute('name', 'invalid');
150
+ }
151
+ return super.insertAt(child, i);
152
+ }
153
+ };
154
+ return GalleryImageToken = _classThis;
155
+ })();
156
+ exports.GalleryImageToken = GalleryImageToken;
157
+ 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,133 @@
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
+ /** 链接显示文字 */
14
+ get innerText() {
15
+ return this.length > 1 ? this.lastChild.text() : this.firstChild.text().replace(/^\s*:/u, '');
16
+ }
17
+ set innerText(text) {
18
+ this.setLinkText(text);
19
+ }
20
+ /** 是否链接到自身 */
21
+ get selfLink() {
22
+ const { link: { title, fragment } } = this;
23
+ return !title && Boolean(fragment);
24
+ }
25
+ set selfLink(selfLink) {
26
+ if (selfLink) {
27
+ this.asSelfLink();
28
+ }
29
+ }
30
+ /** @override */
31
+ get fragment() {
32
+ return super.fragment;
33
+ }
34
+ set fragment(fragment) {
35
+ this.#setFragment(fragment);
36
+ }
37
+ /** interwiki */
38
+ get interwiki() {
39
+ return this.link.interwiki;
40
+ }
41
+ /** @throws `RangeError` 非法的跨维基前缀 */
42
+ set interwiki(interwiki) {
43
+ const { link: { prefix, main, fragment } } = this, link = `${interwiki}:${prefix}${main}${fragment === undefined ? '' : `#${fragment}`}`;
44
+ if (interwiki && !this.isInterwiki(link)) {
45
+ throw new RangeError(`${interwiki} 不是合法的跨维基前缀!`);
46
+ }
47
+ this.setTarget(link);
48
+ }
49
+ /* NOT FOR BROWSER END */
50
+ /** @override */
51
+ lint(start = this.getAbsoluteIndex()) {
52
+ const errors = super.lint(start);
53
+ if (this.closest('ext-link-text')) {
54
+ errors.push((0, lint_1.generateForSelf)(this, { start }, 'nested-link', 'internal link in an external link'));
55
+ }
56
+ return errors;
57
+ }
58
+ /* NOT FOR BROWSER */
59
+ /** @override */
60
+ setTarget(link) {
61
+ super.setTarget(`${/^\s*[:#]/u.test(link) ? '' : ':'}${link}`);
62
+ }
63
+ /**
64
+ * 设置跨语言链接
65
+ * @param lang 语言前缀
66
+ * @param link 页面标题
67
+ * @throws `SyntaxError` 仅有fragment
68
+ */
69
+ setLangLink(lang, link) {
70
+ link = link.trim();
71
+ if (link.startsWith('#')) {
72
+ throw new SyntaxError('跨语言链接不能仅为fragment!');
73
+ }
74
+ super.setTarget(`${lang}${link.startsWith(':') ? '' : ':'}${link}`);
75
+ }
76
+ /**
77
+ * 设置fragment
78
+ * @param fragment fragment
79
+ * @param page 是否是其他页面
80
+ */
81
+ #setFragment(fragment, page = true) {
82
+ fragment &&= fragment.replace(/[<>[\]#|=]/gu, p => encodeURIComponent(p));
83
+ this.setTarget(`${page ? this.name : ''}${fragment === undefined ? '' : `#${fragment}`}`);
84
+ }
85
+ /**
86
+ * 设置fragment
87
+ * @param fragment fragment
88
+ */
89
+ setFragment(fragment) {
90
+ this.#setFragment(fragment);
91
+ }
92
+ /**
93
+ * 修改为到自身的链接
94
+ * @param fragment fragment
95
+ * @throws `RangeError` 空fragment
96
+ */
97
+ asSelfLink(fragment = this.fragment) {
98
+ if (!fragment?.trim()) {
99
+ throw new RangeError('asSelfLink 方法必须指定非空的 fragment!');
100
+ }
101
+ this.#setFragment(fragment, false);
102
+ }
103
+ /**
104
+ * 自动生成管道符后的链接文字
105
+ * @throws `Error` 带有"#"或"%"时不可用
106
+ */
107
+ pipeTrick() {
108
+ const linkText = this.firstChild.text();
109
+ if (linkText.includes('#') || linkText.includes('%')) {
110
+ throw new Error('Pipe trick 不能用于带有"#"或"%"的场合!');
111
+ }
112
+ const m1 = /^:?(?:[ \w\x80-\xFF-]+:)?([^(]+)\(.+\)$/u.exec(linkText);
113
+ if (m1) {
114
+ this.setLinkText(m1[1].trim());
115
+ return;
116
+ }
117
+ const m2 = /^:?(?:[ \w\x80-\xFF-]+:)?([^(]+)(.+)$/u.exec(linkText);
118
+ if (m2) {
119
+ this.setLinkText(m2[1].trim());
120
+ return;
121
+ }
122
+ // eslint-disable-next-line es-x/no-regexp-lookbehind-assertions
123
+ const m3 = /^:?(?:[ \w\x80-\xFF-]+:)?(.+?)(?:(?<!\()\(.+\))?(?:, |,|، )./u
124
+ .exec(linkText);
125
+ if (m3) {
126
+ this.setLinkText(m3[1].trim());
127
+ return;
128
+ }
129
+ this.setLinkText(linkText);
130
+ }
131
+ }
132
+ exports.LinkToken = LinkToken;
133
+ constants_1.classes['LinkToken'] = __filename;
@@ -0,0 +1,59 @@
1
+ import Parser from '../index';
2
+ import { Token } from './index';
3
+ import type { LintError } from '../base';
4
+ import type { SyntaxBase } from '../mixin/syntax';
5
+ import type { AstText, CommentToken, IncludeToken, NoincludeToken, TranscludeToken, AstNodes } from '../internal';
6
+ /** NOT FOR BROWSER */
7
+ export interface MagicLinkToken extends SyntaxBase {
8
+ }
9
+ /** NOT FOR BROWSER END */
10
+ /**
11
+ * 自由外链
12
+ * @classdesc `{childNodes: ...AstText|CommentToken|IncludeToken|NoincludeToken}`
13
+ */
14
+ export declare abstract class MagicLinkToken extends Token {
15
+ type: 'free-ext-link' | 'ext-link-url';
16
+ readonly childNodes: readonly (AstText | CommentToken | IncludeToken | NoincludeToken | TranscludeToken)[];
17
+ abstract get firstChild(): AstText | TranscludeToken;
18
+ abstract get lastChild(): AstText | CommentToken | IncludeToken | NoincludeToken | TranscludeToken;
19
+ abstract get children(): (CommentToken | IncludeToken | NoincludeToken | TranscludeToken)[];
20
+ abstract get firstElementChild(): CommentToken | IncludeToken | NoincludeToken | TranscludeToken | undefined;
21
+ abstract get lastElementChild(): CommentToken | IncludeToken | NoincludeToken | TranscludeToken | undefined;
22
+ /** 协议 */
23
+ get protocol(): string | undefined;
24
+ /** @throws `Error` 特殊外链无法更改协议n */
25
+ set protocol(value: string | undefined);
26
+ /** 和内链保持一致 */
27
+ get link(): string;
28
+ set link(url: string);
29
+ /**
30
+ * @param url 网址
31
+ * @param doubleSlash 是否接受"//"作为协议
32
+ */
33
+ constructor(url?: string, doubleSlash?: boolean, config?: Parser.Config, accum?: Token[]);
34
+ /** @override */
35
+ lint(start?: number): LintError[];
36
+ /** @override */
37
+ cloneNode(): this;
38
+ /**
39
+ * @override
40
+ * @param token 待插入的节点
41
+ * @param i 插入位置
42
+ */
43
+ insertAt(token: string, i?: number): AstText;
44
+ insertAt<T extends AstNodes>(token: T, i?: number): T;
45
+ /**
46
+ * 获取网址
47
+ * @throws `Error` 非标准协议
48
+ */
49
+ getUrl(): URL;
50
+ /**
51
+ * 设置外链目标
52
+ * @param url 含协议的网址
53
+ */
54
+ setTarget(url: string): void;
55
+ /** 是否是模板或魔术字参数 */
56
+ isParamValue(): boolean;
57
+ /** 转义 `=` */
58
+ escape(): void;
59
+ }
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.MagicLinkToken = void 0;
38
+ const lint_1 = require("../util/lint");
39
+ const debug_1 = require("../util/debug");
40
+ const constants_1 = require("../util/constants");
41
+ const string_1 = require("../util/string");
42
+ const syntax_1 = require("../mixin/syntax");
43
+ const index_1 = require("../index");
44
+ const index_2 = require("./index");
45
+ /** NOT FOR BROWSER END */
46
+ /**
47
+ * 自由外链
48
+ * @classdesc `{childNodes: ...AstText|CommentToken|IncludeToken|NoincludeToken}`
49
+ */
50
+ let MagicLinkToken = (() => {
51
+ let _classDecorators = [(0, syntax_1.syntax)()];
52
+ let _classDescriptor;
53
+ let _classExtraInitializers = [];
54
+ let _classThis;
55
+ let _classSuper = index_2.Token;
56
+ var MagicLinkToken = class extends _classSuper {
57
+ static { _classThis = this; }
58
+ static {
59
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
60
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
61
+ MagicLinkToken = _classThis = _classDescriptor.value;
62
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
63
+ __runInitializers(_classThis, _classExtraInitializers);
64
+ }
65
+ /** 协议 */
66
+ get protocol() {
67
+ return this.pattern.exec(this.text())?.[0];
68
+ }
69
+ /** @throws `Error` 特殊外链无法更改协议n */
70
+ set protocol(value) {
71
+ if (typeof value !== 'string') {
72
+ this.typeError('protocol', 'String');
73
+ }
74
+ const { link, pattern } = this;
75
+ if (!pattern.test(link)) {
76
+ throw new Error(`特殊外链无法更改协议:${link}`);
77
+ }
78
+ this.setTarget(link.replace(pattern, value));
79
+ }
80
+ /** 和内链保持一致 */
81
+ get link() {
82
+ const map = { '!': '|', '=': '=' };
83
+ return (0, string_1.text)(this.childNodes.map(child => {
84
+ const { type, name } = child;
85
+ return type === 'magic-word' && name in map ? map[name] : child;
86
+ }));
87
+ }
88
+ set link(url) {
89
+ this.setTarget(url);
90
+ }
91
+ /* NOT FOR BROWSER END */
92
+ /**
93
+ * @param url 网址
94
+ * @param doubleSlash 是否接受"//"作为协议
95
+ */
96
+ constructor(url, doubleSlash = false, config = index_1.default.getConfig(), accum = []) {
97
+ super(url, config, accum, {
98
+ 'Stage-1': '1:', '!ExtToken': '', AstText: ':', TranscludeToken: ':',
99
+ });
100
+ this.type = doubleSlash ? 'ext-link-url' : 'free-ext-link';
101
+ /* NOT FOR BROWSER */
102
+ this.setAttribute('pattern', new RegExp(`^(?:${config.protocol}${doubleSlash ? '|//' : ''})`, 'iu'));
103
+ }
104
+ /** @override */
105
+ lint(start = this.getAbsoluteIndex()) {
106
+ const errors = super.lint(start), source = `[,;。:!?()]+${this.type === 'ext-link-url' ? '|\\|+' : ''}`, regex = new RegExp(source, 'u'), regexGlobal = new RegExp(source, 'gu');
107
+ let rect;
108
+ for (const child of this.childNodes) {
109
+ const { type, data } = child;
110
+ if (type !== 'text' || !regex.test(data)) {
111
+ continue;
112
+ }
113
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
114
+ const refError = (0, lint_1.generateForChild)(child, rect, 'unterminated-url', '', 'warning');
115
+ regexGlobal.lastIndex = 0;
116
+ for (let mt = regexGlobal.exec(data); mt; mt = regexGlobal.exec(data)) {
117
+ 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, pipe = s.startsWith('|');
118
+ const e = {
119
+ ...refError,
120
+ message: index_1.default.msg('$1 in URL', pipe ? '"|"' : 'full-width punctuation'),
121
+ startIndex,
122
+ endIndex: startIndex + s.length,
123
+ startLine,
124
+ endLine: startLine,
125
+ startCol,
126
+ endCol: startCol + s.length,
127
+ };
128
+ if (!pipe) {
129
+ e.suggestions = [
130
+ {
131
+ desc: 'whitespace',
132
+ range: [startIndex, startIndex],
133
+ text: ' ',
134
+ },
135
+ {
136
+ desc: 'escape',
137
+ range: [startIndex, e.endIndex],
138
+ text: encodeURI(s),
139
+ },
140
+ ];
141
+ }
142
+ else if (s.length === 1) {
143
+ e.suggestions = [
144
+ {
145
+ desc: 'whitespace',
146
+ range: [startIndex, startIndex + 1],
147
+ text: ' ',
148
+ },
149
+ ];
150
+ }
151
+ errors.push(e);
152
+ }
153
+ }
154
+ return errors;
155
+ }
156
+ /* NOT FOR BROWSER */
157
+ /** @override */
158
+ cloneNode() {
159
+ const cloned = this.cloneChildNodes();
160
+ return debug_1.Shadow.run(() => {
161
+ // @ts-expect-error abstract class
162
+ const token = new MagicLinkToken(undefined, this.type === 'ext-link-url', this.getAttribute('config'));
163
+ token.append(...cloned);
164
+ token.afterBuild();
165
+ return token;
166
+ });
167
+ }
168
+ insertAt(token, i) {
169
+ if (typeof token !== 'string') {
170
+ const { type, name } = token;
171
+ if (type === 'template') {
172
+ this.constructorError('不可插入模板');
173
+ }
174
+ else if (!debug_1.Shadow.running && type === 'magic-word' && name !== '!' && name !== '=') {
175
+ this.constructorError('不可插入 "{{!}}" 或 "{{=}}" 以外的魔术字');
176
+ }
177
+ }
178
+ return super.insertAt(token, i);
179
+ }
180
+ /**
181
+ * 获取网址
182
+ * @throws `Error` 非标准协议
183
+ */
184
+ getUrl() {
185
+ let { link } = this;
186
+ if (link.startsWith('//')) {
187
+ link = `https:${link}`;
188
+ }
189
+ try {
190
+ return new URL(link);
191
+ }
192
+ catch (e) {
193
+ if (e instanceof TypeError && e.message === 'Invalid URL') {
194
+ throw new Error(`非标准协议的外部链接:${link}`);
195
+ }
196
+ throw e;
197
+ }
198
+ }
199
+ /**
200
+ * 设置外链目标
201
+ * @param url 含协议的网址
202
+ */
203
+ setTarget(url) {
204
+ const { childNodes } = index_1.default.parse(url, this.getAttribute('include'), 2, this.getAttribute('config'));
205
+ this.replaceChildren(...childNodes);
206
+ }
207
+ /** 是否是模板或魔术字参数 */
208
+ isParamValue() {
209
+ return this.closest('parameter')?.getValue() === this.text();
210
+ }
211
+ /** 转义 `=` */
212
+ escape() {
213
+ for (const child of this.childNodes) {
214
+ if (child.type === 'text') {
215
+ child.escape();
216
+ }
217
+ }
218
+ }
219
+ };
220
+ return MagicLinkToken = _classThis;
221
+ })();
222
+ exports.MagicLinkToken = MagicLinkToken;
223
+ constants_1.classes['MagicLinkToken'] = __filename;