wikiparser-node 1.2.0-b → 1.2.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 (154) hide show
  1. package/config/.schema.json +172 -0
  2. package/config/llwiki.json +35 -1
  3. package/config/moegirl.json +44 -1
  4. package/config/zhwiki.json +466 -1
  5. package/dist/addon/table.d.ts +6 -0
  6. package/dist/addon/table.js +564 -0
  7. package/dist/base.d.ts +46 -0
  8. package/dist/bin/toc.js +18 -0
  9. package/dist/index.d.ts +31 -0
  10. package/dist/index.js +209 -0
  11. package/dist/internal.d.ts +44 -0
  12. package/dist/lib/element.d.ts +155 -0
  13. package/dist/lib/element.js +654 -0
  14. package/dist/lib/node.d.ts +146 -0
  15. package/dist/lib/node.js +419 -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 +117 -0
  20. package/dist/lib/text.d.ts +62 -0
  21. package/dist/lib/text.js +235 -0
  22. package/dist/lib/title.d.ts +38 -0
  23. package/dist/lib/title.js +162 -0
  24. package/dist/mixin/attributesParent.js +94 -0
  25. package/dist/mixin/fixed.js +32 -0
  26. package/dist/mixin/flagsParent.js +70 -0
  27. package/dist/mixin/hidden.js +22 -0
  28. package/dist/mixin/magicLinkParent.js +41 -0
  29. package/dist/mixin/singleLine.js +25 -0
  30. package/dist/mixin/sol.js +43 -0
  31. package/dist/mixin/syntax.js +56 -0
  32. package/dist/parser/braces.js +123 -0
  33. package/dist/parser/commentAndExt.js +69 -0
  34. package/dist/parser/converter.js +40 -0
  35. package/dist/parser/externalLinks.js +28 -0
  36. package/dist/parser/hrAndDoubleUnderscore.js +38 -0
  37. package/dist/parser/html.js +36 -0
  38. package/dist/parser/links.js +94 -0
  39. package/dist/parser/list.js +59 -0
  40. package/dist/parser/magicLinks.js +40 -0
  41. package/dist/parser/quotes.js +67 -0
  42. package/dist/parser/selector.js +162 -0
  43. package/dist/parser/table.js +112 -0
  44. package/dist/src/arg.d.ts +58 -0
  45. package/dist/src/arg.js +190 -0
  46. package/dist/src/atom.d.ts +12 -0
  47. package/dist/src/atom.js +27 -0
  48. package/dist/src/attribute.d.ts +86 -0
  49. package/dist/src/attribute.js +392 -0
  50. package/dist/src/attributes.d.ts +107 -0
  51. package/dist/src/attributes.js +344 -0
  52. package/dist/src/converter.d.ts +51 -0
  53. package/dist/src/converter.js +79 -0
  54. package/dist/src/converterFlags.d.ts +87 -0
  55. package/dist/src/converterFlags.js +223 -0
  56. package/dist/src/converterRule.d.ts +81 -0
  57. package/dist/src/converterRule.js +212 -0
  58. package/dist/src/extLink.d.ts +53 -0
  59. package/dist/src/extLink.js +121 -0
  60. package/dist/src/gallery.d.ts +55 -0
  61. package/dist/src/gallery.js +123 -0
  62. package/dist/src/heading.d.ts +72 -0
  63. package/dist/src/heading.js +128 -0
  64. package/dist/src/hidden.d.ts +21 -0
  65. package/dist/src/hidden.js +23 -0
  66. package/dist/src/html.d.ts +103 -0
  67. package/dist/src/html.js +230 -0
  68. package/dist/src/imageParameter.d.ts +66 -0
  69. package/dist/src/imageParameter.js +247 -0
  70. package/dist/src/imagemap.d.ts +57 -0
  71. package/dist/src/imagemap.js +148 -0
  72. package/dist/src/imagemapLink.d.ts +57 -0
  73. package/dist/src/imagemapLink.js +40 -0
  74. package/dist/src/index.d.ts +146 -0
  75. package/dist/src/index.js +777 -0
  76. package/dist/src/link/base.d.ts +52 -0
  77. package/dist/src/link/base.js +207 -0
  78. package/dist/src/link/category.d.ts +13 -0
  79. package/dist/src/link/category.js +29 -0
  80. package/dist/src/link/file.d.ts +96 -0
  81. package/dist/src/link/file.js +253 -0
  82. package/dist/src/link/galleryImage.d.ts +43 -0
  83. package/dist/src/link/galleryImage.js +106 -0
  84. package/dist/src/link/index.d.ts +53 -0
  85. package/dist/src/link/index.js +123 -0
  86. package/dist/src/magicLink.d.ts +69 -0
  87. package/dist/src/magicLink.js +148 -0
  88. package/dist/src/nested.d.ts +44 -0
  89. package/dist/src/nested.js +87 -0
  90. package/dist/src/nowiki/base.d.ts +46 -0
  91. package/dist/src/nowiki/base.js +42 -0
  92. package/dist/src/nowiki/comment.d.ts +39 -0
  93. package/dist/src/nowiki/comment.js +67 -0
  94. package/dist/src/nowiki/dd.d.ts +8 -0
  95. package/dist/src/nowiki/dd.js +25 -0
  96. package/dist/src/nowiki/doubleUnderscore.d.ts +43 -0
  97. package/dist/src/nowiki/doubleUnderscore.js +50 -0
  98. package/dist/src/nowiki/hr.d.ts +29 -0
  99. package/dist/src/nowiki/hr.js +14 -0
  100. package/dist/src/nowiki/index.d.ts +16 -0
  101. package/dist/src/nowiki/index.js +21 -0
  102. package/dist/src/nowiki/list.d.ts +29 -0
  103. package/dist/src/nowiki/list.js +48 -0
  104. package/dist/src/nowiki/listBase.d.ts +18 -0
  105. package/dist/src/nowiki/listBase.js +12 -0
  106. package/dist/src/nowiki/noinclude.d.ts +25 -0
  107. package/dist/src/nowiki/noinclude.js +25 -0
  108. package/dist/src/nowiki/quote.d.ts +22 -0
  109. package/dist/src/nowiki/quote.js +50 -0
  110. package/dist/src/onlyinclude.d.ts +16 -0
  111. package/dist/src/onlyinclude.js +57 -0
  112. package/dist/src/paramTag/index.d.ts +37 -0
  113. package/dist/src/paramTag/index.js +68 -0
  114. package/dist/src/paramTag/inputbox.d.ts +8 -0
  115. package/dist/src/paramTag/inputbox.js +23 -0
  116. package/dist/src/parameter.d.ts +84 -0
  117. package/dist/src/parameter.js +206 -0
  118. package/dist/src/pre.d.ts +28 -0
  119. package/dist/src/pre.js +51 -0
  120. package/dist/src/syntax.d.ts +24 -0
  121. package/dist/src/syntax.js +30 -0
  122. package/dist/src/table/base.d.ts +52 -0
  123. package/dist/src/table/base.js +78 -0
  124. package/dist/src/table/index.d.ts +229 -0
  125. package/dist/src/table/index.js +380 -0
  126. package/dist/src/table/td.d.ts +103 -0
  127. package/dist/src/table/td.js +261 -0
  128. package/dist/src/table/tr.d.ts +32 -0
  129. package/dist/src/table/tr.js +56 -0
  130. package/dist/src/table/trBase.d.ts +54 -0
  131. package/dist/src/table/trBase.js +159 -0
  132. package/dist/src/tagPair/ext.d.ts +58 -0
  133. package/dist/src/tagPair/ext.js +142 -0
  134. package/dist/src/tagPair/include.d.ts +52 -0
  135. package/dist/src/tagPair/include.js +58 -0
  136. package/dist/src/tagPair/index.d.ts +47 -0
  137. package/dist/src/tagPair/index.js +93 -0
  138. package/dist/src/transclude.d.ts +167 -0
  139. package/dist/src/transclude.js +689 -0
  140. package/dist/util/constants.js +108 -0
  141. package/dist/util/debug.js +64 -0
  142. package/dist/util/diff.js +72 -0
  143. package/dist/util/lint.js +48 -0
  144. package/dist/util/string.js +61 -0
  145. package/errors/README +1 -0
  146. package/package.json +12 -32
  147. package/printed/README +1 -0
  148. package/bundle/bundle.min.js +0 -36
  149. package/extensions/dist/base.js +0 -68
  150. package/extensions/dist/editor.js +0 -159
  151. package/extensions/dist/highlight.js +0 -30
  152. package/extensions/dist/lint.js +0 -48
  153. package/extensions/editor.css +0 -63
  154. package/extensions/ui.css +0 -117
@@ -0,0 +1,223 @@
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 Parser = require("../index");
8
+ const index_1 = 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_1.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
+ /** @param flags 转换类型标记 */
30
+ constructor(flags, config = Parser.getConfig(), accum = []) {
31
+ super(undefined, config, accum, {
32
+ AtomToken: ':',
33
+ });
34
+ this.append(...flags.map(flag => new atom_1.AtomToken(flag, 'converter-flag', config, accum)));
35
+ }
36
+ /** @private */
37
+ afterBuild() {
38
+ this.#flags = this.childNodes.map(child => child.text().trim());
39
+ const /** @implements */ converterFlagsListener = ({ prevTarget }) => {
40
+ if (prevTarget) {
41
+ this.#flags[this.childNodes.indexOf(prevTarget)] = prevTarget.text().trim();
42
+ }
43
+ };
44
+ this.addEventListener(['remove', 'insert', 'text', 'replace'], converterFlagsListener);
45
+ }
46
+ /** @private */
47
+ toString(omit) {
48
+ return super.toString(omit, ';');
49
+ }
50
+ /** @override */
51
+ text() {
52
+ return super.text(';');
53
+ }
54
+ /** @private */
55
+ getGaps() {
56
+ return 1;
57
+ }
58
+ /** 获取未知的转换类型标记 */
59
+ getUnknownFlags() {
60
+ return new Set(this.#flags.filter(flag => /\{{3}[^{}]+\}{3}/u.test(flag)));
61
+ }
62
+ /** 获取指定语言变体的转换标记 */
63
+ getVariantFlags() {
64
+ const variants = new Set(this.getAttribute('config').variants);
65
+ return new Set(this.#flags.filter(flag => variants.has(flag)));
66
+ }
67
+ /** @override */
68
+ lint(start = this.getAbsoluteIndex()) {
69
+ const variantFlags = this.getVariantFlags(), unknownFlags = this.getUnknownFlags(), validFlags = new Set(this.#flags.filter(flag => definedFlags.has(flag))), { length: emptyFlagCount } = this.#flags.filter(flag => !flag), knownFlagCount = this.#flags.length - unknownFlags.size - emptyFlagCount, errors = super.lint(start);
70
+ if (variantFlags.size === knownFlagCount || validFlags.size === knownFlagCount) {
71
+ return errors;
72
+ }
73
+ const rect = { start, ...this.getRootNode().posFromIndex(start) }, { childNodes, length } = this;
74
+ for (let i = 0; i < length; i++) {
75
+ const child = childNodes[i], flag = child.text().trim();
76
+ if (flag && !variantFlags.has(flag) && !unknownFlags.has(flag)
77
+ && (variantFlags.size > 0 || !validFlags.has(flag))) {
78
+ const error = (0, lint_1.generateForChild)(child, rect, 'invalid conversion flag');
79
+ errors.push({ ...error, excerpt: childNodes.slice(0, i + 1).map(String).join(';').slice(-50) });
80
+ }
81
+ }
82
+ return errors;
83
+ }
84
+ /** @override */
85
+ print() {
86
+ return super.print({ sep: ';' });
87
+ }
88
+ /* NOT FOR BROWSER */
89
+ /** @override */
90
+ cloneNode() {
91
+ const cloned = this.cloneChildNodes();
92
+ return debug_1.Shadow.run(() => {
93
+ const token = new ConverterFlagsToken([], this.getAttribute('config'));
94
+ token.append(...cloned);
95
+ token.afterBuild();
96
+ return token;
97
+ });
98
+ }
99
+ /** @private */
100
+ getAttribute(key) {
101
+ return key === 'flags' ? this.#flags : super.getAttribute(key);
102
+ }
103
+ /**
104
+ * @override
105
+ * @param i 移除位置
106
+ */
107
+ removeAt(i) {
108
+ const token = super.removeAt(i);
109
+ this.#flags?.splice(i, 1);
110
+ return token;
111
+ }
112
+ /**
113
+ * @override
114
+ * @param token 待插入的子节点
115
+ * @param i 插入位置
116
+ */
117
+ insertAt(token, i = this.length) {
118
+ super.insertAt(token, i);
119
+ this.#flags?.splice(i, 0, token.text().trim());
120
+ return token;
121
+ }
122
+ /** 获取所有转换类型标记 */
123
+ getAllFlags() {
124
+ return new Set(this.#flags);
125
+ }
126
+ /**
127
+ * 获取转换类型标记节点
128
+ * @param flag 转换类型标记
129
+ */
130
+ getFlagTokens(flag) {
131
+ return this.#flags.includes(flag) ? this.childNodes.filter(child => child.text().trim() === flag) : [];
132
+ }
133
+ /** 获取有效的转换类型标记 */
134
+ getEffectiveFlags() {
135
+ const variantFlags = this.getVariantFlags(), unknownFlags = this.getUnknownFlags();
136
+ if (variantFlags.size > 0) {
137
+ return new Set([...variantFlags, ...unknownFlags]);
138
+ }
139
+ const flags = new Set([...this.#flags.filter(flag => definedFlags.has(flag)), ...unknownFlags]);
140
+ if (flags.size === 0) {
141
+ return new Set('S');
142
+ }
143
+ else if (flags.has('R')) {
144
+ return new Set('R');
145
+ }
146
+ else if (flags.has('N')) {
147
+ return new Set('N');
148
+ }
149
+ else if (flags.has('-')) {
150
+ return new Set('-');
151
+ }
152
+ else if (flags.has('H')) {
153
+ const hasT = flags.has('T'), hasD = flags.has('D');
154
+ return hasT && hasD
155
+ ? new Set(['+', 'H', 'T', 'D'])
156
+ : new Set(['+', 'H', ...hasT ? ['T'] : [], ...hasD ? ['D'] : [], ...unknownFlags]);
157
+ }
158
+ if (flags.size === 1 && flags.has('T')) {
159
+ flags.add('H');
160
+ }
161
+ if (flags.has('A')) {
162
+ flags.add('+');
163
+ flags.add('S');
164
+ }
165
+ if (flags.has('D')) {
166
+ flags.delete('S');
167
+ }
168
+ return flags;
169
+ }
170
+ /**
171
+ * 是否具有某转换类型标记
172
+ * @param flag 转换类型标记
173
+ */
174
+ hasFlag(flag) {
175
+ return this.#flags.includes(flag);
176
+ }
177
+ /**
178
+ * 是否具有某有效的转换类型标记
179
+ * @param flag 转换类型标记
180
+ */
181
+ hasEffectiveFlag(flag) {
182
+ return this.getEffectiveFlags().has(flag);
183
+ }
184
+ /**
185
+ * 移除某转换类型标记
186
+ * @param flag 转换类型标记
187
+ */
188
+ removeFlag(flag) {
189
+ for (const token of this.getFlagTokens(flag)) {
190
+ token.remove();
191
+ }
192
+ }
193
+ /**
194
+ * 添加转换类型标记
195
+ * @param flag 转换类型标记
196
+ */
197
+ #newFlag(flag) {
198
+ this.insertAt(debug_1.Shadow.run(() => new atom_1.AtomToken(flag, 'converter-flag', this.getAttribute('config'))));
199
+ }
200
+ /**
201
+ * 设置转换类型标记
202
+ * @param flag 转换类型标记
203
+ */
204
+ setFlag(flag) {
205
+ if (!this.#flags.includes(flag)) {
206
+ this.#newFlag(flag);
207
+ }
208
+ }
209
+ /**
210
+ * 开关转换类型标记
211
+ * @param flag 转换类型标记
212
+ */
213
+ toggleFlag(flag) {
214
+ if (this.#flags.includes(flag)) {
215
+ this.removeFlag(flag);
216
+ }
217
+ else {
218
+ this.#newFlag(flag);
219
+ }
220
+ }
221
+ }
222
+ exports.ConverterFlagsToken = ConverterFlagsToken;
223
+ constants_1.classes['ConverterFlagsToken'] = __filename;
@@ -0,0 +1,81 @@
1
+ import * as 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 class ConverterRuleToken extends Token {
10
+ readonly type = "converter-rule";
11
+ readonly childNodes: [AtomToken] | [AtomToken, AtomToken] | [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
+ * @throws `Error` 至少保留1个子节点
49
+ */
50
+ removeAt(i: number): AtomToken;
51
+ /**
52
+ * @override
53
+ * @throws `Error` 请勿手动插入子节点
54
+ */
55
+ insertAt(): never;
56
+ /** 修改为不转换 */
57
+ noConvert(): void;
58
+ /**
59
+ * 设置转换目标
60
+ * @param to 转换目标
61
+ */
62
+ setTo(to: string): void;
63
+ /**
64
+ * 设置语言变体
65
+ * @param variant 语言变体
66
+ */
67
+ setVariant(variant: string): void;
68
+ /**
69
+ * 设置转换原文
70
+ * @param from 转换原文
71
+ * @throws `Error` 尚未指定语言变体
72
+ */
73
+ setFrom(from: string): void;
74
+ /**
75
+ * 修改为单向转换
76
+ * @param from 转换原文
77
+ */
78
+ makeUnidirectional(from: string): void;
79
+ /** 修改为双向转换 */
80
+ makeBidirectional(): void;
81
+ }
@@ -0,0 +1,212 @@
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 Parser = require("../index");
7
+ const index_1 = require("./index");
8
+ const atom_1 = require("./atom");
9
+ /**
10
+ * 转换规则
11
+ * @classdesc `{childNodes: ...AtomToken}`
12
+ */
13
+ class ConverterRuleToken extends index_1.Token {
14
+ type = 'converter-rule';
15
+ /* NOT FOR BROWSER */
16
+ /** 语言变体 */
17
+ get variant() {
18
+ return this.childNodes.at(-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 = Parser.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
+ this.protectChildren('1:');
76
+ }
77
+ /** @private */
78
+ toString(omit) {
79
+ const { childNodes } = this;
80
+ if (childNodes.length === 3 && !(omit && this.matchesTypes(omit))) {
81
+ const [from, variant, to] = childNodes;
82
+ return `${from.toString(omit)}=>${variant.toString(omit)}:${to.toString(omit)}`;
83
+ }
84
+ return super.toString(omit, ':');
85
+ }
86
+ /** @override */
87
+ text() {
88
+ const { childNodes } = this;
89
+ if (childNodes.length === 3) {
90
+ const [from, variant, to] = childNodes;
91
+ return `${from.text()}=>${variant.text()}:${to.text()}`;
92
+ }
93
+ return super.text(':');
94
+ }
95
+ /** @private */
96
+ getGaps(i) {
97
+ return i === 0 && this.length === 3 ? 2 : 1;
98
+ }
99
+ /** @override */
100
+ print() {
101
+ const { childNodes } = this;
102
+ if (childNodes.length === 3) {
103
+ const [from, variant, to] = childNodes;
104
+ return `<span class="wpb-converter-rule">${from.print()}=>${variant.print()}:${to.print()}</span>`;
105
+ }
106
+ return super.print({ sep: ':' });
107
+ }
108
+ /* NOT FOR BROWSER */
109
+ /** @override */
110
+ cloneNode() {
111
+ const cloned = this.cloneChildNodes(), placeholders = ['', 'zh:', '=>zh:'], placeholder = placeholders[cloned.length - 1];
112
+ return debug_1.Shadow.run(() => {
113
+ const token = new ConverterRuleToken(placeholder, Boolean(placeholder), this.getAttribute('config'));
114
+ for (let i = 0; i < cloned.length; i++) {
115
+ token.childNodes[i].safeReplaceWith(cloned[i]);
116
+ }
117
+ token.afterBuild();
118
+ return token;
119
+ });
120
+ }
121
+ /** @private */
122
+ afterBuild() {
123
+ const /** @implements */ converterRuleListener = (e, data) => {
124
+ const { prevTarget } = e;
125
+ if (this.length > 1 && this.childNodes.at(-2) === prevTarget) {
126
+ const { variant } = this;
127
+ if (!this.getAttribute('config').variants.includes(variant)) {
128
+ (0, debug_1.undo)(e, data);
129
+ throw new Error(`无效的语言变体:${variant}`);
130
+ }
131
+ }
132
+ };
133
+ this.addEventListener(['remove', 'insert', 'text', 'replace'], converterRuleListener);
134
+ }
135
+ /**
136
+ * @override
137
+ * @param i 移除位置
138
+ * @throws `Error` 至少保留1个子节点
139
+ */
140
+ removeAt(i) {
141
+ if (this.length === 1) {
142
+ throw new Error(`${this.constructor.name} 需至少保留 1 个子节点!`);
143
+ }
144
+ return super.removeAt(i);
145
+ }
146
+ /**
147
+ * @override
148
+ * @throws `Error` 请勿手动插入子节点
149
+ */
150
+ insertAt() {
151
+ throw new Error(`转换规则语法复杂,请勿尝试对 ${this.constructor.name} 手动插入子节点!`);
152
+ }
153
+ /** 修改为不转换 */
154
+ noConvert() {
155
+ const { length } = this;
156
+ for (let i = 0; i < length - 1; i++) { // ConverterRuleToken只能从前往后删除子节点
157
+ this.removeAt(0);
158
+ }
159
+ }
160
+ /**
161
+ * 设置转换目标
162
+ * @param to 转换目标
163
+ */
164
+ setTo(to) {
165
+ const { childNodes } = Parser.parse(to, this.getAttribute('include'), undefined, this.getAttribute('config'));
166
+ this.lastChild.replaceChildren(...childNodes);
167
+ }
168
+ /**
169
+ * 设置语言变体
170
+ * @param variant 语言变体
171
+ */
172
+ setVariant(variant) {
173
+ const config = this.getAttribute('config');
174
+ if (this.length === 1) {
175
+ super.insertAt(debug_1.Shadow.run(() => new atom_1.AtomToken(variant, 'converter-rule-variant', config)), 0);
176
+ }
177
+ else {
178
+ this.childNodes.at(-2).setText(variant);
179
+ }
180
+ }
181
+ /**
182
+ * 设置转换原文
183
+ * @param from 转换原文
184
+ * @throws `Error` 尚未指定语言变体
185
+ */
186
+ setFrom(from) {
187
+ const { variant, unidirectional } = this;
188
+ if (!variant) {
189
+ throw new Error('请先指定语言变体!');
190
+ }
191
+ const config = this.getAttribute('config'), { childNodes } = Parser.parse(from, this.getAttribute('include'), undefined, config);
192
+ if (!unidirectional) {
193
+ super.insertAt(debug_1.Shadow.run(() => new atom_1.AtomToken(undefined, 'converter-rule-from', config)), 0);
194
+ }
195
+ this.firstChild.replaceChildren(...childNodes);
196
+ }
197
+ /**
198
+ * 修改为单向转换
199
+ * @param from 转换原文
200
+ */
201
+ makeUnidirectional(from) {
202
+ this.setFrom(from);
203
+ }
204
+ /** 修改为双向转换 */
205
+ makeBidirectional() {
206
+ if (this.unidirectional) {
207
+ this.removeAt(0);
208
+ }
209
+ }
210
+ }
211
+ exports.ConverterRuleToken = ConverterRuleToken;
212
+ constants_1.classes['ConverterRuleToken'] = __filename;
@@ -0,0 +1,53 @@
1
+ import * as Parser from '../index';
2
+ import { Token } from './index';
3
+ import { MagicLinkToken } from './magicLink';
4
+ declare const ExtLinkToken_base: (abstract new (...args: any[]) => {
5
+ readonly firstChild: Parser.MagicLinkToken;
6
+ readonly firstElementChild: Parser.MagicLinkToken;
7
+ protocol: string | undefined;
8
+ link: string;
9
+ getUrl(): URL;
10
+ setTarget(url: string): void;
11
+ readonly length: number;
12
+ toString(omit?: Set<string> | undefined, separator?: string | undefined): string;
13
+ text(separator?: string | undefined): string;
14
+ insertAt(token: unknown, i?: number | undefined): unknown;
15
+ afterBuild(): void;
16
+ getAttribute<T extends string>(key: T): TokenAttributeGetter<T>;
17
+ setAttribute<T_1 extends string>(key: T_1, value: TokenAttributeSetter<T_1>): void;
18
+ addEventListener(events: string | string[], listener: AstListener): void;
19
+ replaceChildren(...elements: (string | Parser.AstNodes)[]): void;
20
+ }) & typeof Parser.Token;
21
+ /**
22
+ * 外链
23
+ * @classdesc `{childNodes: [MagicLinkToken, ?Token]}`
24
+ */
25
+ export declare class ExtLinkToken extends ExtLinkToken_base {
26
+ #private;
27
+ readonly type = "ext-link";
28
+ readonly childNodes: [MagicLinkToken] | [MagicLinkToken, Token];
29
+ abstract get children(): [MagicLinkToken] | [MagicLinkToken, Token];
30
+ abstract get lastChild(): Token;
31
+ abstract get lastElementChild(): Token;
32
+ /** 链接显示文字 */
33
+ get innerText(): string;
34
+ set innerText(text: string);
35
+ /**
36
+ * @param url 网址
37
+ * @param space 空白字符
38
+ * @param text 链接文字
39
+ */
40
+ constructor(url?: string, space?: string, text?: string, config?: Parser.Config, accum?: Token[]);
41
+ /** @override */
42
+ text(): string;
43
+ /** @override */
44
+ print(): string;
45
+ /** @override */
46
+ cloneNode(): this;
47
+ /**
48
+ * 设置链接显示文字
49
+ * @param str 链接显示文字
50
+ */
51
+ setLinkText(str: string): void;
52
+ }
53
+ export {};
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExtLinkToken = void 0;
4
+ const string_1 = require("../util/string");
5
+ const debug_1 = require("../util/debug");
6
+ const constants_1 = require("../util/constants");
7
+ const magicLinkParent_1 = require("../mixin/magicLinkParent");
8
+ const Parser = require("../index");
9
+ const index_1 = require("./index");
10
+ const magicLink_1 = require("./magicLink");
11
+ /**
12
+ * 外链
13
+ * @classdesc `{childNodes: [MagicLinkToken, ?Token]}`
14
+ */
15
+ class ExtLinkToken extends (0, magicLinkParent_1.magicLinkParent)(index_1.Token) {
16
+ type = 'ext-link';
17
+ #space;
18
+ /* NOT FOR BROWSER */
19
+ /** 链接显示文字 */
20
+ get innerText() {
21
+ return this.length > 1
22
+ ? this.lastChild.text()
23
+ : `[${this.getRootNode().querySelectorAll('ext-link[childElementCount=1]').indexOf(this) + 1}]`;
24
+ }
25
+ set innerText(text) {
26
+ this.setLinkText(text);
27
+ }
28
+ /* NOT FOR BROWSER END */
29
+ /**
30
+ * @param url 网址
31
+ * @param space 空白字符
32
+ * @param text 链接文字
33
+ */
34
+ constructor(url, space = '', text = '', config = Parser.getConfig(), accum = []) {
35
+ super(undefined, config, accum, {
36
+ MagicLinkToken: 0, Token: 1,
37
+ });
38
+ this.insertAt(new magicLink_1.MagicLinkToken(url, true, config, accum));
39
+ this.#space = space;
40
+ if (text) {
41
+ const inner = new index_1.Token(text, config, accum, {
42
+ 'Stage-7': ':', ConverterToken: ':',
43
+ });
44
+ inner.type = 'ext-link-text';
45
+ inner.setAttribute('stage', constants_1.MAX_STAGE - 1);
46
+ this.insertAt(inner);
47
+ }
48
+ this.protectChildren(0);
49
+ }
50
+ /** @private */
51
+ toString(omit) {
52
+ if (omit && this.matchesTypes(omit)) {
53
+ return '';
54
+ }
55
+ if (this.length === 1) {
56
+ return `[${super.toString(omit)}${this.#space}]`;
57
+ }
58
+ this.#correct();
59
+ (0, string_1.normalizeSpace)(this.lastChild);
60
+ return `[${super.toString(omit, this.#space)}]`;
61
+ }
62
+ /** @override */
63
+ text() {
64
+ (0, string_1.normalizeSpace)(this.childNodes[1]);
65
+ return `[${super.text(' ')}]`;
66
+ }
67
+ /** @private */
68
+ getAttribute(key) {
69
+ return key === 'padding' ? 1 : super.getAttribute(key);
70
+ }
71
+ /** @private */
72
+ getGaps() {
73
+ this.#correct();
74
+ return this.#space.length;
75
+ }
76
+ /** @override */
77
+ print() {
78
+ return super.print(this.length === 1 ? { pre: '[', post: `${this.#space}]` } : { pre: '[', sep: this.#space, post: ']' });
79
+ }
80
+ /* NOT FOR BROWSER */
81
+ /** @override */
82
+ cloneNode() {
83
+ const [url, text] = this.cloneChildNodes();
84
+ return debug_1.Shadow.run(() => {
85
+ const token = new ExtLinkToken(undefined, '', '', this.getAttribute('config'));
86
+ token.firstChild.safeReplaceWith(url);
87
+ if (text) {
88
+ token.insertAt(text);
89
+ }
90
+ return token;
91
+ });
92
+ }
93
+ /** 修正空白字符 */
94
+ #correct() {
95
+ if (!this.#space && this.length > 1
96
+ // 都替换成`<`肯定不对,但无妨
97
+ && /^[^[\]<>"{\0-\x1F\x7F\p{Zs}\uFFFD]/u.test(this.lastChild.text().replace(/&[lg]t;/u, '<'))) {
98
+ this.#space = ' ';
99
+ }
100
+ }
101
+ /**
102
+ * 设置链接显示文字
103
+ * @param str 链接显示文字
104
+ */
105
+ setLinkText(str) {
106
+ const root = Parser.parse(str, this.getAttribute('include'), 7, this.getAttribute('config'));
107
+ if (this.length === 1) {
108
+ root.type = 'ext-link-text';
109
+ root.setAttribute('acceptable', {
110
+ 'Stage-7': ':', ConverterToken: ':',
111
+ });
112
+ this.insertAt(root);
113
+ }
114
+ else {
115
+ this.lastChild.replaceChildren(...root.childNodes);
116
+ }
117
+ this.#space ||= ' ';
118
+ }
119
+ }
120
+ exports.ExtLinkToken = ExtLinkToken;
121
+ constants_1.classes['ExtLinkToken'] = __filename;