wikiparser-node 0.11.0 → 1.0.0-beta.1

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 (252) hide show
  1. package/config/.schema.json +26 -0
  2. package/dist/index.d.ts +61 -113
  3. package/dist/index.js +314 -0
  4. package/dist/lib/element.d.ts +122 -108
  5. package/dist/lib/element.js +656 -0
  6. package/dist/lib/node.d.ts +115 -221
  7. package/dist/lib/node.js +473 -0
  8. package/dist/lib/ranges.d.ts +27 -26
  9. package/dist/lib/ranges.js +130 -0
  10. package/dist/lib/text.d.ts +36 -28
  11. package/dist/lib/text.js +215 -0
  12. package/dist/lib/title.d.ts +26 -12
  13. package/dist/lib/title.js +108 -0
  14. package/dist/mixin/attributesParent.js +90 -0
  15. package/dist/mixin/fixed.js +29 -0
  16. package/dist/mixin/hidden.js +19 -0
  17. package/dist/mixin/singleLine.js +23 -0
  18. package/dist/mixin/sol.js +41 -0
  19. package/dist/parser/brackets.js +118 -0
  20. package/dist/parser/commentAndExt.js +48 -0
  21. package/dist/parser/converter.js +31 -0
  22. package/dist/parser/externalLinks.js +22 -0
  23. package/dist/parser/hrAndDoubleUnderscore.js +35 -0
  24. package/dist/parser/html.js +29 -0
  25. package/dist/parser/links.js +86 -0
  26. package/dist/parser/list.js +51 -0
  27. package/dist/parser/magicLinks.js +32 -0
  28. package/dist/parser/quotes.js +57 -0
  29. package/dist/parser/selector.js +158 -0
  30. package/dist/parser/table.js +108 -0
  31. package/dist/src/arg.d.ts +47 -23
  32. package/dist/src/arg.js +196 -0
  33. package/dist/src/atom.d.ts +12 -0
  34. package/dist/src/atom.js +22 -0
  35. package/dist/src/attribute.d.ts +74 -33
  36. package/dist/src/attribute.js +433 -0
  37. package/dist/src/attributes.d.ts +61 -55
  38. package/dist/src/attributes.js +371 -0
  39. package/dist/src/converter.d.ts +45 -71
  40. package/dist/src/converter.js +135 -0
  41. package/dist/src/converterFlags.d.ts +64 -39
  42. package/dist/src/converterFlags.js +235 -0
  43. package/dist/src/converterRule.d.ts +49 -27
  44. package/dist/src/converterRule.js +255 -0
  45. package/dist/src/extLink.d.ts +41 -38
  46. package/dist/src/extLink.js +154 -0
  47. package/dist/src/gallery.d.ts +59 -18
  48. package/dist/src/gallery.js +132 -0
  49. package/dist/src/heading.d.ts +60 -22
  50. package/dist/src/heading.js +135 -0
  51. package/dist/src/hidden.d.ts +20 -0
  52. package/dist/src/hidden.js +24 -0
  53. package/dist/src/html.d.ts +83 -29
  54. package/dist/src/html.js +242 -0
  55. package/dist/src/imageParameter.d.ts +59 -40
  56. package/dist/src/imageParameter.js +251 -0
  57. package/dist/src/imagemap.d.ts +65 -21
  58. package/dist/src/imagemap.js +169 -0
  59. package/dist/src/imagemapLink.d.ts +46 -14
  60. package/dist/src/imagemapLink.js +38 -0
  61. package/dist/src/index.d.ts +71 -105
  62. package/dist/src/index.js +826 -0
  63. package/dist/src/link/base.d.ts +71 -0
  64. package/dist/src/link/base.js +225 -0
  65. package/dist/src/link/category.d.ts +10 -11
  66. package/dist/src/link/category.js +26 -0
  67. package/dist/src/link/file.d.ts +61 -39
  68. package/dist/src/link/file.js +242 -0
  69. package/dist/src/link/galleryImage.d.ts +34 -12
  70. package/dist/src/link/galleryImage.js +98 -0
  71. package/dist/src/link/index.d.ts +25 -63
  72. package/dist/src/link/index.js +136 -0
  73. package/dist/src/magicLink.d.ts +22 -15
  74. package/dist/src/magicLink.js +126 -0
  75. package/dist/src/nested.d.ts +47 -0
  76. package/dist/src/nested.js +84 -0
  77. package/dist/src/nowiki/base.d.ts +39 -0
  78. package/dist/src/nowiki/base.js +29 -0
  79. package/dist/src/nowiki/comment.d.ts +31 -20
  80. package/dist/src/nowiki/comment.js +61 -0
  81. package/dist/src/nowiki/dd.d.ts +17 -11
  82. package/dist/src/nowiki/dd.js +50 -0
  83. package/dist/src/nowiki/doubleUnderscore.d.ts +28 -13
  84. package/dist/src/nowiki/doubleUnderscore.js +45 -0
  85. package/dist/src/nowiki/hr.d.ts +28 -10
  86. package/dist/src/nowiki/hr.js +33 -0
  87. package/dist/src/nowiki/index.d.ts +17 -23
  88. package/dist/src/nowiki/index.js +21 -0
  89. package/dist/src/nowiki/list.d.ts +15 -7
  90. package/dist/src/nowiki/list.js +11 -0
  91. package/dist/src/nowiki/noinclude.d.ts +20 -7
  92. package/dist/src/nowiki/noinclude.js +22 -0
  93. package/dist/src/nowiki/quote.d.ts +25 -10
  94. package/dist/src/nowiki/quote.js +55 -0
  95. package/dist/src/onlyinclude.d.ts +28 -12
  96. package/dist/src/onlyinclude.js +64 -0
  97. package/dist/src/paramTag/index.d.ts +40 -17
  98. package/dist/src/paramTag/index.js +76 -0
  99. package/dist/src/paramTag/inputbox.d.ts +8 -7
  100. package/dist/src/paramTag/inputbox.js +19 -0
  101. package/dist/src/parameter.d.ts +62 -41
  102. package/dist/src/parameter.js +201 -0
  103. package/dist/src/pre.d.ts +32 -0
  104. package/dist/src/pre.js +39 -0
  105. package/dist/src/syntax.d.ts +17 -14
  106. package/dist/src/syntax.js +65 -0
  107. package/dist/src/table/base.d.ts +55 -0
  108. package/dist/src/table/base.js +77 -0
  109. package/dist/src/table/index.d.ts +123 -156
  110. package/dist/src/table/index.js +811 -0
  111. package/dist/src/table/td.d.ts +90 -67
  112. package/dist/src/table/td.js +276 -0
  113. package/dist/src/table/tr.d.ts +30 -85
  114. package/dist/src/table/tr.js +48 -0
  115. package/dist/src/table/trBase.d.ts +72 -0
  116. package/dist/src/table/trBase.js +153 -0
  117. package/dist/src/tagPair/ext.d.ts +47 -11
  118. package/dist/src/tagPair/ext.js +127 -0
  119. package/dist/src/tagPair/include.d.ts +32 -13
  120. package/dist/src/tagPair/include.js +40 -0
  121. package/dist/src/tagPair/index.d.ts +44 -29
  122. package/dist/src/tagPair/index.js +111 -0
  123. package/dist/src/transclude.d.ts +88 -85
  124. package/dist/src/transclude.js +739 -0
  125. package/dist/util/base.js +26 -0
  126. package/dist/util/debug.js +52 -0
  127. package/dist/util/diff.js +69 -0
  128. package/dist/util/lint.js +38 -0
  129. package/dist/util/string.js +103 -0
  130. package/errors/README +1 -0
  131. package/i18n/zh-hans.json +1 -0
  132. package/i18n/zh-hant.json +1 -0
  133. package/package.json +21 -24
  134. package/printed/README +1 -0
  135. package/dist/mixin/attributeParent.d.ts +0 -9
  136. package/dist/mixin/fixedToken.d.ts +0 -8
  137. package/dist/mixin/hidden.d.ts +0 -8
  138. package/dist/mixin/singleLine.d.ts +0 -8
  139. package/dist/mixin/sol.d.ts +0 -8
  140. package/dist/parser/brackets.d.ts +0 -12
  141. package/dist/parser/commentAndExt.d.ts +0 -8
  142. package/dist/parser/converter.d.ts +0 -7
  143. package/dist/parser/externalLinks.d.ts +0 -7
  144. package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
  145. package/dist/parser/html.d.ts +0 -7
  146. package/dist/parser/links.d.ts +0 -7
  147. package/dist/parser/list.d.ts +0 -7
  148. package/dist/parser/magicLinks.d.ts +0 -7
  149. package/dist/parser/quotes.d.ts +0 -7
  150. package/dist/parser/selector.d.ts +0 -12
  151. package/dist/parser/table.d.ts +0 -11
  152. package/dist/src/atom/hidden.d.ts +0 -5
  153. package/dist/src/atom/index.d.ts +0 -15
  154. package/dist/src/charinsert.d.ts +0 -32
  155. package/dist/src/hasNowiki/index.d.ts +0 -14
  156. package/dist/src/hasNowiki/pre.d.ts +0 -13
  157. package/dist/src/nested/choose.d.ts +0 -13
  158. package/dist/src/nested/combobox.d.ts +0 -13
  159. package/dist/src/nested/index.d.ts +0 -18
  160. package/dist/src/nested/references.d.ts +0 -13
  161. package/dist/tool/index.d.ts +0 -420
  162. package/dist/util/base.d.ts +0 -10
  163. package/dist/util/debug.d.ts +0 -20
  164. package/dist/util/diff.d.ts +0 -8
  165. package/dist/util/lint.d.ts +0 -28
  166. package/dist/util/string.d.ts +0 -55
  167. package/index.js +0 -333
  168. package/lib/element.js +0 -618
  169. package/lib/node.js +0 -730
  170. package/lib/ranges.js +0 -130
  171. package/lib/text.js +0 -265
  172. package/lib/title.js +0 -83
  173. package/mixin/attributeParent.js +0 -117
  174. package/mixin/fixedToken.js +0 -40
  175. package/mixin/hidden.js +0 -21
  176. package/mixin/singleLine.js +0 -31
  177. package/mixin/sol.js +0 -54
  178. package/parser/brackets.js +0 -128
  179. package/parser/commentAndExt.js +0 -62
  180. package/parser/converter.js +0 -46
  181. package/parser/externalLinks.js +0 -33
  182. package/parser/hrAndDoubleUnderscore.js +0 -49
  183. package/parser/html.js +0 -42
  184. package/parser/links.js +0 -94
  185. package/parser/list.js +0 -59
  186. package/parser/magicLinks.js +0 -41
  187. package/parser/quotes.js +0 -64
  188. package/parser/selector.js +0 -180
  189. package/parser/table.js +0 -114
  190. package/src/arg.js +0 -207
  191. package/src/atom/hidden.js +0 -13
  192. package/src/atom/index.js +0 -43
  193. package/src/attribute.js +0 -472
  194. package/src/attributes.js +0 -453
  195. package/src/charinsert.js +0 -97
  196. package/src/converter.js +0 -176
  197. package/src/converterFlags.js +0 -284
  198. package/src/converterRule.js +0 -256
  199. package/src/extLink.js +0 -180
  200. package/src/gallery.js +0 -149
  201. package/src/hasNowiki/index.js +0 -44
  202. package/src/hasNowiki/pre.js +0 -40
  203. package/src/heading.js +0 -134
  204. package/src/html.js +0 -254
  205. package/src/imageParameter.js +0 -303
  206. package/src/imagemap.js +0 -199
  207. package/src/imagemapLink.js +0 -41
  208. package/src/index.js +0 -938
  209. package/src/link/category.js +0 -44
  210. package/src/link/file.js +0 -287
  211. package/src/link/galleryImage.js +0 -120
  212. package/src/link/index.js +0 -388
  213. package/src/magicLink.js +0 -151
  214. package/src/nested/choose.js +0 -24
  215. package/src/nested/combobox.js +0 -23
  216. package/src/nested/index.js +0 -96
  217. package/src/nested/references.js +0 -23
  218. package/src/nowiki/comment.js +0 -71
  219. package/src/nowiki/dd.js +0 -59
  220. package/src/nowiki/doubleUnderscore.js +0 -56
  221. package/src/nowiki/hr.js +0 -41
  222. package/src/nowiki/index.js +0 -56
  223. package/src/nowiki/list.js +0 -16
  224. package/src/nowiki/noinclude.js +0 -28
  225. package/src/nowiki/quote.js +0 -69
  226. package/src/onlyinclude.js +0 -64
  227. package/src/paramTag/index.js +0 -89
  228. package/src/paramTag/inputbox.js +0 -35
  229. package/src/parameter.js +0 -239
  230. package/src/syntax.js +0 -91
  231. package/src/table/index.js +0 -985
  232. package/src/table/td.js +0 -343
  233. package/src/table/tr.js +0 -319
  234. package/src/tagPair/ext.js +0 -146
  235. package/src/tagPair/include.js +0 -50
  236. package/src/tagPair/index.js +0 -131
  237. package/src/transclude.js +0 -843
  238. package/tool/index.js +0 -1209
  239. package/typings/api.d.ts +0 -9
  240. package/typings/array.d.ts +0 -29
  241. package/typings/event.d.ts +0 -22
  242. package/typings/index.d.ts +0 -118
  243. package/typings/node.d.ts +0 -35
  244. package/typings/parser.d.ts +0 -12
  245. package/typings/table.d.ts +0 -10
  246. package/typings/token.d.ts +0 -31
  247. package/typings/tool.d.ts +0 -6
  248. package/util/base.js +0 -17
  249. package/util/debug.js +0 -73
  250. package/util/diff.js +0 -76
  251. package/util/lint.js +0 -57
  252. package/util/string.js +0 -126
@@ -1,23 +1,31 @@
1
- export = MagicLinkToken;
1
+ import * as Parser from '../index';
2
+ import Token = require('.');
2
3
  /**
3
4
  * 自由外链
4
- * @classdesc `{childNodes: [...AstText|CommentToken|IncludeToken|NoincludeToken]}`
5
+ * @classdesc `{childNodes: ...AstText|CommentToken|IncludeToken|NoincludeToken}`
5
6
  */
6
7
  declare class MagicLinkToken extends Token {
7
- /**
8
- * @param {string} url 网址
9
- * @param {boolean} doubleSlash 是否接受"//"作为协议
10
- * @param {import('../../typings/token').accum} accum
11
- */
12
- constructor(url: string, doubleSlash: boolean, config?: import("../../typings/token").ParserConfig, accum?: import('../../typings/token').accum);
13
- set protocol(arg: string);
8
+ #private;
9
+ type: 'free-ext-link' | 'ext-link-url';
14
10
  /** 协议 */
15
- get protocol(): string;
16
- set link(arg: string);
11
+ get protocol(): string | undefined;
12
+ set protocol(value: string | undefined);
17
13
  /** 和内链保持一致 */
18
14
  get link(): string;
15
+ set link(url: string);
16
+ /**
17
+ * @browser
18
+ * @param url 网址
19
+ * @param doubleSlash 是否接受"//"作为协议
20
+ */
21
+ constructor(url: string | undefined, doubleSlash: boolean, config?: Parser.Config, accum?: Token[]);
22
+ /**
23
+ * @override
24
+ * @browser
25
+ */
26
+ lint(start?: number): Parser.LintError[];
19
27
  /** @override */
20
- override cloneNode(): MagicLinkToken;
28
+ cloneNode(): this;
21
29
  /**
22
30
  * 获取网址
23
31
  * @throws `Error` 非标准协议
@@ -25,12 +33,11 @@ declare class MagicLinkToken extends Token {
25
33
  getUrl(): URL;
26
34
  /**
27
35
  * 设置外链目标
28
- * @param {string|URL} url 含协议的网址
36
+ * @param url 含协议的网址
29
37
  * @throws `SyntaxError` 非法的自由外链目标
30
38
  */
31
39
  setTarget(url: string | URL): void;
32
40
  /** 是否是模板或魔术字参数 */
33
41
  isParamValue(): boolean;
34
- #private;
35
42
  }
36
- import Token = require(".");
43
+ export = MagicLinkToken;
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ const lint_1 = require("../util/lint");
3
+ const Parser = require("../index");
4
+ const Token = require(".");
5
+ /**
6
+ * 自由外链
7
+ * @classdesc `{childNodes: ...AstText|CommentToken|IncludeToken|NoincludeToken}`
8
+ */
9
+ class MagicLinkToken extends Token {
10
+ #protocolRegex;
11
+ /** 协议 */
12
+ get protocol() {
13
+ return this.#protocolRegex.exec(this.text())?.[0];
14
+ }
15
+ set protocol(value) {
16
+ if (typeof value !== 'string') {
17
+ this.typeError('protocol', 'String');
18
+ }
19
+ else if (!new RegExp(`${this.#protocolRegex.source}$`, 'iu').test(value)) {
20
+ throw new RangeError(`非法的外链协议:${value}`);
21
+ }
22
+ const { link } = this;
23
+ if (!this.#protocolRegex.test(link)) {
24
+ throw new Error(`特殊外链无法更改协议:${link}`);
25
+ }
26
+ this.replaceChildren(link.replace(this.#protocolRegex, value));
27
+ }
28
+ /** 和内链保持一致 */
29
+ get link() {
30
+ return this.text();
31
+ }
32
+ set link(url) {
33
+ this.setTarget(url);
34
+ }
35
+ /**
36
+ * @browser
37
+ * @param url 网址
38
+ * @param doubleSlash 是否接受"//"作为协议
39
+ */
40
+ constructor(url, doubleSlash, config = Parser.getConfig(), accum = []) {
41
+ super(url, config, true, accum, {
42
+ 'Stage-1': ':', '!ExtToken': '',
43
+ });
44
+ this.type = doubleSlash ? 'ext-link-url' : 'free-ext-link';
45
+ this.#protocolRegex = new RegExp(`^(?:${config.protocol}${doubleSlash ? '|//' : ''})`, 'iu');
46
+ }
47
+ /**
48
+ * @override
49
+ * @browser
50
+ */
51
+ lint(start = this.getAbsoluteIndex()) {
52
+ const errors = super.lint(start), source = `[,;。:!?()]+${this.type === 'ext-link-url' ? '|\\|+' : ''}`, regex = new RegExp(source, 'u'), regexGlobal = new RegExp(source, 'gu');
53
+ let rect;
54
+ for (const child of this.childNodes) {
55
+ const str = String(child);
56
+ if (child.type !== 'text' || !regex.test(str)) {
57
+ continue;
58
+ }
59
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
60
+ const refError = (0, lint_1.generateForChild)(child, rect, '', 'warning');
61
+ errors.push(...[...str.matchAll(regexGlobal)].map(({ index, 0: s }) => {
62
+ const lines = str.slice(0, index).split('\n'), { length: top } = lines, { length: left } = lines.at(-1), startIndex = start + index, startLine = refError.startLine + top - 1, startCol = (top > 1 ? 0 : refError.startCol) + left;
63
+ return {
64
+ ...refError,
65
+ message: Parser.msg('$1 in URL', s.startsWith('|') ? '"|"' : Parser.msg('full-width punctuation')),
66
+ startIndex,
67
+ endIndex: startIndex + s.length,
68
+ startLine,
69
+ endLine: startLine,
70
+ startCol,
71
+ endCol: startCol + s.length,
72
+ excerpt: str.slice(Math.max(0, index - 25), index + 25),
73
+ };
74
+ }));
75
+ }
76
+ return errors;
77
+ }
78
+ /** @override */
79
+ cloneNode() {
80
+ const cloned = this.cloneChildNodes();
81
+ return Parser.run(() => {
82
+ const token = new MagicLinkToken(undefined, this.type === 'ext-link-url', this.getAttribute('config'));
83
+ token.append(...cloned);
84
+ token.afterBuild();
85
+ return token;
86
+ });
87
+ }
88
+ /**
89
+ * 获取网址
90
+ * @throws `Error` 非标准协议
91
+ */
92
+ getUrl() {
93
+ let url = this.text();
94
+ if (url.startsWith('//')) {
95
+ url = `https:${url}`;
96
+ }
97
+ try {
98
+ return new URL(url);
99
+ }
100
+ catch (e) {
101
+ if (e instanceof TypeError && e.message === 'Invalid URL') {
102
+ throw new Error(`非标准协议的外部链接:${url}`);
103
+ }
104
+ throw e;
105
+ }
106
+ }
107
+ /**
108
+ * 设置外链目标
109
+ * @param url 含协议的网址
110
+ * @throws `SyntaxError` 非法的自由外链目标
111
+ */
112
+ setTarget(url) {
113
+ const strUrl = String(url), root = Parser.parse(strUrl, this.getAttribute('include'), 9, this.getAttribute('config')), { length, firstChild: freeExtLink } = root;
114
+ if (length !== 1 || freeExtLink.type !== 'free-ext-link') {
115
+ throw new SyntaxError(`非法的自由外链目标:${strUrl}`);
116
+ }
117
+ this.replaceChildren(...freeExtLink.childNodes);
118
+ }
119
+ /** 是否是模板或魔术字参数 */
120
+ isParamValue() {
121
+ const parameter = this.closest('parameter');
122
+ return parameter?.getValue() === this.text();
123
+ }
124
+ }
125
+ Parser.classes['MagicLinkToken'] = __filename;
126
+ module.exports = MagicLinkToken;
@@ -0,0 +1,47 @@
1
+ import * as Parser from '../index';
2
+ import Token = require('.');
3
+ import ExtToken = require('./tagPair/ext');
4
+ import NoincludeToken = require('./nowiki/noinclude');
5
+ import CommentToken = require('./nowiki/comment');
6
+ import AttributesToken = require('./attributes');
7
+ /**
8
+ * 嵌套式的扩展标签
9
+ * @classdesc `{childNodes: ...ExtToken|NoincludeToken|CommentToken}`
10
+ */
11
+ declare abstract class NestedToken extends Token {
12
+ #private;
13
+ /** @browser */
14
+ readonly type = "ext-inner";
15
+ childNodes: (ExtToken | NoincludeToken | CommentToken)[];
16
+ abstract get children(): (ExtToken | NoincludeToken | CommentToken)[];
17
+ abstract get firstChild(): ExtToken | NoincludeToken | CommentToken | undefined;
18
+ abstract get firstElementChild(): ExtToken | NoincludeToken | CommentToken | undefined;
19
+ abstract get lastChild(): ExtToken | NoincludeToken | CommentToken | undefined;
20
+ abstract get lastElementChild(): ExtToken | NoincludeToken | CommentToken | undefined;
21
+ abstract get nextSibling(): undefined;
22
+ abstract get nextElementSibling(): undefined;
23
+ abstract get previousSibling(): AttributesToken;
24
+ abstract get previousElementSibling(): AttributesToken;
25
+ abstract get parentNode(): ExtToken | undefined;
26
+ abstract get parentElement(): ExtToken | undefined;
27
+ /**
28
+ * @browser
29
+ * @param regex 内层正则
30
+ * @param tags 内层标签名
31
+ */
32
+ constructor(wikitext: string | undefined, regex: RegExp, tags: string[], config?: Parser.Config, accum?: Token[]);
33
+ /**
34
+ * @override
35
+ * @browser
36
+ */
37
+ lint(start?: number): Parser.LintError[];
38
+ /**
39
+ * @override
40
+ * @param token 待插入的子节点
41
+ * @param i 插入位置
42
+ */
43
+ insertAt<T extends Token>(token: T, i?: number): T;
44
+ /** @override */
45
+ cloneNode(): this;
46
+ }
47
+ export = NestedToken;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ const lint_1 = require("../util/lint");
3
+ const Parser = require("../index");
4
+ const Token = require(".");
5
+ const ExtToken = require("./tagPair/ext");
6
+ const NoincludeToken = require("./nowiki/noinclude");
7
+ const CommentToken = require("./nowiki/comment");
8
+ /**
9
+ * 嵌套式的扩展标签
10
+ * @classdesc `{childNodes: ...ExtToken|NoincludeToken|CommentToken}`
11
+ */
12
+ class NestedToken extends Token {
13
+ /** @browser */
14
+ type = 'ext-inner';
15
+ #tags;
16
+ #regex;
17
+ /**
18
+ * @browser
19
+ * @param regex 内层正则
20
+ * @param tags 内层标签名
21
+ */
22
+ constructor(wikitext, regex, tags, config = Parser.getConfig(), accum = []) {
23
+ const text = wikitext?.replace(regex, (comment, name, attr, inner, closing) => {
24
+ const str = `\0${accum.length + 1}${name ? 'e' : 'c'}\x7F`;
25
+ if (name) {
26
+ new ExtToken(name, attr, inner, closing, config, accum);
27
+ }
28
+ else {
29
+ const closed = comment.endsWith('-->');
30
+ new CommentToken(comment.slice(4, closed ? -3 : undefined), closed, config, accum);
31
+ }
32
+ return str;
33
+ })?.replace(/(?<=^|\0\d+[ce]\x7F)[^\0]+(?=$|\0\d+[ce]\x7F)/gu, substr => {
34
+ new NoincludeToken(substr, config, accum);
35
+ return `\0${accum.length}c\x7F`;
36
+ });
37
+ super(text, config, true, accum, {
38
+ NoincludeToken: ':', ExtToken: ':',
39
+ });
40
+ this.#tags = tags;
41
+ this.#regex = regex;
42
+ }
43
+ /**
44
+ * @override
45
+ * @browser
46
+ */
47
+ lint(start = this.getAbsoluteIndex()) {
48
+ let rect;
49
+ return [
50
+ ...super.lint(start),
51
+ ...this.childNodes.filter(child => {
52
+ if (child.type === 'ext' || child.type === 'comment') {
53
+ return false;
54
+ }
55
+ const str = String(child).trim();
56
+ return str && !/^<!--.*-->$/su.test(str);
57
+ }).map(child => {
58
+ rect ??= { start, ...this.getRootNode().posFromIndex(start) };
59
+ return (0, lint_1.generateForChild)(child, rect, Parser.msg('invalid content in <$1>', this.name));
60
+ }),
61
+ ];
62
+ }
63
+ /**
64
+ * @override
65
+ * @param token 待插入的子节点
66
+ * @param i 插入位置
67
+ */
68
+ insertAt(token, i = this.length) {
69
+ return typeof token !== 'string' && token.type === 'ext' && !this.#tags.includes(token.name)
70
+ ? this.typeError(`${this.constructor.name}只能以${this.#tags.join('或')}标签作为子节点!`)
71
+ : super.insertAt(token, i);
72
+ }
73
+ /** @override */
74
+ cloneNode() {
75
+ const cloned = this.cloneChildNodes(), config = this.getAttribute('config');
76
+ return Parser.run(() => {
77
+ const token = new NestedToken(undefined, this.#regex, this.#tags, config);
78
+ token.append(...cloned);
79
+ return token;
80
+ });
81
+ }
82
+ }
83
+ Parser.classes['NestedToken'] = __filename;
84
+ module.exports = NestedToken;
@@ -0,0 +1,39 @@
1
+ import * as Parser from '../../index';
2
+ import Token = require('..');
3
+ import AstText = require('../../lib/text');
4
+ declare type NowikiTypes = 'ext-inner' | 'comment' | 'dd' | 'double-underscore' | 'hr' | 'list' | 'noinclude' | 'quote';
5
+ declare const NowikiBaseToken_base: ((abstract new (...args: any[]) => {
6
+ removeAt(): never;
7
+ insertAt(token: string, i?: number | undefined): AstText;
8
+ insertAt<T extends import("../../lib/node").AstNodeTypes>(token: T, i?: number | undefined): T;
9
+ length: number;
10
+ toString(selector?: string | undefined, separator?: string | undefined): string;
11
+ text(separator?: string | undefined): string;
12
+ }) & {
13
+ readonly fixed: true;
14
+ }) & typeof Token;
15
+ /**
16
+ * 纯文字Token,不会被解析
17
+ * @classdesc `{childNodes: [AstText]}`
18
+ */
19
+ declare abstract class NowikiBaseToken extends NowikiBaseToken_base {
20
+ type: NowikiTypes;
21
+ childNodes: [AstText];
22
+ abstract get children(): [];
23
+ abstract get firstChild(): AstText;
24
+ abstract get firstElementChild(): undefined;
25
+ abstract get lastChild(): AstText;
26
+ abstract get lastElementChild(): undefined;
27
+ /** @browser */
28
+ constructor(wikitext: string, config?: Parser.Config, accum?: Token[]);
29
+ /** @override */
30
+ cloneNode(this: this & {
31
+ constructor: new (...args: unknown[]) => unknown;
32
+ }): this;
33
+ /**
34
+ * @override
35
+ * @param str 新文本
36
+ */
37
+ setText(str: string): string;
38
+ }
39
+ export = NowikiBaseToken;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ const fixed = require("../../mixin/fixed");
3
+ const Parser = require("../../index");
4
+ const Token = require("..");
5
+ /**
6
+ * 纯文字Token,不会被解析
7
+ * @classdesc `{childNodes: [AstText]}`
8
+ */
9
+ class NowikiBaseToken extends fixed(Token) {
10
+ /** @browser */
11
+ constructor(wikitext, config = Parser.getConfig(), accum = []) {
12
+ super(wikitext, config, true, accum);
13
+ }
14
+ /** @override */
15
+ cloneNode() {
16
+ const { constructor, firstChild: { data }, type } = this, token = Parser.run(() => new constructor(data, this.getAttribute('config')));
17
+ token.type = type;
18
+ return token;
19
+ }
20
+ /**
21
+ * @override
22
+ * @param str 新文本
23
+ */
24
+ setText(str) {
25
+ return super.setText(str, 0);
26
+ }
27
+ }
28
+ Parser.classes['NowikiBaseToken'] = __filename;
29
+ module.exports = NowikiBaseToken;
@@ -1,31 +1,42 @@
1
- export = CommentToken;
2
- /**
3
- * HTML注释,不可见
4
- * @classdesc `{childNodes: [AstText]}`
5
- */
6
- declare class CommentToken extends NowikiToken {
7
- /**
8
- * @param {string} wikitext wikitext
9
- * @param {boolean} closed 是否闭合
10
- * @param {import('../../../typings/token').accum} accum
11
- */
12
- constructor(wikitext: string, closed?: boolean, config?: import("../../../typings/token").ParserConfig, accum?: import('../../../typings/token').accum);
1
+ import * as Parser from '../../index';
2
+ import Token = require('..');
3
+ import NowikiBaseToken = require('./base');
4
+ declare const CommentToken_base: ((abstract new (...args: any[]) => {
5
+ text(): string;
6
+ length: number;
7
+ toString(selector?: string | undefined, separator?: string | undefined): string;
8
+ insertAt(token: unknown, i?: number | undefined): unknown;
9
+ }) & {
10
+ readonly hidden: true;
11
+ }) & typeof NowikiBaseToken;
12
+ /** HTML注释,不可见 */
13
+ declare abstract class CommentToken extends CommentToken_base {
14
+ /** @browser */
15
+ readonly type = "comment";
13
16
  closed: boolean;
14
17
  /** 内部wikitext */
15
18
  get innerText(): string;
16
- /** @override */
17
- override print(): string;
19
+ /**
20
+ * @browser
21
+ * @param closed 是否闭合
22
+ */
23
+ constructor(wikitext: string, closed?: boolean, config?: Parser.Config, accum?: Token[]);
18
24
  /**
19
25
  * @override
20
- * @param {number} start 起始位置
26
+ * @browser
21
27
  */
22
- override lint(start?: number): any;
28
+ print(): string;
23
29
  /**
24
30
  * @override
25
- * @param {string} selector
31
+ * @browser
26
32
  */
27
- override toString(selector: string): string;
33
+ lint(start?: number): Parser.LintError[];
34
+ /**
35
+ * @override
36
+ * @browser
37
+ */
38
+ toString(selector?: string): string;
28
39
  /** @override */
29
- override cloneNode(): CommentToken;
40
+ cloneNode(): this;
30
41
  }
31
- import NowikiToken = require(".");
42
+ export = CommentToken;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ const hidden = require("../../mixin/hidden");
3
+ const lint_1 = require("../../util/lint");
4
+ const Parser = require("../../index");
5
+ const NowikiBaseToken = require("./base");
6
+ /** HTML注释,不可见 */
7
+ class CommentToken extends hidden(NowikiBaseToken) {
8
+ /** @browser */
9
+ type = 'comment';
10
+ closed;
11
+ /** 内部wikitext */
12
+ get innerText() {
13
+ return this.firstChild.data;
14
+ }
15
+ /**
16
+ * @browser
17
+ * @param closed 是否闭合
18
+ */
19
+ constructor(wikitext, closed = true, config = Parser.getConfig(), accum = []) {
20
+ super(wikitext, config, accum);
21
+ this.closed = closed;
22
+ Object.defineProperty(this, 'closed', { enumerable: false });
23
+ }
24
+ /** @private */
25
+ getPadding() {
26
+ return 4;
27
+ }
28
+ /**
29
+ * @override
30
+ * @browser
31
+ */
32
+ print() {
33
+ return super.print({ pre: '&lt;!--', post: this.closed ? '--&gt;' : '' });
34
+ }
35
+ /**
36
+ * @override
37
+ * @browser
38
+ */
39
+ lint(start = this.getAbsoluteIndex()) {
40
+ return this.closed ? [] : [(0, lint_1.generateForSelf)(this, { start }, 'unclosed HTML comment')];
41
+ }
42
+ /**
43
+ * @override
44
+ * @browser
45
+ */
46
+ toString(selector) {
47
+ if (!this.closed && this.nextSibling) {
48
+ Parser.error('自动闭合HTML注释', this);
49
+ this.closed = true;
50
+ }
51
+ return selector && this.matches(selector)
52
+ ? ''
53
+ : `<!--${this.firstChild.data}${this.closed ? '-->' : ''}`;
54
+ }
55
+ /** @override */
56
+ cloneNode() {
57
+ return Parser.run(() => new CommentToken(this.firstChild.data, this.closed, this.getAttribute('config')));
58
+ }
59
+ }
60
+ Parser.classes['CommentToken'] = __filename;
61
+ module.exports = CommentToken;
@@ -1,17 +1,23 @@
1
- export = DdToken;
2
- /**
3
- * :
4
- * @classdesc `{childNodes: [AstText]}`
5
- */
6
- declare class DdToken extends NowikiToken {
7
- /** 是否包含<dt> */
1
+ import NowikiBaseToken = require('./base');
2
+ /** `:` */
3
+ declare abstract class DdToken extends NowikiBaseToken {
4
+ /** @browser */
5
+ readonly type: 'dd' | 'list';
6
+ /** 是否包含`;` */
8
7
  get dt(): boolean;
9
- /** 是否包含<ul> */
8
+ /** 是否包含`*` */
10
9
  get ul(): boolean;
11
- /** 是否包含<ol> */
10
+ /** 是否包含`#` */
12
11
  get ol(): boolean;
13
- set indent(arg: number);
14
12
  /** 缩进数 */
15
13
  get indent(): number;
14
+ /** @throws `RangeError` indent不是自然数 */
15
+ set indent(indent: number);
16
+ /**
17
+ * @override
18
+ * @param str 新文本
19
+ * @throws `RangeError` 错误的列表语法
20
+ */
21
+ setText(str: string): string;
16
22
  }
17
- import NowikiToken = require(".");
23
+ export = DdToken;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ const Parser = require("../../index");
3
+ const NowikiBaseToken = require("./base");
4
+ /** `:` */
5
+ class DdToken extends NowikiBaseToken {
6
+ /** @browser */
7
+ type = 'dd';
8
+ /** 是否包含`;` */
9
+ get dt() {
10
+ return this.firstChild.data.includes(';');
11
+ }
12
+ /** 是否包含`*` */
13
+ get ul() {
14
+ return this.firstChild.data.includes('*');
15
+ }
16
+ /** 是否包含`#` */
17
+ get ol() {
18
+ return this.firstChild.data.includes('#');
19
+ }
20
+ /** 缩进数 */
21
+ get indent() {
22
+ return this.firstChild.data.split(':').length - 1;
23
+ }
24
+ /** @throws `RangeError` indent不是自然数 */
25
+ set indent(indent) {
26
+ if (this.type === 'dd') {
27
+ if (!Number.isInteger(indent)) {
28
+ this.typeError('indent setter', 'Number');
29
+ }
30
+ else if (indent < 0) {
31
+ throw new RangeError('indent 应为自然数!');
32
+ }
33
+ this.setText(':'.repeat(indent));
34
+ }
35
+ }
36
+ /**
37
+ * @override
38
+ * @param str 新文本
39
+ * @throws `RangeError` 错误的列表语法
40
+ */
41
+ setText(str) {
42
+ const src = this.type === 'dd' ? ':' : ';:*#';
43
+ if (new RegExp(`[^${src}]`, 'u').test(str)) {
44
+ throw new RangeError(`${this.constructor.name} 仅能包含${[...src].map(c => `"${c}"`).join('、')}!`);
45
+ }
46
+ return super.setText(str);
47
+ }
48
+ }
49
+ Parser.classes['DdToken'] = __filename;
50
+ module.exports = DdToken;
@@ -1,22 +1,37 @@
1
- export = DoubleUnderscoreToken;
2
- /**
3
- * 状态开关
4
- * @classdesc `{childNodes: [AstText]}`
5
- */
6
- declare class DoubleUnderscoreToken extends NowikiToken {
7
- /** @override */
8
- override print(): string;
1
+ import * as Parser from '../../index';
2
+ import NowikiBaseToken = require('./base');
3
+ import Token = require('..');
4
+ declare const DoubleUnderscoreToken_base: ((abstract new (...args: any[]) => {
5
+ text(): string;
6
+ length: number;
7
+ toString(selector?: string | undefined, separator?: string | undefined): string;
8
+ insertAt(token: unknown, i?: number | undefined): unknown;
9
+ }) & {
10
+ readonly hidden: true;
11
+ }) & typeof NowikiBaseToken;
12
+ /** 状态开关 */
13
+ declare abstract class DoubleUnderscoreToken extends DoubleUnderscoreToken_base {
14
+ /** @browser */
15
+ readonly type = "double-underscore";
16
+ name: string;
17
+ /**
18
+ * @override
19
+ * @browser
20
+ */
21
+ print(): string;
9
22
  /**
10
23
  * @override
11
- * @param {string} selector
24
+ * @browser
12
25
  */
13
- override toString(selector: string): string;
26
+ toString(selector?: string): string;
27
+ /** @param word 状态开关名 */
28
+ constructor(word: string, config?: Parser.Config, accum?: Token[]);
14
29
  /** @override */
15
- override cloneNode(): DoubleUnderscoreToken;
30
+ cloneNode(): this;
16
31
  /**
17
32
  * @override
18
33
  * @throws `Error` 禁止修改
19
34
  */
20
- override setText(): void;
35
+ setText(): never;
21
36
  }
22
- import NowikiToken = require(".");
37
+ export = DoubleUnderscoreToken;