wikiparser-node 1.4.3-b → 1.4.4

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 (166) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/config/.schema.json +172 -0
  4. package/config/enwiki.json +814 -1
  5. package/config/llwiki.json +35 -1
  6. package/config/moegirl.json +44 -1
  7. package/config/zhwiki.json +466 -1
  8. package/dist/addon/table.d.ts +6 -0
  9. package/dist/addon/table.js +564 -0
  10. package/dist/base.d.ts +58 -0
  11. package/dist/index.d.ts +30 -0
  12. package/dist/index.js +211 -0
  13. package/dist/internal.d.ts +44 -0
  14. package/dist/lib/element.d.ts +155 -0
  15. package/dist/lib/element.js +655 -0
  16. package/dist/lib/node.d.ts +146 -0
  17. package/dist/lib/node.js +428 -0
  18. package/dist/lib/range.d.ts +104 -0
  19. package/dist/lib/range.js +385 -0
  20. package/dist/lib/ranges.d.ts +26 -0
  21. package/dist/lib/ranges.js +118 -0
  22. package/dist/lib/text.d.ts +62 -0
  23. package/dist/lib/text.js +306 -0
  24. package/dist/lib/title.d.ts +38 -0
  25. package/dist/lib/title.js +164 -0
  26. package/dist/mixin/attributesParent.d.ts +50 -0
  27. package/dist/mixin/attributesParent.js +78 -0
  28. package/dist/mixin/fixed.d.ts +6 -0
  29. package/dist/mixin/fixed.js +30 -0
  30. package/dist/mixin/flagsParent.d.ts +72 -0
  31. package/dist/mixin/flagsParent.js +59 -0
  32. package/dist/mixin/hidden.d.ts +5 -0
  33. package/dist/mixin/hidden.js +24 -0
  34. package/dist/mixin/magicLinkParent.d.ts +40 -0
  35. package/dist/mixin/magicLinkParent.js +42 -0
  36. package/dist/mixin/singleLine.d.ts +6 -0
  37. package/dist/mixin/singleLine.js +26 -0
  38. package/dist/mixin/sol.d.ts +6 -0
  39. package/dist/mixin/sol.js +43 -0
  40. package/dist/mixin/syntax.d.ts +9 -0
  41. package/dist/mixin/syntax.js +50 -0
  42. package/dist/parser/braces.js +131 -0
  43. package/dist/parser/commentAndExt.js +90 -0
  44. package/dist/parser/converter.js +41 -0
  45. package/dist/parser/externalLinks.js +32 -0
  46. package/dist/parser/hrAndDoubleUnderscore.js +41 -0
  47. package/dist/parser/html.js +39 -0
  48. package/dist/parser/links.js +95 -0
  49. package/dist/parser/list.js +68 -0
  50. package/dist/parser/magicLinks.js +43 -0
  51. package/dist/parser/quotes.js +68 -0
  52. package/dist/parser/selector.js +162 -0
  53. package/dist/parser/table.js +124 -0
  54. package/dist/src/arg.d.ts +58 -0
  55. package/dist/src/arg.js +196 -0
  56. package/dist/src/atom.d.ts +12 -0
  57. package/dist/src/atom.js +27 -0
  58. package/dist/src/attribute.d.ts +69 -0
  59. package/dist/src/attribute.js +475 -0
  60. package/dist/src/attributes.d.ts +109 -0
  61. package/dist/src/attributes.js +346 -0
  62. package/dist/src/converter.d.ts +33 -0
  63. package/dist/src/converter.js +134 -0
  64. package/dist/src/converterFlags.d.ts +87 -0
  65. package/dist/src/converterFlags.js +226 -0
  66. package/dist/src/converterRule.d.ts +77 -0
  67. package/dist/src/converterRule.js +209 -0
  68. package/dist/src/extLink.d.ts +43 -0
  69. package/dist/src/extLink.js +186 -0
  70. package/dist/src/gallery.d.ts +54 -0
  71. package/dist/src/gallery.js +126 -0
  72. package/dist/src/heading.d.ts +47 -0
  73. package/dist/src/heading.js +190 -0
  74. package/dist/src/hidden.d.ts +7 -0
  75. package/dist/src/hidden.js +23 -0
  76. package/dist/src/html.d.ts +65 -0
  77. package/dist/src/html.js +337 -0
  78. package/dist/src/imageParameter.d.ts +65 -0
  79. package/dist/src/imageParameter.js +246 -0
  80. package/dist/src/imagemap.d.ts +56 -0
  81. package/dist/src/imagemap.js +150 -0
  82. package/dist/src/imagemapLink.d.ts +31 -0
  83. package/dist/src/imagemapLink.js +93 -0
  84. package/dist/src/index.d.ts +143 -0
  85. package/dist/src/index.js +803 -0
  86. package/dist/src/link/base.d.ts +52 -0
  87. package/dist/src/link/base.js +209 -0
  88. package/dist/src/link/category.d.ts +13 -0
  89. package/dist/src/link/category.js +28 -0
  90. package/dist/src/link/file.d.ts +96 -0
  91. package/dist/src/link/file.js +265 -0
  92. package/dist/src/link/galleryImage.d.ts +32 -0
  93. package/dist/src/link/galleryImage.js +157 -0
  94. package/dist/src/link/index.d.ts +56 -0
  95. package/dist/src/link/index.js +133 -0
  96. package/dist/src/magicLink.d.ts +59 -0
  97. package/dist/src/magicLink.js +199 -0
  98. package/dist/src/nested.d.ts +43 -0
  99. package/dist/src/nested.js +92 -0
  100. package/dist/src/nowiki/base.d.ts +31 -0
  101. package/dist/src/nowiki/base.js +93 -0
  102. package/dist/src/nowiki/comment.d.ts +25 -0
  103. package/dist/src/nowiki/comment.js +68 -0
  104. package/dist/src/nowiki/dd.d.ts +8 -0
  105. package/dist/src/nowiki/dd.js +24 -0
  106. package/dist/src/nowiki/doubleUnderscore.d.ts +18 -0
  107. package/dist/src/nowiki/doubleUnderscore.js +101 -0
  108. package/dist/src/nowiki/hr.d.ts +5 -0
  109. package/dist/src/nowiki/hr.js +63 -0
  110. package/dist/src/nowiki/index.d.ts +16 -0
  111. package/dist/src/nowiki/index.js +21 -0
  112. package/dist/src/nowiki/list.d.ts +16 -0
  113. package/dist/src/nowiki/list.js +97 -0
  114. package/dist/src/nowiki/listBase.d.ts +5 -0
  115. package/dist/src/nowiki/listBase.js +61 -0
  116. package/dist/src/nowiki/noinclude.d.ts +10 -0
  117. package/dist/src/nowiki/noinclude.js +23 -0
  118. package/dist/src/nowiki/quote.d.ts +14 -0
  119. package/dist/src/nowiki/quote.js +116 -0
  120. package/dist/src/onlyinclude.d.ts +16 -0
  121. package/dist/src/onlyinclude.js +57 -0
  122. package/dist/src/paramTag/index.d.ts +37 -0
  123. package/dist/src/paramTag/index.js +69 -0
  124. package/dist/src/paramTag/inputbox.d.ts +8 -0
  125. package/dist/src/paramTag/inputbox.js +22 -0
  126. package/dist/src/parameter.d.ts +70 -0
  127. package/dist/src/parameter.js +262 -0
  128. package/dist/src/pre.d.ts +28 -0
  129. package/dist/src/pre.js +55 -0
  130. package/dist/src/syntax.d.ts +20 -0
  131. package/dist/src/syntax.js +86 -0
  132. package/dist/src/table/base.d.ts +30 -0
  133. package/dist/src/table/base.js +132 -0
  134. package/dist/src/table/index.d.ts +233 -0
  135. package/dist/src/table/index.js +396 -0
  136. package/dist/src/table/td.d.ts +92 -0
  137. package/dist/src/table/td.js +337 -0
  138. package/dist/src/table/tr.d.ts +32 -0
  139. package/dist/src/table/tr.js +58 -0
  140. package/dist/src/table/trBase.d.ts +53 -0
  141. package/dist/src/table/trBase.js +154 -0
  142. package/dist/src/tagPair/ext.d.ts +32 -0
  143. package/dist/src/tagPair/ext.js +203 -0
  144. package/dist/src/tagPair/include.d.ts +35 -0
  145. package/dist/src/tagPair/include.js +63 -0
  146. package/dist/src/tagPair/index.d.ts +27 -0
  147. package/dist/src/tagPair/index.js +128 -0
  148. package/dist/src/transclude.d.ts +167 -0
  149. package/dist/src/transclude.js +710 -0
  150. package/dist/util/constants.js +113 -0
  151. package/dist/util/debug.js +88 -0
  152. package/dist/util/diff.js +82 -0
  153. package/dist/util/lint.js +30 -0
  154. package/dist/util/string.js +53 -0
  155. package/errors/README +1 -0
  156. package/i18n/zh-hans.json +1 -0
  157. package/i18n/zh-hant.json +1 -0
  158. package/package.json +15 -30
  159. package/printed/README +1 -0
  160. package/bundle/bundle.min.js +0 -36
  161. package/extensions/dist/base.js +0 -64
  162. package/extensions/dist/editor.js +0 -159
  163. package/extensions/dist/highlight.js +0 -59
  164. package/extensions/dist/lint.js +0 -48
  165. package/extensions/editor.css +0 -63
  166. package/extensions/ui.css +0 -141
@@ -0,0 +1,52 @@
1
+ import Parser from '../../index';
2
+ import { Token } from '../index';
3
+ import { AtomToken } from '../atom';
4
+ import type { LintError } from '../../base';
5
+ import type { Title } from '../../lib/title';
6
+ /**
7
+ * 内链
8
+ * @classdesc `{childNodes: [AtomToken, ...Token]}`
9
+ */
10
+ export declare abstract class LinkBaseToken extends Token {
11
+ #private;
12
+ type: 'link' | 'category' | 'file' | 'gallery-image' | 'imagemap-image';
13
+ readonly name: string;
14
+ readonly childNodes: readonly [AtomToken, ...Token[]];
15
+ abstract get firstChild(): AtomToken;
16
+ abstract get lastChild(): Token;
17
+ abstract get children(): [AtomToken, ...Token[]];
18
+ abstract get firstElementChild(): AtomToken;
19
+ abstract get lastElementChild(): Token;
20
+ /** 完整链接 */
21
+ get link(): string | Title;
22
+ set link(link: string);
23
+ /** fragment */
24
+ get fragment(): string | undefined;
25
+ set fragment(fragment: string | undefined);
26
+ /**
27
+ * @param link 链接标题
28
+ * @param linkText 链接显示文字
29
+ * @param delimiter `|`
30
+ */
31
+ constructor(link: string, linkText?: string, config?: Parser.Config, accum?: Token[], delimiter?: string);
32
+ /** @override */
33
+ text(): string;
34
+ /** @override */
35
+ lint(start?: number): LintError[];
36
+ /** @override */
37
+ print(): string;
38
+ /** @override */
39
+ cloneNode(this: this & {
40
+ constructor: new (...args: any[]) => unknown;
41
+ }): this;
42
+ /**
43
+ * 设置链接目标
44
+ * @param link 链接目标
45
+ */
46
+ setTarget(link: string): void;
47
+ /**
48
+ * 设置链接显示文字
49
+ * @param linkStr 链接显示文字
50
+ */
51
+ setLinkText(linkStr?: string): void;
52
+ }
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LinkBaseToken = void 0;
4
+ const lint_1 = require("../../util/lint");
5
+ const constants_1 = require("../../util/constants");
6
+ const debug_1 = require("../../util/debug");
7
+ const index_1 = require("../../index");
8
+ const index_2 = require("../index");
9
+ const atom_1 = require("../atom");
10
+ /**
11
+ * 内链
12
+ * @classdesc `{childNodes: [AtomToken, ...Token]}`
13
+ */
14
+ class LinkBaseToken extends index_2.Token {
15
+ /* NOT FOR BROWSER END */
16
+ #bracket = true;
17
+ #delimiter;
18
+ #title;
19
+ /** 完整链接 */
20
+ get link() {
21
+ return this.#title;
22
+ }
23
+ set link(link) {
24
+ this.setTarget(link);
25
+ }
26
+ /** fragment */
27
+ get fragment() {
28
+ return this.#title.fragment;
29
+ }
30
+ set fragment(fragment) {
31
+ if (fragment === undefined) {
32
+ this.setTarget(this.name);
33
+ }
34
+ }
35
+ /* NOT FOR BROWSER END */
36
+ /**
37
+ * @param link 链接标题
38
+ * @param linkText 链接显示文字
39
+ * @param delimiter `|`
40
+ */
41
+ constructor(link, linkText, config = index_1.default.getConfig(), accum = [], delimiter = '|') {
42
+ super(undefined, config, accum, {
43
+ AtomToken: 0, Token: 1,
44
+ });
45
+ this.insertAt(new atom_1.AtomToken(link, 'link-target', config, accum, {
46
+ 'Stage-2': ':', '!ExtToken': '', '!HeadingToken': '',
47
+ }));
48
+ if (linkText !== undefined) {
49
+ const inner = new index_2.Token(linkText, config, accum, {
50
+ 'Stage-5': ':', QuoteToken: ':', ConverterToken: ':',
51
+ });
52
+ inner.type = 'link-text';
53
+ inner.setAttribute('stage', constants_1.MAX_STAGE - 1);
54
+ this.insertAt(inner);
55
+ }
56
+ this.#delimiter = delimiter;
57
+ /* NOT FOR BROWSER */
58
+ this.protectChildren(0);
59
+ }
60
+ /** @private */
61
+ afterBuild() {
62
+ this.#title = this.getTitle();
63
+ if (this.#delimiter.includes('\0')) {
64
+ this.#delimiter = this.buildFromStr(this.#delimiter, constants_1.BuildMethod.String);
65
+ }
66
+ /* NOT FOR BROWSER */
67
+ this.setAttribute('name', this.#title.title);
68
+ const /** @implements */ linkListener = (e, data) => {
69
+ const { prevTarget } = e;
70
+ if (prevTarget?.type === 'link-target') {
71
+ const name = prevTarget.text(), titleObj = this.getTitle(), { title, interwiki, ns, valid } = titleObj;
72
+ if (!valid) {
73
+ (0, debug_1.undo)(e, data);
74
+ throw new Error(`非法的内链目标:${name}`);
75
+ }
76
+ else if (this.type === 'category' && (interwiki || ns !== 14)
77
+ || this.type === 'file' && (interwiki || ns !== 6)) {
78
+ (0, debug_1.undo)(e, data);
79
+ throw new Error(`${this.type === 'file' ? '文件' : '分类'}链接不可更改命名空间:${name}`);
80
+ }
81
+ else if (this.type === 'link'
82
+ && !interwiki
83
+ && (ns === 6 || ns === 14)
84
+ && !name.trim().startsWith(':')) {
85
+ const { firstChild } = prevTarget;
86
+ if (firstChild?.type === 'text') {
87
+ firstChild.insertData(0, ':');
88
+ }
89
+ else {
90
+ prevTarget.prepend(':');
91
+ }
92
+ }
93
+ this.#title = titleObj;
94
+ this.setAttribute('name', title);
95
+ }
96
+ };
97
+ this.addEventListener(['remove', 'insert', 'replace', 'text'], linkListener);
98
+ }
99
+ /** @private */
100
+ setAttribute(key, value) {
101
+ if (key === 'bracket') {
102
+ this.#bracket = Boolean(value);
103
+ }
104
+ else if (key === 'title') {
105
+ this.#title = value;
106
+ }
107
+ else {
108
+ super.setAttribute(key, value);
109
+ }
110
+ }
111
+ /** @private */
112
+ toString() {
113
+ const str = super.toString(this.#delimiter);
114
+ return this.#bracket ? `[[${str}]]` : str;
115
+ }
116
+ /** @override */
117
+ text() {
118
+ const str = super.text('|');
119
+ return this.#bracket ? `[[${str}]]` : str;
120
+ }
121
+ /** @private */
122
+ getAttribute(key) {
123
+ if (key === 'title') {
124
+ return this.#title;
125
+ }
126
+ return key === 'padding' ? 2 : super.getAttribute(key);
127
+ }
128
+ /** @private */
129
+ getGaps(i) {
130
+ return i === 0 ? this.#delimiter.length : 1;
131
+ }
132
+ /** @override */
133
+ lint(start = this.getAbsoluteIndex()) {
134
+ const errors = super.lint(start), { childNodes: [target, linkText], type: linkType } = this, { encoded, fragment } = this.#title;
135
+ let rect;
136
+ if (target.childNodes.some(({ type }) => type === 'template')) {
137
+ rect = { start, ...this.getRootNode().posFromIndex(start) };
138
+ errors.push((0, lint_1.generateForChild)(target, rect, 'template in an internal link target', 'warning'));
139
+ }
140
+ if (encoded) {
141
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
142
+ errors.push((0, lint_1.generateForChild)(target, rect, 'unnecessary URL encoding in an internal link'));
143
+ }
144
+ if ((linkType === 'link' || linkType === 'category')
145
+ && linkText?.childNodes.some(({ type, data }) => type === 'text' && data.includes('|'))) {
146
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
147
+ errors.push((0, lint_1.generateForChild)(linkText, rect, 'additional "|" in the link text', 'warning'));
148
+ }
149
+ else if (linkType !== 'link' && fragment !== undefined) {
150
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
151
+ errors.push((0, lint_1.generateForChild)(target, rect, 'useless fragment'));
152
+ }
153
+ return errors;
154
+ }
155
+ /** @private */
156
+ getTitle() {
157
+ return this.normalizeTitle(this.firstChild.text(), 0, false, true, true);
158
+ }
159
+ /** @override */
160
+ print() {
161
+ return super.print(this.#bracket ? { pre: '[[', post: ']]', sep: this.#delimiter } : { sep: this.#delimiter });
162
+ }
163
+ /* NOT FOR BROWSER */
164
+ /** @override */
165
+ cloneNode() {
166
+ const [link, ...linkText] = this.cloneChildNodes();
167
+ return debug_1.Shadow.run(() => {
168
+ const token = new this.constructor('', undefined, this.getAttribute('config'));
169
+ token.firstChild.safeReplaceWith(link);
170
+ token.append(...linkText);
171
+ token.afterBuild();
172
+ return token;
173
+ });
174
+ }
175
+ /**
176
+ * 设置链接目标
177
+ * @param link 链接目标
178
+ */
179
+ setTarget(link) {
180
+ const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(String(link), this.getAttribute('include'), 2, config), token = debug_1.Shadow.run(() => new atom_1.AtomToken(undefined, 'link-target', config, [], {
181
+ 'Stage-2': ':', '!ExtToken': '', '!HeadingToken': '',
182
+ }));
183
+ token.append(...childNodes);
184
+ this.firstChild.safeReplaceWith(token);
185
+ }
186
+ /**
187
+ * 设置链接显示文字
188
+ * @param linkStr 链接显示文字
189
+ */
190
+ setLinkText(linkStr) {
191
+ if (linkStr === undefined) {
192
+ this.childNodes[1]?.remove();
193
+ return;
194
+ }
195
+ const root = index_1.default.parse(linkStr, this.getAttribute('include'), undefined, this.getAttribute('config'));
196
+ if (this.length === 1) {
197
+ root.type = 'link-text';
198
+ root.setAttribute('acceptable', {
199
+ 'Stage-5': ':', QuoteToken: ':', ConverterToken: ':',
200
+ });
201
+ this.insertAt(root);
202
+ }
203
+ else {
204
+ this.lastChild.replaceChildren(...root.childNodes);
205
+ }
206
+ }
207
+ }
208
+ exports.LinkBaseToken = LinkBaseToken;
209
+ constants_1.classes['LinkBaseToken'] = __filename;
@@ -0,0 +1,13 @@
1
+ import { LinkBaseToken } from './base';
2
+ /** 分类 */
3
+ export declare abstract class CategoryToken extends LinkBaseToken {
4
+ readonly type = "category";
5
+ /** 分类排序关键字 */
6
+ get sortkey(): string | undefined;
7
+ set sortkey(text: string | undefined);
8
+ /**
9
+ * 设置排序关键字
10
+ * @param text 排序关键字
11
+ */
12
+ setSortkey(text?: string): void;
13
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CategoryToken = void 0;
4
+ const string_1 = require("../../util/string");
5
+ const constants_1 = require("../../util/constants");
6
+ const base_1 = require("./base");
7
+ /** 分类 */
8
+ class CategoryToken extends base_1.LinkBaseToken {
9
+ type = 'category';
10
+ /* NOT FOR BROWSER */
11
+ /** 分类排序关键字 */
12
+ get sortkey() {
13
+ const { childNodes: [, child] } = this;
14
+ return child && (0, string_1.decodeHtml)(child.text());
15
+ }
16
+ set sortkey(text) {
17
+ this.setSortkey(text);
18
+ }
19
+ /**
20
+ * 设置排序关键字
21
+ * @param text 排序关键字
22
+ */
23
+ setSortkey(text) {
24
+ this.setLinkText(text);
25
+ }
26
+ }
27
+ exports.CategoryToken = CategoryToken;
28
+ constants_1.classes['CategoryToken'] = __filename;
@@ -0,0 +1,96 @@
1
+ import Parser from '../../index';
2
+ import { LinkBaseToken } from './base';
3
+ import { ImageParameterToken } from '../imageParameter';
4
+ import type { Title } from '../../lib/title';
5
+ import type { LintError } from '../../base';
6
+ import type { Token, AtomToken } from '../../internal';
7
+ /**
8
+ * 图片
9
+ * @classdesc `{childNodes: [AtomToken, ...ImageParameterToken]}`
10
+ */
11
+ export declare abstract class FileToken extends LinkBaseToken {
12
+ #private;
13
+ readonly type: 'file' | 'gallery-image' | 'imagemap-image';
14
+ readonly childNodes: readonly [AtomToken, ...ImageParameterToken[]];
15
+ abstract get lastChild(): AtomToken | ImageParameterToken;
16
+ abstract get children(): [AtomToken, ...ImageParameterToken[]];
17
+ abstract get lastElementChild(): AtomToken | ImageParameterToken;
18
+ /** 图片链接 */
19
+ get link(): string | Title;
20
+ set link(value: string);
21
+ /** 图片大小 */
22
+ get size(): {
23
+ width: string;
24
+ height: string;
25
+ } | undefined;
26
+ set size(size: {
27
+ width: string;
28
+ height: string;
29
+ } | undefined);
30
+ /** 图片宽度 */
31
+ get width(): string | undefined;
32
+ set width(width: string | undefined);
33
+ /** 图片高度 */
34
+ get height(): string | undefined;
35
+ set height(height: string | undefined);
36
+ /**
37
+ * @param link 文件名
38
+ * @param text 图片参数
39
+ * @param delimiter `|`
40
+ */
41
+ constructor(link: string, text?: string, config?: Parser.Config, accum?: Token[], delimiter?: string);
42
+ /** @override */
43
+ lint(start?: number): LintError[];
44
+ /** 获取所有图片参数节点 */
45
+ getAllArgs(): ImageParameterToken[];
46
+ /**
47
+ * 获取指定图片参数
48
+ * @param key 参数名
49
+ */
50
+ getArgs(key: string): ImageParameterToken[];
51
+ /** 获取图片框架属性参数节点 */
52
+ getFrameArgs(): ImageParameterToken[];
53
+ /** 获取图片水平对齐参数节点 */
54
+ getHorizAlignArgs(): ImageParameterToken[];
55
+ /** 获取图片垂直对齐参数节点 */
56
+ getVertAlignArgs(): ImageParameterToken[];
57
+ /**
58
+ * 获取生效的指定图片参数
59
+ * @param key 参数名
60
+ */
61
+ getArg(key: string): ImageParameterToken | undefined;
62
+ /**
63
+ * 获取生效的指定图片参数值
64
+ * @param key 参数名
65
+ */
66
+ getValue(key: string): string | true | undefined;
67
+ /**
68
+ * 是否具有指定图片参数
69
+ * @param key 参数名
70
+ */
71
+ hasArg(key: string): boolean;
72
+ /**
73
+ * 移除指定图片参数
74
+ * @param key 参数名
75
+ */
76
+ removeArg(key: string): void;
77
+ /** 获取图片参数名 */
78
+ getKeys(): Set<string>;
79
+ /**
80
+ * 获取指定的图片参数值
81
+ * @param key 参数名
82
+ */
83
+ getValues(key: string): (string | true)[];
84
+ /**
85
+ * 设置图片参数
86
+ * @param key 参数名
87
+ * @param value 参数值
88
+ * @throws `RangeError` 未定义的图片参数
89
+ */
90
+ setValue(key: string, value?: string | boolean): void;
91
+ /**
92
+ * @override
93
+ * @throws `Error` 不适用于图片
94
+ */
95
+ setLinkText(): never;
96
+ }
@@ -0,0 +1,265 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileToken = void 0;
4
+ const string_1 = require("../../util/string");
5
+ const lint_1 = require("../../util/lint");
6
+ const debug_1 = require("../../util/debug");
7
+ const constants_1 = require("../../util/constants");
8
+ const index_1 = require("../../index");
9
+ const base_1 = require("./base");
10
+ const imageParameter_1 = require("../imageParameter");
11
+ const frame = new Set(['manualthumb', 'frameless', 'framed', 'thumbnail']), horizAlign = new Set(['left', 'right', 'center', 'none']), vertAlign = new Set(['baseline', 'sub', 'super', 'top', 'text-top', 'middle', 'bottom', 'text-bottom']);
12
+ /**
13
+ * a more sophisticated string-explode function
14
+ * @param start start syntax of a nested AST node
15
+ * @param end end syntax of a nested AST node
16
+ * @param separator syntax for explosion
17
+ * @param str string to be exploded
18
+ */
19
+ const explode = (start, end, separator, str) => {
20
+ if (str === undefined) {
21
+ return [];
22
+ }
23
+ const regex = new RegExp(`${[start, end, separator].map(string_1.escapeRegExp).join('|')}`, 'gu'), exploded = [];
24
+ let mt = regex.exec(str), depth = 0, lastIndex = 0;
25
+ while (mt) {
26
+ const { 0: match, index } = mt;
27
+ if (match !== separator) {
28
+ depth += match === start ? 1 : -1;
29
+ }
30
+ else if (depth === 0) {
31
+ exploded.push(str.slice(lastIndex, index));
32
+ ({ lastIndex } = regex);
33
+ }
34
+ mt = regex.exec(str);
35
+ }
36
+ exploded.push(str.slice(lastIndex));
37
+ return exploded;
38
+ };
39
+ /**
40
+ * 图片
41
+ * @classdesc `{childNodes: [AtomToken, ...ImageParameterToken]}`
42
+ */
43
+ class FileToken extends base_1.LinkBaseToken {
44
+ type = 'file';
45
+ /** 图片链接 */
46
+ get link() {
47
+ return this.getArg('link')?.link ?? super.link;
48
+ }
49
+ set link(value) {
50
+ this.setValue('link', value);
51
+ }
52
+ /** 图片大小 */
53
+ get size() {
54
+ return this.getArg('width')?.size;
55
+ }
56
+ set size(size) {
57
+ this.setValue('width', size && `${size.width}${size.height && 'x'}${size.height}`);
58
+ }
59
+ /** 图片宽度 */
60
+ get width() {
61
+ return this.size?.width;
62
+ }
63
+ set width(width) {
64
+ const arg = this.getArg('width');
65
+ if (arg) {
66
+ arg.width = width;
67
+ }
68
+ else {
69
+ this.setValue('width', width);
70
+ }
71
+ }
72
+ /** 图片高度 */
73
+ get height() {
74
+ return this.size?.height;
75
+ }
76
+ set height(height) {
77
+ const arg = this.getArg('width');
78
+ if (arg) {
79
+ arg.height = height;
80
+ }
81
+ else {
82
+ this.setValue('width', height && `x${height}`);
83
+ }
84
+ }
85
+ /* NOT FOR BROWSER END */
86
+ /**
87
+ * @param link 文件名
88
+ * @param text 图片参数
89
+ * @param delimiter `|`
90
+ */
91
+ constructor(link, text, config = index_1.default.getConfig(), accum = [], delimiter = '|') {
92
+ super(link, undefined, config, accum, delimiter);
93
+ /* NOT FOR BROWSER */
94
+ this.setAttribute('acceptable', { AtomToken: 0, ImageParameterToken: '1:' });
95
+ /* NOT FOR BROWSER END */
96
+ this.append(...explode('-{', '}-', '|', text).map(
97
+ // @ts-expect-error abstract class
98
+ part => new imageParameter_1.ImageParameterToken(part, config, accum)));
99
+ }
100
+ /** @override */
101
+ lint(start = this.getAbsoluteIndex()) {
102
+ const errors = super.lint(start), args = this.getAllArgs().filter(({ childNodes }) => {
103
+ const visibleNodes = childNodes.filter(node => node.text().trim());
104
+ return visibleNodes.length !== 1 || visibleNodes[0].type !== 'arg';
105
+ }), keys = [...new Set(args.map(({ name }) => name))].filter(key => key !== 'invalid'), frameKeys = keys.filter(key => frame.has(key)), horizAlignKeys = keys.filter(key => horizAlign.has(key)), vertAlignKeys = keys.filter(key => vertAlign.has(key));
106
+ if (this.closest('ext-link-text') && this.getValue('link')?.trim() !== '') {
107
+ errors.push((0, lint_1.generateForSelf)(this, { start }, 'internal link in an external link'));
108
+ }
109
+ if (args.length === keys.length
110
+ && frameKeys.length < 2
111
+ && horizAlignKeys.length < 2
112
+ && vertAlignKeys.length < 2) {
113
+ return errors;
114
+ }
115
+ const rect = { start, ...this.getRootNode().posFromIndex(start) };
116
+ /**
117
+ * 图片参数到语法错误的映射
118
+ * @param msg 消息键
119
+ * @param p1 替换$1
120
+ */
121
+ const generate = (msg, p1) => (arg) => (0, lint_1.generateForChild)(arg, rect, index_1.default.msg(`${msg} image $1 parameter`, p1));
122
+ for (const key of keys) {
123
+ let relevantArgs = args.filter(({ name }) => name === key);
124
+ if (key === 'caption') {
125
+ relevantArgs = [...relevantArgs.slice(0, -1).filter(arg => arg.text()), ...relevantArgs.slice(-1)];
126
+ }
127
+ if (relevantArgs.length > 1) {
128
+ errors.push(...relevantArgs.map(generate('duplicated', key)));
129
+ }
130
+ }
131
+ if (frameKeys.length > 1) {
132
+ errors.push(...args.filter(({ name }) => frame.has(name)).map(generate('conflicting', 'frame')));
133
+ }
134
+ if (horizAlignKeys.length > 1) {
135
+ errors.push(...args.filter(({ name }) => horizAlign.has(name)).map(generate('conflicting', 'horizontal-alignment')));
136
+ }
137
+ if (vertAlignKeys.length > 1) {
138
+ errors.push(...args.filter(({ name }) => vertAlign.has(name)).map(generate('conflicting', 'vertical-alignment')));
139
+ }
140
+ return errors;
141
+ }
142
+ /** 获取所有图片参数节点 */
143
+ getAllArgs() {
144
+ return this.childNodes.slice(1);
145
+ }
146
+ /**
147
+ * 获取指定图片参数
148
+ * @param key 参数名
149
+ */
150
+ getArgs(key) {
151
+ return this.getAllArgs().filter(({ name }) => key === name);
152
+ }
153
+ /**
154
+ * 获取特定类型的图片属性参数节点
155
+ * @param keys 接受的参数名
156
+ * @param type 类型名
157
+ */
158
+ #getTypedArgs(keys, type) {
159
+ const args = this.getAllArgs().filter(({ name }) => keys.has(name));
160
+ /* NOT FOR BROWSER */
161
+ if (args.length > 1) {
162
+ index_1.default.warn(`图片 ${this.name} 带有 ${args.length} 个${type}参数,只有最后 1 个 ${args[0].name} 会生效!`);
163
+ }
164
+ /* NOT FOR BROWSER END */
165
+ return args;
166
+ }
167
+ /** 获取图片框架属性参数节点 */
168
+ getFrameArgs() {
169
+ return this.#getTypedArgs(frame, '框架');
170
+ }
171
+ /** 获取图片水平对齐参数节点 */
172
+ getHorizAlignArgs() {
173
+ return this.#getTypedArgs(horizAlign, '水平对齐');
174
+ }
175
+ /** 获取图片垂直对齐参数节点 */
176
+ getVertAlignArgs() {
177
+ return this.#getTypedArgs(vertAlign, '垂直对齐');
178
+ }
179
+ /**
180
+ * 获取生效的指定图片参数
181
+ * @param key 参数名
182
+ */
183
+ getArg(key) {
184
+ const args = this.getArgs(key);
185
+ return args[args.length - 1];
186
+ }
187
+ /**
188
+ * 获取生效的指定图片参数值
189
+ * @param key 参数名
190
+ */
191
+ getValue(key) {
192
+ return this.getArg(key)?.getValue();
193
+ }
194
+ /* NOT FOR BROWSER */
195
+ /**
196
+ * 是否具有指定图片参数
197
+ * @param key 参数名
198
+ */
199
+ hasArg(key) {
200
+ return this.getArgs(key).length > 0;
201
+ }
202
+ /**
203
+ * 移除指定图片参数
204
+ * @param key 参数名
205
+ */
206
+ removeArg(key) {
207
+ for (const token of this.getArgs(key)) {
208
+ this.removeChild(token);
209
+ }
210
+ }
211
+ /** 获取图片参数名 */
212
+ getKeys() {
213
+ return new Set(this.getAllArgs().map(({ name }) => name));
214
+ }
215
+ /**
216
+ * 获取指定的图片参数值
217
+ * @param key 参数名
218
+ */
219
+ getValues(key) {
220
+ return this.getArgs(key).map(token => token.getValue());
221
+ }
222
+ /**
223
+ * 设置图片参数
224
+ * @param key 参数名
225
+ * @param value 参数值
226
+ * @throws `RangeError` 未定义的图片参数
227
+ */
228
+ setValue(key, value = false) {
229
+ if (value === false) {
230
+ this.removeArg(key);
231
+ return;
232
+ }
233
+ const token = this.getArg(key);
234
+ if (token) {
235
+ token.setValue(value);
236
+ return;
237
+ }
238
+ const config = this.getAttribute('config'), syntax = key === 'caption' ? '$1' : Object.entries(config.img).find(([, name]) => name === key)?.[0];
239
+ if (syntax === undefined) {
240
+ throw new RangeError(`未定义的图片参数: ${key}`);
241
+ }
242
+ const free = syntax.includes('$1');
243
+ if (value === true && free) {
244
+ this.typeError('setValue', 'Boolean');
245
+ }
246
+ const parameter = debug_1.Shadow.run(
247
+ // @ts-expect-error abstract class
248
+ () => new imageParameter_1.ImageParameterToken(syntax.replace('$1', ''), config));
249
+ if (free) {
250
+ const { childNodes } = index_1.default.parse(value, this.getAttribute('include'), undefined, config);
251
+ parameter.replaceChildren(...childNodes);
252
+ }
253
+ parameter.afterBuild();
254
+ this.insertAt(parameter);
255
+ }
256
+ /**
257
+ * @override
258
+ * @throws `Error` 不适用于图片
259
+ */
260
+ setLinkText() {
261
+ throw new Error('setLinkText 方法不适用于图片!');
262
+ }
263
+ }
264
+ exports.FileToken = FileToken;
265
+ constants_1.classes['FileToken'] = __filename;