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