wikiparser-node 0.11.0 → 1.0.0-beta.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 (251) hide show
  1. package/dist/index.d.ts +59 -113
  2. package/dist/index.js +307 -0
  3. package/dist/lib/element.d.ts +122 -108
  4. package/dist/lib/element.js +656 -0
  5. package/dist/lib/node.d.ts +115 -221
  6. package/dist/lib/node.js +473 -0
  7. package/dist/lib/ranges.d.ts +27 -26
  8. package/dist/lib/ranges.js +130 -0
  9. package/dist/lib/text.d.ts +36 -28
  10. package/dist/lib/text.js +215 -0
  11. package/dist/lib/title.d.ts +25 -12
  12. package/dist/lib/title.js +100 -0
  13. package/dist/mixin/attributesParent.js +90 -0
  14. package/dist/mixin/fixed.js +29 -0
  15. package/dist/mixin/hidden.js +19 -0
  16. package/dist/mixin/singleLine.js +23 -0
  17. package/dist/mixin/sol.js +41 -0
  18. package/dist/parser/brackets.js +117 -0
  19. package/dist/parser/commentAndExt.js +48 -0
  20. package/dist/parser/converter.js +31 -0
  21. package/dist/parser/externalLinks.js +22 -0
  22. package/dist/parser/hrAndDoubleUnderscore.js +33 -0
  23. package/dist/parser/html.js +29 -0
  24. package/dist/parser/links.js +86 -0
  25. package/dist/parser/list.js +51 -0
  26. package/dist/parser/magicLinks.js +32 -0
  27. package/dist/parser/quotes.js +57 -0
  28. package/dist/parser/selector.js +158 -0
  29. package/dist/parser/table.js +108 -0
  30. package/dist/src/arg.d.ts +47 -23
  31. package/dist/src/arg.js +196 -0
  32. package/dist/src/atom.d.ts +12 -0
  33. package/dist/src/atom.js +22 -0
  34. package/dist/src/attribute.d.ts +74 -33
  35. package/dist/src/attribute.js +433 -0
  36. package/dist/src/attributes.d.ts +61 -55
  37. package/dist/src/attributes.js +371 -0
  38. package/dist/src/converter.d.ts +45 -71
  39. package/dist/src/converter.js +135 -0
  40. package/dist/src/converterFlags.d.ts +64 -39
  41. package/dist/src/converterFlags.js +235 -0
  42. package/dist/src/converterRule.d.ts +49 -27
  43. package/dist/src/converterRule.js +255 -0
  44. package/dist/src/extLink.d.ts +41 -38
  45. package/dist/src/extLink.js +154 -0
  46. package/dist/src/gallery.d.ts +59 -18
  47. package/dist/src/gallery.js +132 -0
  48. package/dist/src/heading.d.ts +60 -22
  49. package/dist/src/heading.js +135 -0
  50. package/dist/src/hidden.d.ts +20 -0
  51. package/dist/src/hidden.js +24 -0
  52. package/dist/src/html.d.ts +83 -29
  53. package/dist/src/html.js +242 -0
  54. package/dist/src/imageParameter.d.ts +59 -40
  55. package/dist/src/imageParameter.js +251 -0
  56. package/dist/src/imagemap.d.ts +65 -21
  57. package/dist/src/imagemap.js +164 -0
  58. package/dist/src/imagemapLink.d.ts +46 -14
  59. package/dist/src/imagemapLink.js +38 -0
  60. package/dist/src/index.d.ts +71 -105
  61. package/dist/src/index.js +826 -0
  62. package/dist/src/link/base.d.ts +71 -0
  63. package/dist/src/link/base.js +225 -0
  64. package/dist/src/link/category.d.ts +10 -11
  65. package/dist/src/link/category.js +26 -0
  66. package/dist/src/link/file.d.ts +61 -39
  67. package/dist/src/link/file.js +242 -0
  68. package/dist/src/link/galleryImage.d.ts +34 -12
  69. package/dist/src/link/galleryImage.js +98 -0
  70. package/dist/src/link/index.d.ts +25 -63
  71. package/dist/src/link/index.js +136 -0
  72. package/dist/src/magicLink.d.ts +22 -15
  73. package/dist/src/magicLink.js +126 -0
  74. package/dist/src/nested.d.ts +47 -0
  75. package/dist/src/nested.js +84 -0
  76. package/dist/src/nowiki/base.d.ts +39 -0
  77. package/dist/src/nowiki/base.js +29 -0
  78. package/dist/src/nowiki/comment.d.ts +31 -20
  79. package/dist/src/nowiki/comment.js +61 -0
  80. package/dist/src/nowiki/dd.d.ts +17 -11
  81. package/dist/src/nowiki/dd.js +50 -0
  82. package/dist/src/nowiki/doubleUnderscore.d.ts +28 -13
  83. package/dist/src/nowiki/doubleUnderscore.js +45 -0
  84. package/dist/src/nowiki/hr.d.ts +28 -10
  85. package/dist/src/nowiki/hr.js +33 -0
  86. package/dist/src/nowiki/index.d.ts +17 -23
  87. package/dist/src/nowiki/index.js +21 -0
  88. package/dist/src/nowiki/list.d.ts +15 -7
  89. package/dist/src/nowiki/list.js +11 -0
  90. package/dist/src/nowiki/noinclude.d.ts +20 -7
  91. package/dist/src/nowiki/noinclude.js +22 -0
  92. package/dist/src/nowiki/quote.d.ts +25 -10
  93. package/dist/src/nowiki/quote.js +55 -0
  94. package/dist/src/onlyinclude.d.ts +28 -12
  95. package/dist/src/onlyinclude.js +64 -0
  96. package/dist/src/paramTag/index.d.ts +40 -17
  97. package/dist/src/paramTag/index.js +76 -0
  98. package/dist/src/paramTag/inputbox.d.ts +8 -7
  99. package/dist/src/paramTag/inputbox.js +19 -0
  100. package/dist/src/parameter.d.ts +62 -41
  101. package/dist/src/parameter.js +201 -0
  102. package/dist/src/pre.d.ts +32 -0
  103. package/dist/src/pre.js +39 -0
  104. package/dist/src/syntax.d.ts +17 -14
  105. package/dist/src/syntax.js +65 -0
  106. package/dist/src/table/base.d.ts +55 -0
  107. package/dist/src/table/base.js +77 -0
  108. package/dist/src/table/index.d.ts +123 -156
  109. package/dist/src/table/index.js +811 -0
  110. package/dist/src/table/td.d.ts +90 -67
  111. package/dist/src/table/td.js +276 -0
  112. package/dist/src/table/tr.d.ts +30 -85
  113. package/dist/src/table/tr.js +48 -0
  114. package/dist/src/table/trBase.d.ts +72 -0
  115. package/dist/src/table/trBase.js +153 -0
  116. package/dist/src/tagPair/ext.d.ts +47 -11
  117. package/dist/src/tagPair/ext.js +127 -0
  118. package/dist/src/tagPair/include.d.ts +32 -13
  119. package/dist/src/tagPair/include.js +40 -0
  120. package/dist/src/tagPair/index.d.ts +44 -29
  121. package/dist/src/tagPair/index.js +111 -0
  122. package/dist/src/transclude.d.ts +88 -85
  123. package/dist/src/transclude.js +739 -0
  124. package/dist/util/base.js +26 -0
  125. package/dist/util/debug.js +52 -0
  126. package/dist/util/diff.js +69 -0
  127. package/dist/util/lint.js +38 -0
  128. package/dist/util/string.js +103 -0
  129. package/errors/README +1 -0
  130. package/i18n/zh-hans.json +1 -0
  131. package/i18n/zh-hant.json +1 -0
  132. package/package.json +11 -14
  133. package/printed/README +1 -0
  134. package/dist/mixin/attributeParent.d.ts +0 -9
  135. package/dist/mixin/fixedToken.d.ts +0 -8
  136. package/dist/mixin/hidden.d.ts +0 -8
  137. package/dist/mixin/singleLine.d.ts +0 -8
  138. package/dist/mixin/sol.d.ts +0 -8
  139. package/dist/parser/brackets.d.ts +0 -12
  140. package/dist/parser/commentAndExt.d.ts +0 -8
  141. package/dist/parser/converter.d.ts +0 -7
  142. package/dist/parser/externalLinks.d.ts +0 -7
  143. package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
  144. package/dist/parser/html.d.ts +0 -7
  145. package/dist/parser/links.d.ts +0 -7
  146. package/dist/parser/list.d.ts +0 -7
  147. package/dist/parser/magicLinks.d.ts +0 -7
  148. package/dist/parser/quotes.d.ts +0 -7
  149. package/dist/parser/selector.d.ts +0 -12
  150. package/dist/parser/table.d.ts +0 -11
  151. package/dist/src/atom/hidden.d.ts +0 -5
  152. package/dist/src/atom/index.d.ts +0 -15
  153. package/dist/src/charinsert.d.ts +0 -32
  154. package/dist/src/hasNowiki/index.d.ts +0 -14
  155. package/dist/src/hasNowiki/pre.d.ts +0 -13
  156. package/dist/src/nested/choose.d.ts +0 -13
  157. package/dist/src/nested/combobox.d.ts +0 -13
  158. package/dist/src/nested/index.d.ts +0 -18
  159. package/dist/src/nested/references.d.ts +0 -13
  160. package/dist/tool/index.d.ts +0 -420
  161. package/dist/util/base.d.ts +0 -10
  162. package/dist/util/debug.d.ts +0 -20
  163. package/dist/util/diff.d.ts +0 -8
  164. package/dist/util/lint.d.ts +0 -28
  165. package/dist/util/string.d.ts +0 -55
  166. package/index.js +0 -333
  167. package/lib/element.js +0 -618
  168. package/lib/node.js +0 -730
  169. package/lib/ranges.js +0 -130
  170. package/lib/text.js +0 -265
  171. package/lib/title.js +0 -83
  172. package/mixin/attributeParent.js +0 -117
  173. package/mixin/fixedToken.js +0 -40
  174. package/mixin/hidden.js +0 -21
  175. package/mixin/singleLine.js +0 -31
  176. package/mixin/sol.js +0 -54
  177. package/parser/brackets.js +0 -128
  178. package/parser/commentAndExt.js +0 -62
  179. package/parser/converter.js +0 -46
  180. package/parser/externalLinks.js +0 -33
  181. package/parser/hrAndDoubleUnderscore.js +0 -49
  182. package/parser/html.js +0 -42
  183. package/parser/links.js +0 -94
  184. package/parser/list.js +0 -59
  185. package/parser/magicLinks.js +0 -41
  186. package/parser/quotes.js +0 -64
  187. package/parser/selector.js +0 -180
  188. package/parser/table.js +0 -114
  189. package/src/arg.js +0 -207
  190. package/src/atom/hidden.js +0 -13
  191. package/src/atom/index.js +0 -43
  192. package/src/attribute.js +0 -472
  193. package/src/attributes.js +0 -453
  194. package/src/charinsert.js +0 -97
  195. package/src/converter.js +0 -176
  196. package/src/converterFlags.js +0 -284
  197. package/src/converterRule.js +0 -256
  198. package/src/extLink.js +0 -180
  199. package/src/gallery.js +0 -149
  200. package/src/hasNowiki/index.js +0 -44
  201. package/src/hasNowiki/pre.js +0 -40
  202. package/src/heading.js +0 -134
  203. package/src/html.js +0 -254
  204. package/src/imageParameter.js +0 -303
  205. package/src/imagemap.js +0 -199
  206. package/src/imagemapLink.js +0 -41
  207. package/src/index.js +0 -938
  208. package/src/link/category.js +0 -44
  209. package/src/link/file.js +0 -287
  210. package/src/link/galleryImage.js +0 -120
  211. package/src/link/index.js +0 -388
  212. package/src/magicLink.js +0 -151
  213. package/src/nested/choose.js +0 -24
  214. package/src/nested/combobox.js +0 -23
  215. package/src/nested/index.js +0 -96
  216. package/src/nested/references.js +0 -23
  217. package/src/nowiki/comment.js +0 -71
  218. package/src/nowiki/dd.js +0 -59
  219. package/src/nowiki/doubleUnderscore.js +0 -56
  220. package/src/nowiki/hr.js +0 -41
  221. package/src/nowiki/index.js +0 -56
  222. package/src/nowiki/list.js +0 -16
  223. package/src/nowiki/noinclude.js +0 -28
  224. package/src/nowiki/quote.js +0 -69
  225. package/src/onlyinclude.js +0 -64
  226. package/src/paramTag/index.js +0 -89
  227. package/src/paramTag/inputbox.js +0 -35
  228. package/src/parameter.js +0 -239
  229. package/src/syntax.js +0 -91
  230. package/src/table/index.js +0 -985
  231. package/src/table/td.js +0 -343
  232. package/src/table/tr.js +0 -319
  233. package/src/tagPair/ext.js +0 -146
  234. package/src/tagPair/include.js +0 -50
  235. package/src/tagPair/index.js +0 -131
  236. package/src/transclude.js +0 -843
  237. package/tool/index.js +0 -1209
  238. package/typings/api.d.ts +0 -9
  239. package/typings/array.d.ts +0 -29
  240. package/typings/event.d.ts +0 -22
  241. package/typings/index.d.ts +0 -118
  242. package/typings/node.d.ts +0 -35
  243. package/typings/parser.d.ts +0 -12
  244. package/typings/table.d.ts +0 -10
  245. package/typings/token.d.ts +0 -31
  246. package/typings/tool.d.ts +0 -6
  247. package/util/base.js +0 -17
  248. package/util/debug.js +0 -73
  249. package/util/diff.js +0 -76
  250. package/util/lint.js +0 -57
  251. package/util/string.js +0 -126
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+ const debug_1 = require("../util/debug");
3
+ const string_1 = require("../util/string");
4
+ const Parser = require("../index");
5
+ const Token = require(".");
6
+ const AtomToken = require("./atom");
7
+ /**
8
+ * 转换规则
9
+ * @classdesc `{childNodes: ...AtomToken}`
10
+ */
11
+ class ConverterRuleToken extends Token {
12
+ /** @browser */
13
+ type = 'converter-rule';
14
+ /** 语言变体 */
15
+ get variant() {
16
+ return this.childNodes.at(-2)?.text()?.trim() ?? '';
17
+ }
18
+ set variant(variant) {
19
+ this.setVariant(variant);
20
+ }
21
+ /** 是否是单向转换 */
22
+ get unidirectional() {
23
+ return this.length === 3;
24
+ }
25
+ /** @throws `Error` 不能用于将双向转换或不转换更改为单向转换 */
26
+ set unidirectional(flag) {
27
+ const { length } = this;
28
+ if (length === 3 && !flag) {
29
+ this.makeBidirectional();
30
+ }
31
+ else if (length === 2 && flag) {
32
+ throw new Error('想更改为单向转换请使用 makeUnidirectional 方法!');
33
+ }
34
+ else if (length === 1 && flag) {
35
+ throw new Error('想更改为单向转换请先使用 setVariant 方法指定语言变体!');
36
+ }
37
+ }
38
+ /** 是否是双向转换 */
39
+ get bidirectional() {
40
+ return this.length === 2;
41
+ }
42
+ /** @throws `Error` 不能用于将双向转换更改为单向转换或将不转换更改为双向转换 */
43
+ set bidirectional(flag) {
44
+ const { length } = this;
45
+ if (length === 3 && flag) {
46
+ this.makeBidirectional();
47
+ }
48
+ else if (length === 2 && !flag) {
49
+ throw new Error('想更改为单向转换请使用 makeUnidirectional 方法!');
50
+ }
51
+ else if (length === 1 && flag) {
52
+ throw new Error('想更改为双向转换请使用 setVariant 方法!');
53
+ }
54
+ }
55
+ /**
56
+ * @browser
57
+ * @param rule 转换规则
58
+ * @param hasColon 是否带有":"
59
+ */
60
+ constructor(rule, hasColon = true, config = Parser.getConfig(), accum = []) {
61
+ super(undefined, config, true, accum);
62
+ if (hasColon) {
63
+ const i = rule.indexOf(':'), j = rule.slice(0, i).indexOf('=>'), v = j === -1 ? rule.slice(0, i) : rule.slice(j + 2, i);
64
+ if (config.variants.includes(v.trim())) {
65
+ super.insertAt(new AtomToken(v, 'converter-rule-variant', config, accum));
66
+ super.insertAt(new AtomToken(rule.slice(i + 1), 'converter-rule-to', config, accum));
67
+ if (j !== -1) {
68
+ super.insertAt(new AtomToken(rule.slice(0, j), 'converter-rule-from', config, accum), 0);
69
+ }
70
+ }
71
+ else {
72
+ super.insertAt(new AtomToken(rule, 'converter-rule-noconvert', config, accum));
73
+ }
74
+ }
75
+ else {
76
+ super.insertAt(new AtomToken(rule, 'converter-rule-noconvert', config, accum));
77
+ }
78
+ this.protectChildren('1:');
79
+ }
80
+ /**
81
+ * @override
82
+ * @browser
83
+ */
84
+ toString(selector) {
85
+ const { childNodes } = this;
86
+ if (childNodes.length === 3 && !(selector && this.matches(selector))) {
87
+ const [from, variant, to] = childNodes;
88
+ return `${from.toString(selector)}=>${variant.toString(selector)}:${to.toString(selector)}`;
89
+ }
90
+ return super.toString(selector, ':');
91
+ }
92
+ /**
93
+ * @override
94
+ * @browser
95
+ */
96
+ text() {
97
+ const { childNodes } = this;
98
+ if (childNodes.length === 3) {
99
+ const [from, variant, to] = childNodes;
100
+ return `${from.text()}=>${variant.text()}:${to.text()}`;
101
+ }
102
+ return super.text(':');
103
+ }
104
+ /** @private */
105
+ getGaps(i = 0) {
106
+ const { length } = this, j = i < 0 ? i + length : i;
107
+ return j === 0 && length === 3 ? 2 : 1;
108
+ }
109
+ /**
110
+ * @override
111
+ * @browser
112
+ */
113
+ print() {
114
+ const { childNodes } = this;
115
+ if (childNodes.length === 3) {
116
+ const [from, variant, to] = childNodes;
117
+ return `<span class="wpb-converter-rule">${from.print()}=>${variant.print()}:${to.print()}</span>`;
118
+ }
119
+ return super.print({ sep: ':' });
120
+ }
121
+ /** @override */
122
+ cloneNode() {
123
+ const cloned = this.cloneChildNodes(), placeholders = ['', 'zh:', '=>zh:'], placeholder = placeholders[cloned.length - 1];
124
+ return Parser.run(() => {
125
+ const token = new ConverterRuleToken(placeholder, placeholder, this.getAttribute('config'));
126
+ for (let i = 0; i < cloned.length; i++) {
127
+ token.childNodes[i].safeReplaceWith(cloned[i]);
128
+ }
129
+ token.afterBuild();
130
+ return token;
131
+ });
132
+ }
133
+ /** @private */
134
+ afterBuild() {
135
+ const /** @implements */ converterRuleListener = (e, data) => {
136
+ const { prevTarget } = e;
137
+ if (this.length > 1 && this.childNodes.at(-2) === prevTarget) {
138
+ const v = prevTarget.text().trim(), { variants } = this.getAttribute('config');
139
+ if (!variants.includes(v)) {
140
+ (0, debug_1.undo)(e, data);
141
+ throw new Error(`无效的语言变体:${v}`);
142
+ }
143
+ }
144
+ };
145
+ this.addEventListener(['remove', 'insert', 'text', 'replace'], converterRuleListener);
146
+ }
147
+ /**
148
+ * @override
149
+ * @param i 移除位置
150
+ * @throws `Error` 至少保留1个子节点
151
+ */
152
+ removeAt(i) {
153
+ if (this.length === 1) {
154
+ throw new Error(`${this.constructor.name} 需至少保留 1 个子节点!`);
155
+ }
156
+ const removed = super.removeAt(i);
157
+ if (this.length === 1) {
158
+ this.firstChild.type = 'converter-rule-noconvert';
159
+ }
160
+ return removed;
161
+ }
162
+ /**
163
+ * @override
164
+ * @throws `Error` 请勿手动插入子节点
165
+ */
166
+ insertAt() {
167
+ throw new Error(`转换规则语法复杂,请勿尝试对 ${this.constructor.name} 手动插入子节点!`);
168
+ }
169
+ /** 修改为不转换 */
170
+ noConvert() {
171
+ const { length } = this;
172
+ for (let i = 0; i < length - 1; i++) { // ConverterRuleToken只能从前往后删除子节点
173
+ this.removeAt(0);
174
+ }
175
+ }
176
+ /**
177
+ * 设置转换目标
178
+ * @param to 转换目标
179
+ * @throws `SyntaxError` 非法的转换目标
180
+ */
181
+ setTo(to) {
182
+ const config = this.getAttribute('config'), include = this.getAttribute('include'), root = Parser.parse(`-{|${config.variants[0] ?? 'zh'}:${to}}-`, include, undefined, config), { length, firstChild: converter } = root;
183
+ if (length !== 1 || converter.type !== 'converter') {
184
+ throw new SyntaxError(`非法的转换目标:${(0, string_1.noWrap)(to)}`);
185
+ }
186
+ const { lastChild: converterRule } = converter;
187
+ if (converter.length !== 2 || converterRule.length !== 2) {
188
+ throw new SyntaxError(`非法的转换目标:${(0, string_1.noWrap)(to)}`);
189
+ }
190
+ const { lastChild } = converterRule;
191
+ converterRule.destroy();
192
+ this.lastChild.safeReplaceWith(lastChild);
193
+ }
194
+ /**
195
+ * 设置语言变体
196
+ * @param variant 语言变体
197
+ * @throws `RangeError` 无效的语言变体
198
+ */
199
+ setVariant(variant) {
200
+ if (typeof variant !== 'string') {
201
+ this.typeError('setVariant', 'String');
202
+ }
203
+ const config = this.getAttribute('config'), v = variant.trim();
204
+ if (!config.variants.includes(v)) {
205
+ throw new RangeError(`无效的语言变体:${v}`);
206
+ }
207
+ else if (this.length === 1) {
208
+ super.insertAt(Parser.run(() => new AtomToken(variant, 'converter-rule-variant', config)), 0);
209
+ }
210
+ else {
211
+ this.childNodes.at(-2).setText(variant);
212
+ }
213
+ }
214
+ /**
215
+ * 设置转换原文
216
+ * @param from 转换原文
217
+ * @throws `Error` 尚未指定语言变体
218
+ * @throws `SyntaxError` 非法的转换原文
219
+ */
220
+ setFrom(from) {
221
+ const { variant, unidirectional } = this;
222
+ if (!variant) {
223
+ throw new Error('请先指定语言变体!');
224
+ }
225
+ const config = this.getAttribute('config'), root = Parser.parse(`-{|${from}=>${variant}:}-`, this.getAttribute('include'), undefined, config), { length, firstChild: converter } = root;
226
+ if (length !== 1 || converter.type !== 'converter') {
227
+ throw new SyntaxError(`非法的转换原文:${(0, string_1.noWrap)(from)}`);
228
+ }
229
+ const { lastChild: converterRule } = converter;
230
+ if (converter.length !== 2 || converterRule.length !== 3) {
231
+ throw new SyntaxError(`非法的转换原文:${(0, string_1.noWrap)(from)}`);
232
+ }
233
+ else if (unidirectional) {
234
+ this.firstChild.safeReplaceWith(converterRule.firstChild);
235
+ }
236
+ else {
237
+ super.insertAt(converterRule.firstChild, 0);
238
+ }
239
+ }
240
+ /**
241
+ * 修改为单向转换
242
+ * @param from 转换原文
243
+ */
244
+ makeUnidirectional(from) {
245
+ this.setFrom(from);
246
+ }
247
+ /** 修改为双向转换 */
248
+ makeBidirectional() {
249
+ if (this.unidirectional) {
250
+ this.removeAt(0);
251
+ }
252
+ }
253
+ }
254
+ Parser.classes['ConverterRuleToken'] = __filename;
255
+ module.exports = ConverterRuleToken;
@@ -1,62 +1,65 @@
1
- export = ExtLinkToken;
1
+ import * as Parser from '../index';
2
+ import Token = require('.');
3
+ import MagicLinkToken = require('./magicLink');
2
4
  /**
3
5
  * 外链
4
6
  * @classdesc `{childNodes: [MagicLinkToken, ?Token]}`
5
7
  */
6
- declare class ExtLinkToken extends Token {
8
+ declare abstract class ExtLinkToken extends Token {
9
+ #private;
10
+ /** @browser */
11
+ readonly type = "ext-link";
12
+ childNodes: [MagicLinkToken] | [MagicLinkToken, Token];
13
+ abstract get children(): [MagicLinkToken] | [MagicLinkToken, Token];
14
+ abstract get firstChild(): MagicLinkToken;
15
+ abstract get firstElementChild(): MagicLinkToken;
16
+ abstract get lastChild(): Token;
17
+ abstract get lastElementChild(): Token;
18
+ /** 协议 */
19
+ get protocol(): string | undefined;
20
+ set protocol(value: string | undefined);
21
+ /** 和内链保持一致 */
22
+ get link(): string;
23
+ set link(url: string);
24
+ /** 链接显示文字 */
25
+ get innerText(): string;
7
26
  /**
8
- * @param {string} url 网址
9
- * @param {string} space 空白字符
10
- * @param {string} text 链接文字
11
- * @param {import('../../typings/token').accum} accum
27
+ * @browser
28
+ * @param url 网址
29
+ * @param space 空白字符
30
+ * @param text 链接文字
12
31
  */
13
- constructor(url: string, space?: string, text?: string, config?: import("../../typings/token").ParserConfig, accum?: import('../../typings/token').accum);
14
- /** @this {{firstChild: MagicLinkToken}} */
15
- set protocol(arg: string);
32
+ constructor(url: string, space?: string, text?: string, config?: Parser.Config, accum?: Token[]);
16
33
  /**
17
- * 协议
18
- * @this {{firstChild: MagicLinkToken}}
34
+ * @override
35
+ * @browser
19
36
  */
20
- get protocol(): string;
21
- set link(arg: string);
37
+ toString(selector?: string): string;
22
38
  /**
23
- * 和内链保持一致
24
- * @this {{firstChild: MagicLinkToken}}
39
+ * @override
40
+ * @browser
25
41
  */
26
- get link(): string;
27
- /** 链接显示文字 */
28
- get innerText(): string;
42
+ text(): string;
29
43
  /**
30
44
  * @override
31
- * @param {string} selector
45
+ * @browser
32
46
  */
33
- override toString(selector: string): string;
34
- /** @override */
35
- override text(): string;
36
- /** @override */
37
- override print(): string;
47
+ print(): string;
38
48
  /** @override */
39
- override cloneNode(): ExtLinkToken;
40
- /**
41
- * 获取网址
42
- * @this {{firstChild: MagicLinkToken}}
43
- */
44
- getUrl(this: {
45
- firstChild: MagicLinkToken;
46
- }): URL;
49
+ cloneNode(): this;
50
+ /** 获取网址 */
51
+ getUrl(): URL;
47
52
  /**
48
53
  * 设置链接目标
49
- * @param {string|URL} url 网址
54
+ * @param url 网址
50
55
  * @throws `SyntaxError` 非法的外链目标
51
56
  */
52
57
  setTarget(url: string | URL): void;
53
58
  /**
54
59
  * 设置链接显示文字
55
- * @param {string} text 链接显示文字
60
+ * @param str 链接显示文字
56
61
  * @throws `SyntaxError` 非法的链接显示文字
57
62
  */
58
- setLinkText(text: string): void;
59
- #private;
63
+ setLinkText(str: string): void;
60
64
  }
61
- import Token = require(".");
62
- import MagicLinkToken = require("./magicLink");
65
+ export = ExtLinkToken;
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ const string_1 = require("../util/string");
3
+ const Parser = require("../index");
4
+ const Token = require(".");
5
+ const MagicLinkToken = require("./magicLink");
6
+ /**
7
+ * 外链
8
+ * @classdesc `{childNodes: [MagicLinkToken, ?Token]}`
9
+ */
10
+ class ExtLinkToken extends Token {
11
+ /** @browser */
12
+ type = 'ext-link';
13
+ /** @browser */
14
+ #space;
15
+ /** 协议 */
16
+ get protocol() {
17
+ return this.firstChild.protocol;
18
+ }
19
+ set protocol(value) {
20
+ this.firstChild.protocol = value;
21
+ }
22
+ /** 和内链保持一致 */
23
+ get link() {
24
+ return this.firstChild.link;
25
+ }
26
+ set link(url) {
27
+ this.setTarget(url);
28
+ }
29
+ /** 链接显示文字 */
30
+ get innerText() {
31
+ return this.length > 1
32
+ ? this.lastChild.text()
33
+ : `[${this.getRootNode().querySelectorAll('ext-link[childElementCount=1]').indexOf(this) + 1}]`;
34
+ }
35
+ /**
36
+ * @browser
37
+ * @param url 网址
38
+ * @param space 空白字符
39
+ * @param text 链接文字
40
+ */
41
+ constructor(url, space = '', text = '', config = Parser.getConfig(), accum = []) {
42
+ super(undefined, config, true, accum, {
43
+ MagicLinkToken: 0, Token: 1,
44
+ });
45
+ this.insertAt(new MagicLinkToken(url, true, config, accum));
46
+ this.#space = space;
47
+ if (text) {
48
+ const inner = new Token(text, config, true, accum, {
49
+ 'Stage-7': ':', ConverterToken: ':',
50
+ });
51
+ inner.type = 'ext-link-text';
52
+ this.insertAt(inner.setAttribute('stage', Parser.MAX_STAGE - 1));
53
+ }
54
+ this.protectChildren(0);
55
+ }
56
+ /**
57
+ * @override
58
+ * @browser
59
+ */
60
+ toString(selector) {
61
+ if (selector && this.matches(selector)) {
62
+ return '';
63
+ }
64
+ else if (this.length === 1) {
65
+ return `[${super.toString(selector)}${this.#space}]`;
66
+ }
67
+ this.#correct();
68
+ (0, string_1.normalizeSpace)(this.lastChild);
69
+ return `[${super.toString(selector, this.#space)}]`;
70
+ }
71
+ /**
72
+ * @override
73
+ * @browser
74
+ */
75
+ text() {
76
+ (0, string_1.normalizeSpace)(this.childNodes[1]);
77
+ return `[${super.text(' ')}]`;
78
+ }
79
+ /** @private */
80
+ getPadding() {
81
+ return 1;
82
+ }
83
+ /** @private */
84
+ getGaps() {
85
+ this.#correct();
86
+ return this.#space.length;
87
+ }
88
+ /**
89
+ * @override
90
+ * @browser
91
+ */
92
+ print() {
93
+ return super.print(this.length > 1 ? { pre: '[', sep: this.#space, post: ']' } : { pre: '[', post: `${this.#space}]` });
94
+ }
95
+ /** @override */
96
+ cloneNode() {
97
+ const [url, text] = this.cloneChildNodes();
98
+ return Parser.run(() => {
99
+ const token = new ExtLinkToken(undefined, '', '', this.getAttribute('config'));
100
+ token.firstChild.safeReplaceWith(url);
101
+ if (text) {
102
+ token.insertAt(text);
103
+ }
104
+ return token;
105
+ });
106
+ }
107
+ /** 修正空白字符 */
108
+ #correct() {
109
+ if (!this.#space && this.length > 1
110
+ // 都替换成`<`肯定不对,但无妨
111
+ && /^[^[\]<>"{\0-\x1F\x7F\p{Zs}\uFFFD]/u.test(this.lastChild.text().replace(/&[lg]t;/u, '<'))) {
112
+ this.#space = ' ';
113
+ }
114
+ }
115
+ /** 获取网址 */
116
+ getUrl() {
117
+ return this.firstChild.getUrl();
118
+ }
119
+ /**
120
+ * 设置链接目标
121
+ * @param url 网址
122
+ * @throws `SyntaxError` 非法的外链目标
123
+ */
124
+ setTarget(url) {
125
+ const strUrl = String(url), root = Parser.parse(`[${strUrl}]`, this.getAttribute('include'), 8, this.getAttribute('config')), { length, firstChild: extLink } = root;
126
+ if (length !== 1 || extLink.type !== 'ext-link' || extLink.length !== 1) {
127
+ throw new SyntaxError(`非法的外链目标:${strUrl}`);
128
+ }
129
+ const { firstChild } = extLink;
130
+ extLink.destroy();
131
+ this.firstChild.safeReplaceWith(firstChild);
132
+ }
133
+ /**
134
+ * 设置链接显示文字
135
+ * @param str 链接显示文字
136
+ * @throws `SyntaxError` 非法的链接显示文字
137
+ */
138
+ setLinkText(str) {
139
+ const root = Parser.parse(`[//url ${str}]`, this.getAttribute('include'), 8, this.getAttribute('config')), { length, firstChild: extLink } = root;
140
+ if (length !== 1 || extLink.type !== 'ext-link' || extLink.length !== 2) {
141
+ throw new SyntaxError(`非法的外链文字:${(0, string_1.noWrap)(str)}`);
142
+ }
143
+ const { lastChild } = extLink;
144
+ if (this.length === 1) {
145
+ this.insertAt(lastChild);
146
+ }
147
+ else {
148
+ this.lastChild.safeReplaceWith(lastChild);
149
+ }
150
+ this.#space ||= ' ';
151
+ }
152
+ }
153
+ Parser.classes['ExtLinkToken'] = __filename;
154
+ module.exports = ExtLinkToken;
@@ -1,33 +1,74 @@
1
- export = GalleryToken;
1
+ import * as Parser from '../index';
2
+ import AstText = require('../lib/text');
3
+ import Token = require('.');
4
+ import GalleryImageToken = require('./link/galleryImage');
5
+ import HiddenToken = require('./hidden');
6
+ import AttributesToken = require('./attributes');
7
+ import ExtToken = require('./tagPair/ext');
8
+ import type { AstNodeTypes } from '../lib/node';
2
9
  /**
3
10
  * gallery标签
4
11
  * @classdesc `{childNodes: ...(GalleryImageToken|HiddenToken|AstText)}`
5
12
  */
6
- declare class GalleryToken extends Token {
13
+ declare abstract class GalleryToken extends Token {
14
+ /** @browser */
15
+ readonly type = "ext-inner";
16
+ childNodes: (GalleryImageToken | HiddenToken | AstText)[];
17
+ abstract get children(): (GalleryImageToken | HiddenToken)[];
18
+ abstract get firstChild(): GalleryImageToken | HiddenToken | AstText | undefined;
19
+ abstract get firstElementChild(): GalleryImageToken | HiddenToken | undefined;
20
+ abstract get lastChild(): GalleryImageToken | HiddenToken | AstText | undefined;
21
+ abstract get lastElementChild(): GalleryImageToken | HiddenToken | 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
+ get images(): GalleryImageToken[];
7
30
  /**
8
- * @param {string} inner 标签内部wikitext
9
- * @param {import('../../typings/token').accum} accum
31
+ * @browser
32
+ * @param inner 标签内部wikitext
10
33
  */
11
- constructor(inner: string, config?: import("../../typings/token").ParserConfig, accum?: import('../../typings/token').accum);
12
- /** 所有图片 */
13
- get images(): this[];
34
+ constructor(inner?: string, config?: Parser.Config, accum?: Token[]);
14
35
  /**
15
36
  * @override
16
- * @param {string} selector
37
+ * @browser
17
38
  */
18
- override toString(selector: string): string;
19
- /** @override */
20
- override text(): string;
21
- /** @override */
22
- override print(): string;
39
+ toString(selector?: string): string;
40
+ /**
41
+ * @override
42
+ * @browser
43
+ */
44
+ text(): string;
45
+ /**
46
+ * @override
47
+ * @browser
48
+ */
49
+ print(): string;
50
+ /**
51
+ * @override
52
+ * @browser
53
+ */
54
+ lint(start?: number): Parser.LintError[];
23
55
  /** @override */
24
- override cloneNode(): GalleryToken;
56
+ cloneNode(): this;
25
57
  /**
26
58
  * 插入图片
27
- * @param {string} file 图片文件名
28
- * @param {number} i 插入位置
59
+ * @param file 图片文件名
60
+ * @param i 插入位置
29
61
  * @throws `SyntaxError` 非法的文件名
30
62
  */
31
- insertImage(file: string, i?: number): Token;
63
+ insertImage(file: string, i?: number): GalleryImageToken;
64
+ /**
65
+ * @override
66
+ * @param token 待插入的节点
67
+ * @param i 插入位置
68
+ * @throws `RangeError` 插入不可见内容
69
+ */
70
+ insertAt(token: string, i?: number): AstText;
71
+ /** @ignore */
72
+ insertAt<T extends AstNodeTypes>(token: T, i?: number): T;
32
73
  }
33
- import Token = require(".");
74
+ export = GalleryToken;