wikiparser-node 0.11.0 → 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +59 -113
- package/dist/index.js +307 -0
- package/dist/lib/element.d.ts +122 -108
- package/dist/lib/element.js +656 -0
- package/dist/lib/node.d.ts +115 -221
- package/dist/lib/node.js +473 -0
- package/dist/lib/ranges.d.ts +27 -26
- package/dist/lib/ranges.js +130 -0
- package/dist/lib/text.d.ts +36 -28
- package/dist/lib/text.js +215 -0
- package/dist/lib/title.d.ts +25 -12
- package/dist/lib/title.js +100 -0
- package/dist/mixin/attributesParent.js +90 -0
- package/dist/mixin/fixed.js +29 -0
- package/dist/mixin/hidden.js +19 -0
- package/dist/mixin/singleLine.js +23 -0
- package/dist/mixin/sol.js +41 -0
- package/dist/parser/brackets.js +117 -0
- package/dist/parser/commentAndExt.js +48 -0
- package/dist/parser/converter.js +31 -0
- package/dist/parser/externalLinks.js +22 -0
- package/dist/parser/hrAndDoubleUnderscore.js +33 -0
- package/dist/parser/html.js +29 -0
- package/dist/parser/links.js +86 -0
- package/dist/parser/list.js +51 -0
- package/dist/parser/magicLinks.js +32 -0
- package/dist/parser/quotes.js +57 -0
- package/dist/parser/selector.js +158 -0
- package/dist/parser/table.js +108 -0
- package/dist/src/arg.d.ts +47 -23
- package/dist/src/arg.js +196 -0
- package/dist/src/atom.d.ts +12 -0
- package/dist/src/atom.js +22 -0
- package/dist/src/attribute.d.ts +74 -33
- package/dist/src/attribute.js +433 -0
- package/dist/src/attributes.d.ts +61 -55
- package/dist/src/attributes.js +371 -0
- package/dist/src/converter.d.ts +45 -71
- package/dist/src/converter.js +135 -0
- package/dist/src/converterFlags.d.ts +64 -39
- package/dist/src/converterFlags.js +235 -0
- package/dist/src/converterRule.d.ts +49 -27
- package/dist/src/converterRule.js +255 -0
- package/dist/src/extLink.d.ts +41 -38
- package/dist/src/extLink.js +154 -0
- package/dist/src/gallery.d.ts +59 -18
- package/dist/src/gallery.js +132 -0
- package/dist/src/heading.d.ts +60 -22
- package/dist/src/heading.js +135 -0
- package/dist/src/hidden.d.ts +20 -0
- package/dist/src/hidden.js +24 -0
- package/dist/src/html.d.ts +83 -29
- package/dist/src/html.js +242 -0
- package/dist/src/imageParameter.d.ts +59 -40
- package/dist/src/imageParameter.js +251 -0
- package/dist/src/imagemap.d.ts +65 -21
- package/dist/src/imagemap.js +164 -0
- package/dist/src/imagemapLink.d.ts +46 -14
- package/dist/src/imagemapLink.js +38 -0
- package/dist/src/index.d.ts +71 -105
- package/dist/src/index.js +826 -0
- package/dist/src/link/base.d.ts +71 -0
- package/dist/src/link/base.js +225 -0
- package/dist/src/link/category.d.ts +10 -11
- package/dist/src/link/category.js +26 -0
- package/dist/src/link/file.d.ts +61 -39
- package/dist/src/link/file.js +242 -0
- package/dist/src/link/galleryImage.d.ts +34 -12
- package/dist/src/link/galleryImage.js +98 -0
- package/dist/src/link/index.d.ts +25 -63
- package/dist/src/link/index.js +136 -0
- package/dist/src/magicLink.d.ts +22 -15
- package/dist/src/magicLink.js +126 -0
- package/dist/src/nested.d.ts +47 -0
- package/dist/src/nested.js +84 -0
- package/dist/src/nowiki/base.d.ts +39 -0
- package/dist/src/nowiki/base.js +29 -0
- package/dist/src/nowiki/comment.d.ts +31 -20
- package/dist/src/nowiki/comment.js +61 -0
- package/dist/src/nowiki/dd.d.ts +17 -11
- package/dist/src/nowiki/dd.js +50 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +28 -13
- package/dist/src/nowiki/doubleUnderscore.js +45 -0
- package/dist/src/nowiki/hr.d.ts +28 -10
- package/dist/src/nowiki/hr.js +33 -0
- package/dist/src/nowiki/index.d.ts +17 -23
- package/dist/src/nowiki/index.js +21 -0
- package/dist/src/nowiki/list.d.ts +15 -7
- package/dist/src/nowiki/list.js +11 -0
- package/dist/src/nowiki/noinclude.d.ts +20 -7
- package/dist/src/nowiki/noinclude.js +22 -0
- package/dist/src/nowiki/quote.d.ts +25 -10
- package/dist/src/nowiki/quote.js +55 -0
- package/dist/src/onlyinclude.d.ts +28 -12
- package/dist/src/onlyinclude.js +64 -0
- package/dist/src/paramTag/index.d.ts +40 -17
- package/dist/src/paramTag/index.js +76 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -7
- package/dist/src/paramTag/inputbox.js +19 -0
- package/dist/src/parameter.d.ts +62 -41
- package/dist/src/parameter.js +201 -0
- package/dist/src/pre.d.ts +32 -0
- package/dist/src/pre.js +39 -0
- package/dist/src/syntax.d.ts +17 -14
- package/dist/src/syntax.js +65 -0
- package/dist/src/table/base.d.ts +55 -0
- package/dist/src/table/base.js +77 -0
- package/dist/src/table/index.d.ts +123 -156
- package/dist/src/table/index.js +811 -0
- package/dist/src/table/td.d.ts +90 -67
- package/dist/src/table/td.js +276 -0
- package/dist/src/table/tr.d.ts +30 -85
- package/dist/src/table/tr.js +48 -0
- package/dist/src/table/trBase.d.ts +72 -0
- package/dist/src/table/trBase.js +153 -0
- package/dist/src/tagPair/ext.d.ts +47 -11
- package/dist/src/tagPair/ext.js +127 -0
- package/dist/src/tagPair/include.d.ts +32 -13
- package/dist/src/tagPair/include.js +40 -0
- package/dist/src/tagPair/index.d.ts +44 -29
- package/dist/src/tagPair/index.js +111 -0
- package/dist/src/transclude.d.ts +88 -85
- package/dist/src/transclude.js +739 -0
- package/dist/util/base.js +26 -0
- package/dist/util/debug.js +52 -0
- package/dist/util/diff.js +69 -0
- package/dist/util/lint.js +38 -0
- package/dist/util/string.js +103 -0
- package/errors/README +1 -0
- package/i18n/zh-hans.json +1 -0
- package/i18n/zh-hant.json +1 -0
- package/package.json +11 -14
- package/printed/README +1 -0
- package/dist/mixin/attributeParent.d.ts +0 -9
- package/dist/mixin/fixedToken.d.ts +0 -8
- package/dist/mixin/hidden.d.ts +0 -8
- package/dist/mixin/singleLine.d.ts +0 -8
- package/dist/mixin/sol.d.ts +0 -8
- package/dist/parser/brackets.d.ts +0 -12
- package/dist/parser/commentAndExt.d.ts +0 -8
- package/dist/parser/converter.d.ts +0 -7
- package/dist/parser/externalLinks.d.ts +0 -7
- package/dist/parser/hrAndDoubleUnderscore.d.ts +0 -11
- package/dist/parser/html.d.ts +0 -7
- package/dist/parser/links.d.ts +0 -7
- package/dist/parser/list.d.ts +0 -7
- package/dist/parser/magicLinks.d.ts +0 -7
- package/dist/parser/quotes.d.ts +0 -7
- package/dist/parser/selector.d.ts +0 -12
- package/dist/parser/table.d.ts +0 -11
- package/dist/src/atom/hidden.d.ts +0 -5
- package/dist/src/atom/index.d.ts +0 -15
- package/dist/src/charinsert.d.ts +0 -32
- package/dist/src/hasNowiki/index.d.ts +0 -14
- package/dist/src/hasNowiki/pre.d.ts +0 -13
- package/dist/src/nested/choose.d.ts +0 -13
- package/dist/src/nested/combobox.d.ts +0 -13
- package/dist/src/nested/index.d.ts +0 -18
- package/dist/src/nested/references.d.ts +0 -13
- package/dist/tool/index.d.ts +0 -420
- package/dist/util/base.d.ts +0 -10
- package/dist/util/debug.d.ts +0 -20
- package/dist/util/diff.d.ts +0 -8
- package/dist/util/lint.d.ts +0 -28
- package/dist/util/string.d.ts +0 -55
- package/index.js +0 -333
- package/lib/element.js +0 -618
- package/lib/node.js +0 -730
- package/lib/ranges.js +0 -130
- package/lib/text.js +0 -265
- package/lib/title.js +0 -83
- package/mixin/attributeParent.js +0 -117
- package/mixin/fixedToken.js +0 -40
- package/mixin/hidden.js +0 -21
- package/mixin/singleLine.js +0 -31
- package/mixin/sol.js +0 -54
- package/parser/brackets.js +0 -128
- package/parser/commentAndExt.js +0 -62
- package/parser/converter.js +0 -46
- package/parser/externalLinks.js +0 -33
- package/parser/hrAndDoubleUnderscore.js +0 -49
- package/parser/html.js +0 -42
- package/parser/links.js +0 -94
- package/parser/list.js +0 -59
- package/parser/magicLinks.js +0 -41
- package/parser/quotes.js +0 -64
- package/parser/selector.js +0 -180
- package/parser/table.js +0 -114
- package/src/arg.js +0 -207
- package/src/atom/hidden.js +0 -13
- package/src/atom/index.js +0 -43
- package/src/attribute.js +0 -472
- package/src/attributes.js +0 -453
- package/src/charinsert.js +0 -97
- package/src/converter.js +0 -176
- package/src/converterFlags.js +0 -284
- package/src/converterRule.js +0 -256
- package/src/extLink.js +0 -180
- package/src/gallery.js +0 -149
- package/src/hasNowiki/index.js +0 -44
- package/src/hasNowiki/pre.js +0 -40
- package/src/heading.js +0 -134
- package/src/html.js +0 -254
- package/src/imageParameter.js +0 -303
- package/src/imagemap.js +0 -199
- package/src/imagemapLink.js +0 -41
- package/src/index.js +0 -938
- package/src/link/category.js +0 -44
- package/src/link/file.js +0 -287
- package/src/link/galleryImage.js +0 -120
- package/src/link/index.js +0 -388
- package/src/magicLink.js +0 -151
- package/src/nested/choose.js +0 -24
- package/src/nested/combobox.js +0 -23
- package/src/nested/index.js +0 -96
- package/src/nested/references.js +0 -23
- package/src/nowiki/comment.js +0 -71
- package/src/nowiki/dd.js +0 -59
- package/src/nowiki/doubleUnderscore.js +0 -56
- package/src/nowiki/hr.js +0 -41
- package/src/nowiki/index.js +0 -56
- package/src/nowiki/list.js +0 -16
- package/src/nowiki/noinclude.js +0 -28
- package/src/nowiki/quote.js +0 -69
- package/src/onlyinclude.js +0 -64
- package/src/paramTag/index.js +0 -89
- package/src/paramTag/inputbox.js +0 -35
- package/src/parameter.js +0 -239
- package/src/syntax.js +0 -91
- package/src/table/index.js +0 -985
- package/src/table/td.js +0 -343
- package/src/table/tr.js +0 -319
- package/src/tagPair/ext.js +0 -146
- package/src/tagPair/include.js +0 -50
- package/src/tagPair/index.js +0 -131
- package/src/transclude.js +0 -843
- package/tool/index.js +0 -1209
- package/typings/api.d.ts +0 -9
- package/typings/array.d.ts +0 -29
- package/typings/event.d.ts +0 -22
- package/typings/index.d.ts +0 -118
- package/typings/node.d.ts +0 -35
- package/typings/parser.d.ts +0 -12
- package/typings/table.d.ts +0 -10
- package/typings/token.d.ts +0 -31
- package/typings/tool.d.ts +0 -6
- package/util/base.js +0 -17
- package/util/debug.js +0 -73
- package/util/diff.js +0 -76
- package/util/lint.js +0 -57
- package/util/string.js +0 -126
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Parser = require("../index");
|
|
3
|
+
const Token = require(".");
|
|
4
|
+
const GalleryImageToken = require("./link/galleryImage");
|
|
5
|
+
const HiddenToken = require("./hidden");
|
|
6
|
+
/**
|
|
7
|
+
* gallery标签
|
|
8
|
+
* @classdesc `{childNodes: ...(GalleryImageToken|HiddenToken|AstText)}`
|
|
9
|
+
*/
|
|
10
|
+
class GalleryToken extends Token {
|
|
11
|
+
/** @browser */
|
|
12
|
+
type = 'ext-inner';
|
|
13
|
+
/** 所有图片 */
|
|
14
|
+
get images() {
|
|
15
|
+
return this.childNodes.filter(({ type }) => type === 'gallery-image');
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* @browser
|
|
19
|
+
* @param inner 标签内部wikitext
|
|
20
|
+
*/
|
|
21
|
+
constructor(inner, config = Parser.getConfig(), accum = []) {
|
|
22
|
+
super(undefined, config, true, accum, {
|
|
23
|
+
AstText: ':', GalleryImageToken: ':', HiddenToken: ':',
|
|
24
|
+
});
|
|
25
|
+
for (const line of inner?.split('\n') ?? []) {
|
|
26
|
+
const matches = /^([^|]+)(?:\|(.*))?/u.exec(line);
|
|
27
|
+
if (!matches) {
|
|
28
|
+
super.insertAt((line.trim()
|
|
29
|
+
? new HiddenToken(line, config, [], {
|
|
30
|
+
AstText: ':',
|
|
31
|
+
})
|
|
32
|
+
: line));
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const [, file, alt] = matches, title = this.normalizeTitle(file, 6, true, true);
|
|
36
|
+
if (title.valid) {
|
|
37
|
+
super.insertAt(new GalleryImageToken('gallery', file, alt, config, accum));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
super.insertAt(new HiddenToken(line, config, [], {
|
|
41
|
+
AstText: ':',
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @override
|
|
48
|
+
* @browser
|
|
49
|
+
*/
|
|
50
|
+
toString(selector) {
|
|
51
|
+
return super.toString(selector, '\n');
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @override
|
|
55
|
+
* @browser
|
|
56
|
+
*/
|
|
57
|
+
text() {
|
|
58
|
+
return super.text('\n').replace(/\n\s*\n/gu, '\n');
|
|
59
|
+
}
|
|
60
|
+
/** @private */
|
|
61
|
+
getGaps() {
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @override
|
|
66
|
+
* @browser
|
|
67
|
+
*/
|
|
68
|
+
print() {
|
|
69
|
+
return super.print({ sep: '\n' });
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @override
|
|
73
|
+
* @browser
|
|
74
|
+
*/
|
|
75
|
+
lint(start = this.getAbsoluteIndex()) {
|
|
76
|
+
const { top, left } = this.getRootNode().posFromIndex(start), errors = [];
|
|
77
|
+
for (let i = 0, startIndex = start; i < this.length; i++) {
|
|
78
|
+
const child = this.childNodes[i], str = String(child), { length } = str, trimmed = str.trim(), startLine = top + i, startCol = i ? 0 : left;
|
|
79
|
+
if (child.type === 'hidden' && trimmed && !/^<!--.*-->$/u.test(trimmed)) {
|
|
80
|
+
errors.push({
|
|
81
|
+
message: Parser.msg('invalid content in <$1>', 'gallery'),
|
|
82
|
+
severity: 'error',
|
|
83
|
+
startIndex,
|
|
84
|
+
endIndex: startIndex + length,
|
|
85
|
+
startLine,
|
|
86
|
+
endLine: startLine,
|
|
87
|
+
startCol,
|
|
88
|
+
endCol: startCol + length,
|
|
89
|
+
excerpt: String(child).slice(0, 50),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else if (child.type !== 'hidden' && child.type !== 'text') {
|
|
93
|
+
errors.push(...child.lint(startIndex));
|
|
94
|
+
}
|
|
95
|
+
startIndex += length + 1;
|
|
96
|
+
}
|
|
97
|
+
return errors;
|
|
98
|
+
}
|
|
99
|
+
/** @override */
|
|
100
|
+
cloneNode() {
|
|
101
|
+
const cloned = this.cloneChildNodes();
|
|
102
|
+
return Parser.run(() => {
|
|
103
|
+
const token = new GalleryToken(undefined, this.getAttribute('config'));
|
|
104
|
+
token.append(...cloned);
|
|
105
|
+
return token;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 插入图片
|
|
110
|
+
* @param file 图片文件名
|
|
111
|
+
* @param i 插入位置
|
|
112
|
+
* @throws `SyntaxError` 非法的文件名
|
|
113
|
+
*/
|
|
114
|
+
insertImage(file, i = this.length) {
|
|
115
|
+
const title = this.normalizeTitle(file, 6, true, true);
|
|
116
|
+
if (title.valid) {
|
|
117
|
+
const token = Parser.run(
|
|
118
|
+
() => new GalleryImageToken('gallery', file, undefined, this.getAttribute('config')));
|
|
119
|
+
return this.insertAt(token, i);
|
|
120
|
+
}
|
|
121
|
+
throw new SyntaxError(`非法的文件名:${file}`);
|
|
122
|
+
}
|
|
123
|
+
/** @ignore */
|
|
124
|
+
insertAt(token, i = 0) {
|
|
125
|
+
if (typeof token === 'string' && token.trim() || token instanceof HiddenToken) {
|
|
126
|
+
throw new RangeError('请勿向图库中插入不可见内容!');
|
|
127
|
+
}
|
|
128
|
+
return super.insertAt(token, i);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
Parser.classes['GalleryToken'] = __filename;
|
|
132
|
+
module.exports = GalleryToken;
|
package/dist/src/heading.d.ts
CHANGED
|
@@ -1,44 +1,82 @@
|
|
|
1
|
-
|
|
1
|
+
import * as Parser from '../index';
|
|
2
|
+
import Token = require('.');
|
|
3
|
+
import SyntaxToken = require('./syntax');
|
|
4
|
+
declare const HeadingToken_base: (abstract new (...args: any[]) => {
|
|
5
|
+
"__#14@#isRoot"(): boolean;
|
|
6
|
+
prependNewLine(): string;
|
|
7
|
+
toString(selector?: string | undefined): string; /** 内部wikitext */
|
|
8
|
+
getPadding(): number;
|
|
9
|
+
text(): string;
|
|
10
|
+
length: number;
|
|
11
|
+
insertAt(token: unknown, i?: number | undefined): unknown;
|
|
12
|
+
}) & ((abstract new (...args: any[]) => {
|
|
13
|
+
removeAt(): never;
|
|
14
|
+
insertAt(token: string, i?: number | undefined): import("../lib/text");
|
|
15
|
+
insertAt<T extends import("../lib/node").AstNodeTypes>(token: T, i?: number | undefined): T;
|
|
16
|
+
length: number;
|
|
17
|
+
toString(selector?: string | undefined, separator?: string | undefined): string;
|
|
18
|
+
text(separator?: string | undefined): string;
|
|
19
|
+
}) & {
|
|
20
|
+
readonly fixed: true;
|
|
21
|
+
}) & typeof Token;
|
|
2
22
|
/**
|
|
3
23
|
* 章节标题
|
|
4
24
|
* @classdesc `{childNodes: [Token, SyntaxToken]}`
|
|
5
25
|
*/
|
|
6
|
-
declare class HeadingToken extends
|
|
26
|
+
declare abstract class HeadingToken extends HeadingToken_base {
|
|
27
|
+
#private;
|
|
28
|
+
/** @browser */
|
|
29
|
+
readonly type = "heading";
|
|
30
|
+
name: string;
|
|
31
|
+
childNodes: [Token, SyntaxToken];
|
|
32
|
+
abstract get children(): [Token, SyntaxToken];
|
|
33
|
+
abstract get firstChild(): Token;
|
|
34
|
+
abstract get firstElementChild(): Token;
|
|
35
|
+
abstract get lastChild(): SyntaxToken;
|
|
36
|
+
abstract get lastElementChild(): SyntaxToken;
|
|
7
37
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
10
|
-
* @param {import('../../typings/token').accum} accum
|
|
38
|
+
* 标题层级
|
|
39
|
+
* @browser
|
|
11
40
|
*/
|
|
12
|
-
|
|
41
|
+
get level(): number;
|
|
42
|
+
/** @throws `RangeError` 标题层级应为 1 - 6 之间的整数 */
|
|
43
|
+
set level(n: number);
|
|
13
44
|
/** 内部wikitext */
|
|
14
45
|
get innerText(): string;
|
|
46
|
+
/**
|
|
47
|
+
* @browser
|
|
48
|
+
* @param level 标题层级
|
|
49
|
+
* @param input 标题文字
|
|
50
|
+
*/
|
|
51
|
+
constructor(level: number, input: string[], config?: Parser.Config, accum?: Token[]);
|
|
15
52
|
/**
|
|
16
53
|
* @override
|
|
17
|
-
* @
|
|
18
|
-
* @param {string} selector
|
|
19
|
-
* @returns {string}
|
|
54
|
+
* @browser
|
|
20
55
|
*/
|
|
21
|
-
|
|
22
|
-
prependNewLine(): '' | '\n';
|
|
23
|
-
} & HeadingToken, selector: string): string;
|
|
56
|
+
toString(selector?: string): string;
|
|
24
57
|
/**
|
|
25
58
|
* @override
|
|
26
|
-
* @
|
|
27
|
-
* @returns {string}
|
|
59
|
+
* @browser
|
|
28
60
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
61
|
+
text(): string;
|
|
62
|
+
/**
|
|
63
|
+
* @override
|
|
64
|
+
* @browser
|
|
65
|
+
*/
|
|
66
|
+
print(): string;
|
|
67
|
+
/**
|
|
68
|
+
* @override
|
|
69
|
+
* @browser
|
|
70
|
+
*/
|
|
71
|
+
lint(start?: number): Parser.LintError[];
|
|
34
72
|
/** @override */
|
|
35
|
-
|
|
73
|
+
cloneNode(): this;
|
|
36
74
|
/**
|
|
37
75
|
* 设置标题层级
|
|
38
|
-
* @param
|
|
76
|
+
* @param n 标题层级
|
|
39
77
|
*/
|
|
40
78
|
setLevel(n: number): void;
|
|
41
79
|
/** 移除标题后的不可见内容 */
|
|
42
80
|
removeTrail(): void;
|
|
43
81
|
}
|
|
44
|
-
|
|
82
|
+
export = HeadingToken;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const lint_1 = require("../util/lint");
|
|
3
|
+
const fixed = require("../mixin/fixed");
|
|
4
|
+
const sol = require("../mixin/sol");
|
|
5
|
+
const Parser = require("../index");
|
|
6
|
+
const Token = require(".");
|
|
7
|
+
const SyntaxToken = require("./syntax");
|
|
8
|
+
/**
|
|
9
|
+
* 章节标题
|
|
10
|
+
* @classdesc `{childNodes: [Token, SyntaxToken]}`
|
|
11
|
+
*/
|
|
12
|
+
class HeadingToken extends sol(fixed(Token)) {
|
|
13
|
+
/** @browser */
|
|
14
|
+
type = 'heading';
|
|
15
|
+
/**
|
|
16
|
+
* 标题层级
|
|
17
|
+
* @browser
|
|
18
|
+
*/
|
|
19
|
+
get level() {
|
|
20
|
+
return Number(this.name);
|
|
21
|
+
}
|
|
22
|
+
/** @throws `RangeError` 标题层级应为 1 - 6 之间的整数 */
|
|
23
|
+
set level(n) {
|
|
24
|
+
this.setLevel(n);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 标题格式的等号
|
|
28
|
+
* @browser
|
|
29
|
+
*/
|
|
30
|
+
get #equals() {
|
|
31
|
+
return '='.repeat(this.level);
|
|
32
|
+
}
|
|
33
|
+
/** 内部wikitext */
|
|
34
|
+
get innerText() {
|
|
35
|
+
return this.firstChild.text();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @browser
|
|
39
|
+
* @param level 标题层级
|
|
40
|
+
* @param input 标题文字
|
|
41
|
+
*/
|
|
42
|
+
constructor(level, input, config = Parser.getConfig(), accum = []) {
|
|
43
|
+
super(undefined, config, true, accum);
|
|
44
|
+
this.setAttribute('name', String(level));
|
|
45
|
+
const token = new Token(input[0], config, true, accum);
|
|
46
|
+
token.type = 'heading-title';
|
|
47
|
+
token.setAttribute('stage', 2);
|
|
48
|
+
const trail = new SyntaxToken(input[1], /^[^\S\n]*$/u, 'heading-trail', config, accum, {
|
|
49
|
+
'Stage-1': ':', '!ExtToken': '',
|
|
50
|
+
});
|
|
51
|
+
this.append(token, trail);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @override
|
|
55
|
+
* @browser
|
|
56
|
+
*/
|
|
57
|
+
toString(selector) {
|
|
58
|
+
const equals = this.#equals;
|
|
59
|
+
return selector && this.matches(selector)
|
|
60
|
+
? ''
|
|
61
|
+
: `${this.prependNewLine()}${equals}${this.firstChild.toString(selector)}${equals}${this.lastChild.toString(selector)}`;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @override
|
|
65
|
+
* @browser
|
|
66
|
+
*/
|
|
67
|
+
text() {
|
|
68
|
+
const equals = this.#equals;
|
|
69
|
+
return `${this.prependNewLine()}${equals}${this.firstChild.text()}${equals}`;
|
|
70
|
+
}
|
|
71
|
+
/** @private */
|
|
72
|
+
getPadding() {
|
|
73
|
+
return super.getPadding() + this.level;
|
|
74
|
+
}
|
|
75
|
+
/** @private */
|
|
76
|
+
getGaps() {
|
|
77
|
+
return this.level;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @override
|
|
81
|
+
* @browser
|
|
82
|
+
*/
|
|
83
|
+
print() {
|
|
84
|
+
const equals = this.#equals;
|
|
85
|
+
return super.print({ pre: equals, sep: equals });
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @override
|
|
89
|
+
* @browser
|
|
90
|
+
*/
|
|
91
|
+
lint(start = this.getAbsoluteIndex()) {
|
|
92
|
+
const errors = super.lint(start), innerStr = String(this.firstChild);
|
|
93
|
+
let refError;
|
|
94
|
+
if (this.name === '1') {
|
|
95
|
+
refError = (0, lint_1.generateForSelf)(this, { start }, '<h1>');
|
|
96
|
+
errors.push(refError);
|
|
97
|
+
}
|
|
98
|
+
if (innerStr.startsWith('=') || innerStr.endsWith('=')) {
|
|
99
|
+
refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
|
|
100
|
+
errors.push({ ...refError, message: Parser.msg('unbalanced "=" in a section header') });
|
|
101
|
+
}
|
|
102
|
+
if (this.closest('html-attrs, table-attrs')) {
|
|
103
|
+
refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
|
|
104
|
+
errors.push({ ...refError, message: Parser.msg('section header in a HTML tag') });
|
|
105
|
+
}
|
|
106
|
+
return errors;
|
|
107
|
+
}
|
|
108
|
+
/** @override */
|
|
109
|
+
cloneNode() {
|
|
110
|
+
const [title, trail] = this.cloneChildNodes();
|
|
111
|
+
return Parser.run(() => {
|
|
112
|
+
const token = new HeadingToken(this.level, [], this.getAttribute('config'));
|
|
113
|
+
token.firstChild.safeReplaceWith(title);
|
|
114
|
+
token.lastChild.safeReplaceWith(trail);
|
|
115
|
+
return token;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 设置标题层级
|
|
120
|
+
* @param n 标题层级
|
|
121
|
+
*/
|
|
122
|
+
setLevel(n) {
|
|
123
|
+
if (!Number.isInteger(n)) {
|
|
124
|
+
this.typeError('setLevel', 'Number');
|
|
125
|
+
}
|
|
126
|
+
const level = String(Math.min(Math.max(n, 1), 6));
|
|
127
|
+
this.setAttribute('name', level);
|
|
128
|
+
}
|
|
129
|
+
/** 移除标题后的不可见内容 */
|
|
130
|
+
removeTrail() {
|
|
131
|
+
this.lastChild.replaceChildren();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
Parser.classes['HeadingToken'] = __filename;
|
|
135
|
+
module.exports = HeadingToken;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as Parser from '../index';
|
|
2
|
+
import Token = require('.');
|
|
3
|
+
declare const HiddenToken_base: ((abstract new (...args: any[]) => {
|
|
4
|
+
text(): string;
|
|
5
|
+
length: number;
|
|
6
|
+
toString(selector?: string | undefined, separator?: string | undefined): string;
|
|
7
|
+
insertAt(token: unknown, i?: number | undefined): unknown;
|
|
8
|
+
}) & {
|
|
9
|
+
readonly hidden: true;
|
|
10
|
+
}) & typeof Token;
|
|
11
|
+
/** 不可见的节点 */
|
|
12
|
+
declare class HiddenToken extends HiddenToken_base {
|
|
13
|
+
/** @browser */
|
|
14
|
+
readonly type = "hidden";
|
|
15
|
+
/** @browser */
|
|
16
|
+
constructor(wikitext?: string, config?: Parser.Config, accum?: Token[], acceptable?: Acceptable);
|
|
17
|
+
/** @override */
|
|
18
|
+
cloneNode(): this;
|
|
19
|
+
}
|
|
20
|
+
export = HiddenToken;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const hidden = require("../mixin/hidden");
|
|
3
|
+
const Parser = require("../index");
|
|
4
|
+
const Token = require(".");
|
|
5
|
+
/** 不可见的节点 */
|
|
6
|
+
class HiddenToken extends hidden(Token) {
|
|
7
|
+
/** @browser */
|
|
8
|
+
type = 'hidden';
|
|
9
|
+
/** @browser */
|
|
10
|
+
constructor(wikitext, config = Parser.getConfig(), accum = [], acceptable) {
|
|
11
|
+
super(wikitext, config, true, accum, acceptable);
|
|
12
|
+
}
|
|
13
|
+
/** @override */
|
|
14
|
+
cloneNode() {
|
|
15
|
+
const cloned = this.cloneChildNodes(), config = this.getAttribute('config'), acceptable = this.getAttribute('acceptable');
|
|
16
|
+
return Parser.run(() => {
|
|
17
|
+
const token = new HiddenToken(undefined, config, [], acceptable);
|
|
18
|
+
token.append(...cloned);
|
|
19
|
+
return token;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
Parser.classes['HiddenToken'] = __filename;
|
|
24
|
+
module.exports = HiddenToken;
|
package/dist/src/html.d.ts
CHANGED
|
@@ -1,56 +1,110 @@
|
|
|
1
|
-
|
|
1
|
+
import * as Parser from '../index';
|
|
2
|
+
import Token = require('.');
|
|
3
|
+
import AttributesToken = require('./attributes');
|
|
4
|
+
import type { TokenAttributeGetter } from '../lib/node';
|
|
5
|
+
declare const HtmlToken_base: (abstract new (...args: any[]) => {
|
|
6
|
+
childNodes: import("../lib/node").AstNodeTypes[];
|
|
7
|
+
readonly "__#12@#attributesChild": AttributesToken;
|
|
8
|
+
readonly attributes: Record<string, string | true>;
|
|
9
|
+
className: string;
|
|
10
|
+
readonly classList: Set<string>;
|
|
11
|
+
id: string;
|
|
12
|
+
hasAttr(key: string): boolean;
|
|
13
|
+
getAttr(key: string): string | true | undefined;
|
|
14
|
+
getAttrNames(): Set<string>;
|
|
15
|
+
hasAttrs(): boolean;
|
|
16
|
+
getAttrs(): Record<string, string | true>;
|
|
17
|
+
setAttr(key: string, value: string | boolean): void;
|
|
18
|
+
removeAttr(key: string): void;
|
|
19
|
+
toggleAttr(key: string, force?: boolean | undefined): void;
|
|
20
|
+
length: number;
|
|
21
|
+
toString(selector?: string | undefined, separator?: string | undefined): string;
|
|
22
|
+
text(separator?: string | undefined): string;
|
|
23
|
+
insertAt(token: unknown, i?: number | undefined): unknown;
|
|
24
|
+
}) & ((abstract new (...args: any[]) => {
|
|
25
|
+
removeAt(): never;
|
|
26
|
+
insertAt(token: string, i?: number | undefined): import("../lib/text");
|
|
27
|
+
insertAt<T extends import("../lib/node").AstNodeTypes>(token: T, i?: number | undefined): T;
|
|
28
|
+
length: number;
|
|
29
|
+
toString(selector?: string | undefined, separator?: string | undefined): string;
|
|
30
|
+
text(separator?: string | undefined): string;
|
|
31
|
+
}) & {
|
|
32
|
+
readonly fixed: true;
|
|
33
|
+
}) & typeof Token;
|
|
2
34
|
/**
|
|
3
35
|
* HTML标签
|
|
4
36
|
* @classdesc `{childNodes: [AttributesToken]}`
|
|
5
37
|
*/
|
|
6
|
-
declare class HtmlToken extends
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/**
|
|
38
|
+
declare abstract class HtmlToken extends HtmlToken_base {
|
|
39
|
+
#private;
|
|
40
|
+
/** @browser */
|
|
41
|
+
readonly type = "html";
|
|
42
|
+
name: string;
|
|
43
|
+
childNodes: [AttributesToken];
|
|
44
|
+
abstract get children(): [AttributesToken];
|
|
45
|
+
abstract get firstChild(): AttributesToken;
|
|
46
|
+
abstract get firstElementChild(): AttributesToken;
|
|
47
|
+
abstract get lastChild(): AttributesToken;
|
|
48
|
+
abstract get lastElementChild(): AttributesToken;
|
|
49
|
+
/**
|
|
50
|
+
* 是否是闭合标签
|
|
51
|
+
* @browser
|
|
52
|
+
*/
|
|
18
53
|
get closing(): boolean;
|
|
19
|
-
/** @throws `Error`
|
|
20
|
-
set
|
|
21
|
-
/**
|
|
54
|
+
/** @throws `Error` 自封闭标签或空标签 */
|
|
55
|
+
set closing(value: boolean);
|
|
56
|
+
/** 是否自封闭 */
|
|
22
57
|
get selfClosing(): boolean;
|
|
58
|
+
/** @throws `Error` 闭合标签或无效自封闭标签 */
|
|
59
|
+
set selfClosing(value: boolean);
|
|
60
|
+
/**
|
|
61
|
+
* @browser
|
|
62
|
+
* @param name 标签名
|
|
63
|
+
* @param attr 标签属性
|
|
64
|
+
* @param closing 是否闭合
|
|
65
|
+
* @param selfClosing 是否自封闭
|
|
66
|
+
*/
|
|
67
|
+
constructor(name: string, attr: AttributesToken, closing: boolean, selfClosing: boolean, config?: Parser.Config, accum?: Token[]);
|
|
23
68
|
/**
|
|
24
69
|
* @override
|
|
25
|
-
* @
|
|
70
|
+
* @browser
|
|
26
71
|
*/
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
72
|
+
toString(selector?: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* @override
|
|
75
|
+
* @browser
|
|
76
|
+
*/
|
|
77
|
+
text(): string;
|
|
78
|
+
/**
|
|
79
|
+
* @override
|
|
80
|
+
* @browser
|
|
81
|
+
*/
|
|
82
|
+
print(): string;
|
|
83
|
+
/**
|
|
84
|
+
* @override
|
|
85
|
+
* @browser
|
|
86
|
+
*/
|
|
87
|
+
lint(start?: number): Parser.LintError[];
|
|
32
88
|
/**
|
|
33
89
|
* 搜索匹配的标签
|
|
34
|
-
* @
|
|
90
|
+
* @browser
|
|
35
91
|
* @throws `SyntaxError` 同时闭合和自封闭的标签
|
|
36
92
|
* @throws `SyntaxError` 无效自封闭标签
|
|
37
93
|
* @throws `SyntaxError` 未闭合的标签
|
|
38
94
|
*/
|
|
39
|
-
findMatchingTag(): this |
|
|
95
|
+
findMatchingTag(): this | undefined;
|
|
40
96
|
/** @override */
|
|
41
|
-
|
|
97
|
+
cloneNode(): this;
|
|
42
98
|
/**
|
|
43
99
|
* 更换标签名
|
|
44
|
-
* @param
|
|
100
|
+
* @param tag 标签名
|
|
45
101
|
* @throws `RangeError` 非法的HTML标签
|
|
46
102
|
*/
|
|
47
103
|
replaceTag(tag: string): void;
|
|
48
104
|
/**
|
|
49
105
|
* 修复无效自封闭标签
|
|
50
|
-
* @complexity `n`
|
|
51
106
|
* @throws `Error` 无法修复无效自封闭标签
|
|
52
107
|
*/
|
|
53
108
|
fix(): void;
|
|
54
|
-
#private;
|
|
55
109
|
}
|
|
56
|
-
|
|
110
|
+
export = HtmlToken;
|