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,227 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConverterFlagsToken = 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 atom_1 = require("./atom");
10
+ const definedFlags = new Set(['A', 'T', 'R', 'D', '-', 'H', 'N']);
11
+ /**
12
+ * 转换flags
13
+ * @classdesc `{childNodes: ...AtomToken}`
14
+ */
15
+ class ConverterFlagsToken extends index_2.Token {
16
+ type = 'converter-flags';
17
+ #flags;
18
+ /* NOT FOR BROWSER */
19
+ /** 所有转换类型标记 */
20
+ get flags() {
21
+ return this.getAllFlags();
22
+ }
23
+ set flags(value) {
24
+ this.replaceChildren();
25
+ for (const flag of value) {
26
+ this.#newFlag(flag);
27
+ }
28
+ }
29
+ /* NOT FOR BROWSER END */
30
+ /** @param flags 转换类型标记 */
31
+ constructor(flags, config = index_1.default.getConfig(), accum = []) {
32
+ super(undefined, config, accum, {
33
+ AtomToken: ':',
34
+ });
35
+ this.append(...flags.map(flag => new atom_1.AtomToken(flag, 'converter-flag', config, accum)));
36
+ }
37
+ /** @private */
38
+ afterBuild() {
39
+ this.#flags = this.childNodes.map(child => child.text().trim());
40
+ /* NOT FOR BROWSER */
41
+ const /** @implements */ converterFlagsListener = ({ prevTarget }) => {
42
+ if (prevTarget) {
43
+ this.#flags[this.childNodes.indexOf(prevTarget)] = prevTarget.text().trim();
44
+ }
45
+ };
46
+ this.addEventListener(['remove', 'insert', 'text', 'replace'], converterFlagsListener);
47
+ }
48
+ /** @private */
49
+ toString() {
50
+ return super.toString(';');
51
+ }
52
+ /** @override */
53
+ text() {
54
+ return super.text(';');
55
+ }
56
+ /** @private */
57
+ getGaps() {
58
+ return 1;
59
+ }
60
+ /** 获取未知的转换类型标记 */
61
+ getUnknownFlags() {
62
+ return new Set(this.#flags.filter(flag => /\{{3}[^{}]+\}{3}/u.test(flag)));
63
+ }
64
+ /** 获取指定语言变体的转换标记 */
65
+ getVariantFlags() {
66
+ const variants = new Set(this.getAttribute('config').variants);
67
+ return new Set(this.#flags.filter(flag => variants.has(flag)));
68
+ }
69
+ /** @override */
70
+ lint(start = this.getAbsoluteIndex()) {
71
+ const variantFlags = this.getVariantFlags(), unknownFlags = this.getUnknownFlags(), validFlags = new Set(this.#flags.filter(flag => definedFlags.has(flag))), emptyFlagCount = this.#flags.filter(flag => !flag).length, knownFlagCount = this.#flags.length - unknownFlags.size - emptyFlagCount, errors = super.lint(start);
72
+ if (variantFlags.size === knownFlagCount || validFlags.size === knownFlagCount) {
73
+ return errors;
74
+ }
75
+ const rect = { start, ...this.getRootNode().posFromIndex(start) }, { childNodes, length } = this;
76
+ for (let i = 0; i < length; i++) {
77
+ const child = childNodes[i], flag = child.text().trim();
78
+ if (flag
79
+ && !variantFlags.has(flag)
80
+ && !unknownFlags.has(flag)
81
+ && (variantFlags.size > 0 || !validFlags.has(flag))) {
82
+ errors.push((0, lint_1.generateForChild)(child, rect, 'invalid conversion flag'));
83
+ }
84
+ }
85
+ return errors;
86
+ }
87
+ /** @override */
88
+ print() {
89
+ return super.print({ sep: ';' });
90
+ }
91
+ /* NOT FOR BROWSER */
92
+ /** @override */
93
+ cloneNode() {
94
+ const cloned = this.cloneChildNodes();
95
+ return debug_1.Shadow.run(() => {
96
+ // @ts-expect-error abstract class
97
+ const token = new ConverterFlagsToken([], this.getAttribute('config'));
98
+ token.append(...cloned);
99
+ token.afterBuild();
100
+ return token;
101
+ });
102
+ }
103
+ /** @private */
104
+ getAttribute(key) {
105
+ return key === 'flags' ? this.#flags : super.getAttribute(key);
106
+ }
107
+ /**
108
+ * @override
109
+ * @param i 移除位置
110
+ */
111
+ removeAt(i) {
112
+ const token = super.removeAt(i);
113
+ this.#flags?.splice(i, 1);
114
+ return token;
115
+ }
116
+ /**
117
+ * @override
118
+ * @param token 待插入的子节点
119
+ * @param i 插入位置
120
+ */
121
+ insertAt(token, i = this.length) {
122
+ super.insertAt(token, i);
123
+ this.#flags?.splice(i, 0, token.text().trim());
124
+ return token;
125
+ }
126
+ /** 获取所有转换类型标记 */
127
+ getAllFlags() {
128
+ return new Set(this.#flags);
129
+ }
130
+ /**
131
+ * 获取转换类型标记节点
132
+ * @param flag 转换类型标记
133
+ */
134
+ getFlagTokens(flag) {
135
+ return this.#flags.includes(flag) ? this.childNodes.filter(child => child.text().trim() === flag) : [];
136
+ }
137
+ /** 获取有效的转换类型标记 */
138
+ getEffectiveFlags() {
139
+ const variantFlags = this.getVariantFlags(), unknownFlags = this.getUnknownFlags();
140
+ if (variantFlags.size > 0) {
141
+ return new Set([...variantFlags, ...unknownFlags]);
142
+ }
143
+ const flags = new Set([...this.#flags.filter(flag => definedFlags.has(flag)), ...unknownFlags]);
144
+ if (flags.size === 0) {
145
+ return new Set('S');
146
+ }
147
+ else if (flags.has('R')) {
148
+ return new Set('R');
149
+ }
150
+ else if (flags.has('N')) {
151
+ return new Set('N');
152
+ }
153
+ else if (flags.has('-')) {
154
+ return new Set('-');
155
+ }
156
+ else if (flags.has('H')) {
157
+ const hasT = flags.has('T'), hasD = flags.has('D');
158
+ return hasT && hasD
159
+ ? new Set(['+', 'H', 'T', 'D'])
160
+ : new Set(['+', 'H', ...hasT ? ['T'] : [], ...hasD ? ['D'] : [], ...unknownFlags]);
161
+ }
162
+ if (flags.size === 1 && flags.has('T')) {
163
+ flags.add('H');
164
+ }
165
+ if (flags.has('A')) {
166
+ flags.add('+');
167
+ flags.add('S');
168
+ }
169
+ if (flags.has('D')) {
170
+ flags.delete('S');
171
+ }
172
+ return flags;
173
+ }
174
+ /**
175
+ * 是否具有某转换类型标记
176
+ * @param flag 转换类型标记
177
+ */
178
+ hasFlag(flag) {
179
+ return this.#flags.includes(flag);
180
+ }
181
+ /**
182
+ * 是否具有某有效的转换类型标记
183
+ * @param flag 转换类型标记
184
+ */
185
+ hasEffectiveFlag(flag) {
186
+ return this.getEffectiveFlags().has(flag);
187
+ }
188
+ /**
189
+ * 移除某转换类型标记
190
+ * @param flag 转换类型标记
191
+ */
192
+ removeFlag(flag) {
193
+ for (const token of this.getFlagTokens(flag)) {
194
+ token.remove();
195
+ }
196
+ }
197
+ /**
198
+ * 添加转换类型标记
199
+ * @param flag 转换类型标记
200
+ */
201
+ #newFlag(flag) {
202
+ this.insertAt(debug_1.Shadow.run(() => new atom_1.AtomToken(flag, 'converter-flag', this.getAttribute('config'))));
203
+ }
204
+ /**
205
+ * 设置转换类型标记
206
+ * @param flag 转换类型标记
207
+ */
208
+ setFlag(flag) {
209
+ if (!this.#flags.includes(flag)) {
210
+ this.#newFlag(flag);
211
+ }
212
+ }
213
+ /**
214
+ * 开关转换类型标记
215
+ * @param flag 转换类型标记
216
+ */
217
+ toggleFlag(flag) {
218
+ if (this.#flags.includes(flag)) {
219
+ this.removeFlag(flag);
220
+ }
221
+ else {
222
+ this.#newFlag(flag);
223
+ }
224
+ }
225
+ }
226
+ exports.ConverterFlagsToken = ConverterFlagsToken;
227
+ constants_1.classes['ConverterFlagsToken'] = __filename;
@@ -0,0 +1,77 @@
1
+ import Parser from '../index';
2
+ import { Token } from './index';
3
+ import { AtomToken } from './atom';
4
+ import type { ConverterToken, ConverterFlagsToken } from '../internal';
5
+ /**
6
+ * 转换规则
7
+ * @classdesc `{childNodes: ...AtomToken}`
8
+ */
9
+ export declare abstract class ConverterRuleToken extends Token {
10
+ readonly type = "converter-rule";
11
+ readonly childNodes: readonly [AtomToken] | readonly [AtomToken, AtomToken] | readonly [AtomToken, AtomToken, AtomToken];
12
+ abstract get children(): [AtomToken] | [AtomToken, AtomToken] | [AtomToken, AtomToken, AtomToken];
13
+ abstract get firstChild(): AtomToken;
14
+ abstract get firstElementChild(): AtomToken;
15
+ abstract get lastChild(): AtomToken;
16
+ abstract get lastElementChild(): AtomToken;
17
+ abstract get parentNode(): ConverterToken | undefined;
18
+ abstract get parentElement(): ConverterToken | undefined;
19
+ abstract get previousSibling(): ConverterFlagsToken | this;
20
+ abstract get previousElementSibling(): ConverterFlagsToken | this;
21
+ abstract get nextSibling(): this | undefined;
22
+ abstract get nextElementSibling(): this | undefined;
23
+ /** 语言变体 */
24
+ get variant(): string;
25
+ set variant(variant: string);
26
+ /** 是否是单向转换 */
27
+ get unidirectional(): boolean;
28
+ /** @throws `Error` 不能用于将双向转换或不转换更改为单向转换 */
29
+ set unidirectional(flag: boolean);
30
+ /** 是否是双向转换 */
31
+ get bidirectional(): boolean;
32
+ /** @throws `Error` 不能用于将双向转换更改为单向转换或将不转换更改为双向转换 */
33
+ set bidirectional(flag: boolean);
34
+ /**
35
+ * @param rule 转换规则
36
+ * @param hasColon 是否带有":"
37
+ */
38
+ constructor(rule: string, hasColon?: boolean, config?: Parser.Config, accum?: Token[]);
39
+ /** @override */
40
+ text(): string;
41
+ /** @override */
42
+ print(): string;
43
+ /** @override */
44
+ cloneNode(): this;
45
+ /**
46
+ * @override
47
+ * @param i 移除位置
48
+ */
49
+ removeAt(i: number): AtomToken;
50
+ /** @override */
51
+ insertAt(): never;
52
+ /** 修改为不转换 */
53
+ noConvert(): void;
54
+ /**
55
+ * 设置转换目标
56
+ * @param to 转换目标
57
+ */
58
+ setTo(to: string): void;
59
+ /**
60
+ * 设置语言变体
61
+ * @param variant 语言变体
62
+ */
63
+ setVariant(variant: string): void;
64
+ /**
65
+ * 设置转换原文
66
+ * @param from 转换原文
67
+ * @throws `Error` 尚未指定语言变体
68
+ */
69
+ setFrom(from: string): void;
70
+ /**
71
+ * 修改为单向转换
72
+ * @param from 转换原文
73
+ */
74
+ makeUnidirectional(from: string): void;
75
+ /** 修改为双向转换 */
76
+ makeBidirectional(): void;
77
+ }
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConverterRuleToken = void 0;
4
+ const debug_1 = require("../util/debug");
5
+ const constants_1 = require("../util/constants");
6
+ const index_1 = require("../index");
7
+ const index_2 = require("./index");
8
+ const atom_1 = require("./atom");
9
+ /**
10
+ * 转换规则
11
+ * @classdesc `{childNodes: ...AtomToken}`
12
+ */
13
+ class ConverterRuleToken extends index_2.Token {
14
+ type = 'converter-rule';
15
+ /* NOT FOR BROWSER */
16
+ /** 语言变体 */
17
+ get variant() {
18
+ return this.childNodes[this.length - 2]?.text().trim() ?? '';
19
+ }
20
+ set variant(variant) {
21
+ this.setVariant(variant);
22
+ }
23
+ /** 是否是单向转换 */
24
+ get unidirectional() {
25
+ return this.length === 3;
26
+ }
27
+ /** @throws `Error` 不能用于将双向转换或不转换更改为单向转换 */
28
+ set unidirectional(flag) {
29
+ const { length } = this;
30
+ if (length === 3 && !flag) {
31
+ this.makeBidirectional();
32
+ }
33
+ else if (length === 2 && flag) {
34
+ throw new Error('想更改为单向转换请使用 makeUnidirectional 方法!');
35
+ }
36
+ else if (length === 1 && flag) {
37
+ throw new Error('想更改为单向转换请先使用 setVariant 方法指定语言变体!');
38
+ }
39
+ }
40
+ /** 是否是双向转换 */
41
+ get bidirectional() {
42
+ return this.length === 2;
43
+ }
44
+ /** @throws `Error` 不能用于将双向转换更改为单向转换或将不转换更改为双向转换 */
45
+ set bidirectional(flag) {
46
+ const { length } = this;
47
+ if (length === 3 && flag) {
48
+ this.makeBidirectional();
49
+ }
50
+ else if (length === 2 && !flag) {
51
+ throw new Error('想更改为单向转换请使用 makeUnidirectional 方法!');
52
+ }
53
+ else if (length === 1 && flag) {
54
+ throw new Error('想更改为双向转换请使用 setVariant 方法!');
55
+ }
56
+ }
57
+ /* NOT FOR BROWSER END */
58
+ /**
59
+ * @param rule 转换规则
60
+ * @param hasColon 是否带有":"
61
+ */
62
+ constructor(rule, hasColon = true, config = index_1.default.getConfig(), accum = []) {
63
+ super(undefined, config, accum);
64
+ const i = rule.indexOf(':'), j = rule.slice(0, i).indexOf('=>'), v = j === -1 ? rule.slice(0, i) : rule.slice(j + 2, i);
65
+ if (hasColon && config.variants.includes(v.trim())) {
66
+ super.insertAt(new atom_1.AtomToken(v, 'converter-rule-variant', config, accum));
67
+ super.insertAt(new atom_1.AtomToken(rule.slice(i + 1), 'converter-rule-to', config, accum));
68
+ if (j !== -1) {
69
+ super.insertAt(new atom_1.AtomToken(rule.slice(0, j), 'converter-rule-from', config, accum), 0);
70
+ }
71
+ }
72
+ else {
73
+ super.insertAt(new atom_1.AtomToken(rule, 'converter-rule-to', config, accum));
74
+ }
75
+ /* NOT FOR BROWSER */
76
+ this.protectChildren('1:');
77
+ }
78
+ /** @private */
79
+ toString() {
80
+ const { childNodes } = this;
81
+ if (childNodes.length === 3) {
82
+ const [from, variant, to] = childNodes;
83
+ return `${String(from)}=>${String(variant)}:${String(to)}`;
84
+ }
85
+ return super.toString(':');
86
+ }
87
+ /** @override */
88
+ text() {
89
+ const { childNodes } = this;
90
+ if (childNodes.length === 3) {
91
+ const [from, variant, to] = childNodes;
92
+ return `${from.text()}=>${variant.text()}:${to.text()}`;
93
+ }
94
+ return super.text(':');
95
+ }
96
+ /** @private */
97
+ getGaps(i) {
98
+ return i === 0 && this.length === 3 ? 2 : 1;
99
+ }
100
+ /** @override */
101
+ print() {
102
+ const { childNodes } = this;
103
+ if (childNodes.length === 3) {
104
+ const [from, variant, to] = childNodes;
105
+ return `<span class="wpb-converter-rule">${from.print()}=>${variant.print()}:${to.print()}</span>`;
106
+ }
107
+ return super.print({ sep: ':' });
108
+ }
109
+ /* NOT FOR BROWSER */
110
+ /** @override */
111
+ cloneNode() {
112
+ const cloned = this.cloneChildNodes(), placeholders = ['', 'zh:', '=>zh:'], placeholder = placeholders[cloned.length - 1];
113
+ return debug_1.Shadow.run(() => {
114
+ // @ts-expect-error abstract class
115
+ const token = new ConverterRuleToken(placeholder, Boolean(placeholder), this.getAttribute('config'));
116
+ for (let i = 0; i < cloned.length; i++) {
117
+ token.childNodes[i].safeReplaceWith(cloned[i]);
118
+ }
119
+ token.afterBuild();
120
+ return token;
121
+ });
122
+ }
123
+ /** @private */
124
+ afterBuild() {
125
+ const /** @implements */ converterRuleListener = (e, data) => {
126
+ const { prevTarget } = e;
127
+ if (this.length > 1 && this.childNodes[this.length - 2] === prevTarget) {
128
+ const { variant } = this;
129
+ if (!this.getAttribute('config').variants.includes(variant)) {
130
+ (0, debug_1.undo)(e, data);
131
+ throw new Error(`无效的语言变体:${variant}`);
132
+ }
133
+ }
134
+ };
135
+ this.addEventListener(['remove', 'insert', 'text', 'replace'], converterRuleListener);
136
+ }
137
+ /**
138
+ * @override
139
+ * @param i 移除位置
140
+ */
141
+ removeAt(i) {
142
+ if (this.length === 1) {
143
+ this.constructorError('需至少保留 1 个子节点');
144
+ }
145
+ return super.removeAt(i);
146
+ }
147
+ /** @override */
148
+ insertAt() {
149
+ this.constructorError('语法复杂,请勿尝试手动插入子节点');
150
+ }
151
+ /** 修改为不转换 */
152
+ noConvert() {
153
+ const { length } = this;
154
+ for (let i = 0; i < length - 1; i++) { // ConverterRuleToken只能从前往后删除子节点
155
+ this.removeAt(0);
156
+ }
157
+ }
158
+ /**
159
+ * 设置转换目标
160
+ * @param to 转换目标
161
+ */
162
+ setTo(to) {
163
+ const { childNodes } = index_1.default.parse(to, this.getAttribute('include'), undefined, this.getAttribute('config'));
164
+ this.lastChild.replaceChildren(...childNodes);
165
+ }
166
+ /**
167
+ * 设置语言变体
168
+ * @param variant 语言变体
169
+ */
170
+ setVariant(variant) {
171
+ const config = this.getAttribute('config');
172
+ if (this.length === 1) {
173
+ super.insertAt(debug_1.Shadow.run(() => new atom_1.AtomToken(variant, 'converter-rule-variant', config)), 0);
174
+ }
175
+ else {
176
+ this.childNodes[this.length - 2].setText(variant);
177
+ }
178
+ }
179
+ /**
180
+ * 设置转换原文
181
+ * @param from 转换原文
182
+ * @throws `Error` 尚未指定语言变体
183
+ */
184
+ setFrom(from) {
185
+ const { variant, unidirectional } = this;
186
+ if (!variant) {
187
+ throw new Error('请先指定语言变体!');
188
+ }
189
+ const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(from, this.getAttribute('include'), undefined, config);
190
+ if (!unidirectional) {
191
+ super.insertAt(debug_1.Shadow.run(() => new atom_1.AtomToken(undefined, 'converter-rule-from', config)), 0);
192
+ }
193
+ this.firstChild.replaceChildren(...childNodes);
194
+ }
195
+ /**
196
+ * 修改为单向转换
197
+ * @param from 转换原文
198
+ */
199
+ makeUnidirectional(from) {
200
+ this.setFrom(from);
201
+ }
202
+ /** 修改为双向转换 */
203
+ makeBidirectional() {
204
+ if (this.unidirectional) {
205
+ this.removeAt(0);
206
+ }
207
+ }
208
+ }
209
+ exports.ConverterRuleToken = ConverterRuleToken;
210
+ constants_1.classes['ConverterRuleToken'] = __filename;
@@ -0,0 +1,38 @@
1
+ import Parser from '../index';
2
+ import { Token } from './index';
3
+ import { MagicLinkToken } from './magicLink';
4
+ import type { LintError } from '../base';
5
+ import type { MagicLinkParentBase } from '../mixin/magicLinkParent';
6
+ export interface ExtLinkToken extends MagicLinkParentBase {
7
+ }
8
+ export declare abstract class ExtLinkToken extends Token {
9
+ #private;
10
+ readonly type = "ext-link";
11
+ readonly childNodes: readonly [MagicLinkToken] | readonly [MagicLinkToken, Token];
12
+ abstract get children(): [MagicLinkToken] | [MagicLinkToken, Token];
13
+ abstract get lastChild(): Token;
14
+ abstract get lastElementChild(): Token;
15
+ /** 链接显示文字 */
16
+ get innerText(): string;
17
+ set innerText(text: string);
18
+ /**
19
+ * @param url 网址
20
+ * @param space 空白字符
21
+ * @param text 链接文字
22
+ */
23
+ constructor(url?: string, space?: string, text?: string, config?: Parser.Config, accum?: Token[]);
24
+ /** @override */
25
+ text(): string;
26
+ /** @override */
27
+ lint(start?: number): LintError[];
28
+ /** @override */
29
+ print(): string;
30
+ /** @override */
31
+ cloneNode(): this;
32
+ /**
33
+ * 设置链接显示文字
34
+ * @param str 链接显示文字
35
+ */
36
+ setLinkText(str: string): void;
37
+ }
38
+ export {};
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExtLinkToken = void 0;
4
+ const constants_1 = require("../util/constants");
5
+ const lint_1 = require("../util/lint");
6
+ const string_1 = require("../util/string");
7
+ const debug_1 = require("../util/debug");
8
+ const magicLinkParent_1 = require("../mixin/magicLinkParent");
9
+ const index_1 = require("../index");
10
+ const index_2 = require("./index");
11
+ const magicLink_1 = require("./magicLink");
12
+ /**
13
+ * 外链
14
+ * @classdesc `{childNodes: [MagicLinkToken, ?Token]}`
15
+ */
16
+ class ExtLinkToken extends (0, magicLinkParent_1.magicLinkParent)(index_2.Token) {
17
+ type = 'ext-link';
18
+ #space;
19
+ /* NOT FOR BROWSER */
20
+ /** 链接显示文字 */
21
+ get innerText() {
22
+ return this.length > 1
23
+ ? this.lastChild.text()
24
+ : `[${this.getRootNode().querySelectorAll('ext-link[childElementCount=1]').indexOf(this) + 1}]`;
25
+ }
26
+ set innerText(text) {
27
+ this.setLinkText(text);
28
+ }
29
+ /* NOT FOR BROWSER END */
30
+ /**
31
+ * @param url 网址
32
+ * @param space 空白字符
33
+ * @param text 链接文字
34
+ */
35
+ constructor(url, space = '', text = '', config = index_1.default.getConfig(), accum = []) {
36
+ super(undefined, config, accum, {
37
+ MagicLinkToken: 0, Token: 1,
38
+ });
39
+ // @ts-expect-error abstract class
40
+ this.insertAt(new magicLink_1.MagicLinkToken(url, true, config, accum));
41
+ this.#space = space;
42
+ if (text) {
43
+ const inner = new index_2.Token(text, config, accum, {
44
+ 'Stage-7': ':', ConverterToken: ':',
45
+ });
46
+ inner.type = 'ext-link-text';
47
+ inner.setAttribute('stage', constants_1.MAX_STAGE - 1);
48
+ this.insertAt(inner);
49
+ }
50
+ /* NOT FOR BROWSER */
51
+ this.protectChildren(0);
52
+ }
53
+ /** @private */
54
+ toString() {
55
+ if (this.length === 1) {
56
+ return `[${super.toString()}${this.#space}]`;
57
+ }
58
+ /* NOT FOR BROWSER */
59
+ this.#correct();
60
+ (0, string_1.normalizeSpace)(this.lastChild);
61
+ /* NOT FOR BROWSER END */
62
+ return `[${super.toString(this.#space)}]`;
63
+ }
64
+ /** @override */
65
+ text() {
66
+ /* NOT FOR BROWSER */
67
+ (0, string_1.normalizeSpace)(this.childNodes[1]);
68
+ /* NOT FOR BROWSER END */
69
+ return `[${super.text(' ')}]`;
70
+ }
71
+ /** @private */
72
+ getAttribute(key) {
73
+ return key === 'padding' ? 1 : super.getAttribute(key);
74
+ }
75
+ /** @private */
76
+ getGaps() {
77
+ /* NOT FOR BROWSER */
78
+ this.#correct();
79
+ /* NOT FOR BROWSER END */
80
+ return this.#space.length;
81
+ }
82
+ /** @override */
83
+ lint(start = this.getAbsoluteIndex()) {
84
+ const errors = super.lint(start);
85
+ if (this.length === 1 && this.closest('heading-title')) {
86
+ errors.push((0, lint_1.generateForSelf)(this, { start }, 'variable anchor in a section header'));
87
+ }
88
+ return errors;
89
+ }
90
+ /** @override */
91
+ print() {
92
+ return super.print(this.length === 1 ? { pre: '[', post: `${this.#space}]` } : { pre: '[', sep: this.#space, post: ']' });
93
+ }
94
+ /* NOT FOR BROWSER */
95
+ /** @override */
96
+ cloneNode() {
97
+ const [url, text] = this.cloneChildNodes();
98
+ return debug_1.Shadow.run(() => {
99
+ // @ts-expect-error abstract class
100
+ const token = new ExtLinkToken(undefined, '', '', this.getAttribute('config'));
101
+ token.firstChild.safeReplaceWith(url);
102
+ if (text) {
103
+ token.insertAt(text);
104
+ }
105
+ return token;
106
+ });
107
+ }
108
+ /** 修正空白字符 */
109
+ #correct() {
110
+ if (!this.#space
111
+ && this.length > 1
112
+ // 都替换成`<`肯定不对,但无妨
113
+ && /^[^[\]<>"{\0-\x1F\x7F\p{Zs}\uFFFD]/u.test(this.lastChild.text().replace(/&[lg]t;/u, '<'))) {
114
+ this.#space = ' ';
115
+ }
116
+ }
117
+ /**
118
+ * 设置链接显示文字
119
+ * @param str 链接显示文字
120
+ */
121
+ setLinkText(str) {
122
+ const root = index_1.default.parse(str, this.getAttribute('include'), 7, this.getAttribute('config'));
123
+ if (this.length === 1) {
124
+ root.type = 'ext-link-text';
125
+ root.setAttribute('acceptable', {
126
+ 'Stage-7': ':', ConverterToken: ':',
127
+ });
128
+ this.insertAt(root);
129
+ }
130
+ else {
131
+ this.lastChild.replaceChildren(...root.childNodes);
132
+ }
133
+ this.#space ||= ' ';
134
+ }
135
+ }
136
+ exports.ExtLinkToken = ExtLinkToken;
137
+ constants_1.classes['ExtLinkToken'] = __filename;