wikiparser-node 1.4.1 → 1.4.2-b

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