wikiparser-node 0.9.2-b → 0.10.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/README.md +40 -0
- package/config/.schema.json +134 -0
- package/config/default.json +832 -0
- package/config/llwiki.json +630 -0
- package/config/moegirl.json +729 -0
- package/config/zhwiki.json +1269 -0
- package/i18n/zh-hans.json +1 -0
- package/i18n/zh-hant.json +1 -0
- package/index.js +333 -0
- package/lib/element.js +611 -0
- package/lib/node.js +770 -0
- package/lib/ranges.js +130 -0
- package/lib/text.js +263 -0
- package/lib/title.js +83 -0
- package/mixin/attributeParent.js +117 -0
- package/mixin/fixedToken.js +40 -0
- package/mixin/hidden.js +21 -0
- package/mixin/singleLine.js +31 -0
- package/mixin/sol.js +54 -0
- package/package.json +49 -47
- package/parser/brackets.js +126 -0
- package/parser/commentAndExt.js +59 -0
- package/parser/converter.js +46 -0
- package/parser/externalLinks.js +33 -0
- package/parser/hrAndDoubleUnderscore.js +49 -0
- package/parser/html.js +42 -0
- package/parser/links.js +94 -0
- package/parser/list.js +59 -0
- package/parser/magicLinks.js +41 -0
- package/parser/quotes.js +64 -0
- package/parser/selector.js +177 -0
- package/parser/table.js +114 -0
- package/src/arg.js +207 -0
- package/src/atom/hidden.js +13 -0
- package/src/atom/index.js +43 -0
- package/src/attribute.js +470 -0
- package/src/attributes.js +453 -0
- package/src/charinsert.js +97 -0
- package/src/converter.js +176 -0
- package/src/converterFlags.js +284 -0
- package/src/converterRule.js +256 -0
- package/src/extLink.js +180 -0
- package/src/gallery.js +149 -0
- package/src/hasNowiki/index.js +44 -0
- package/src/hasNowiki/pre.js +40 -0
- package/src/heading.js +134 -0
- package/src/html.js +254 -0
- package/src/imageParameter.js +303 -0
- package/src/imagemap.js +199 -0
- package/src/imagemapLink.js +41 -0
- package/src/index.js +932 -0
- package/src/link/category.js +44 -0
- package/src/link/file.js +287 -0
- package/src/link/galleryImage.js +120 -0
- package/src/link/index.js +388 -0
- package/src/magicLink.js +149 -0
- package/src/nested/choose.js +24 -0
- package/src/nested/combobox.js +23 -0
- package/src/nested/index.js +93 -0
- package/src/nested/references.js +23 -0
- package/src/nowiki/comment.js +71 -0
- package/src/nowiki/dd.js +59 -0
- package/src/nowiki/doubleUnderscore.js +56 -0
- package/src/nowiki/hr.js +41 -0
- package/src/nowiki/index.js +56 -0
- package/src/nowiki/list.js +16 -0
- package/src/nowiki/noinclude.js +28 -0
- package/src/nowiki/quote.js +69 -0
- package/src/onlyinclude.js +64 -0
- package/src/paramTag/index.js +89 -0
- package/src/paramTag/inputbox.js +35 -0
- package/src/parameter.js +239 -0
- package/src/syntax.js +91 -0
- package/src/table/index.js +983 -0
- package/src/table/td.js +338 -0
- package/src/table/tr.js +319 -0
- package/src/tagPair/ext.js +145 -0
- package/src/tagPair/include.js +50 -0
- package/src/tagPair/index.js +126 -0
- package/src/transclude.js +843 -0
- package/tool/index.js +1202 -0
- package/util/base.js +17 -0
- package/util/debug.js +73 -0
- package/util/diff.js +76 -0
- package/util/lint.js +55 -0
- package/util/string.js +126 -0
- package/bundle/bundle.min.js +0 -38
- package/extensions/editor.css +0 -62
- package/extensions/editor.js +0 -328
- package/extensions/ui.css +0 -119
package/src/extLink.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..'),
|
|
4
|
+
{noWrap, normalizeSpace} = require('../util/string'),
|
|
5
|
+
Token = require('.'),
|
|
6
|
+
MagicLinkToken = require('./magicLink');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 外链
|
|
10
|
+
* @classdesc `{childNodes: [MagicLinkToken, ?Token]}`
|
|
11
|
+
*/
|
|
12
|
+
class ExtLinkToken extends Token {
|
|
13
|
+
type = 'ext-link';
|
|
14
|
+
#space;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 协议
|
|
18
|
+
* @this {{firstChild: MagicLinkToken}}
|
|
19
|
+
*/
|
|
20
|
+
get protocol() {
|
|
21
|
+
return this.firstChild.protocol;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @this {{firstChild: MagicLinkToken}} */
|
|
25
|
+
set protocol(value) {
|
|
26
|
+
this.firstChild.protocol = value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 和内链保持一致
|
|
31
|
+
* @this {{firstChild: MagicLinkToken}}
|
|
32
|
+
*/
|
|
33
|
+
get link() {
|
|
34
|
+
return this.firstChild.link;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
set link(url) {
|
|
38
|
+
this.setTarget(url);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 链接显示文字 */
|
|
42
|
+
get innerText() {
|
|
43
|
+
return this.length > 1
|
|
44
|
+
? this.lastChild.text()
|
|
45
|
+
: `[${this.getRootNode().querySelectorAll('ext-link[childElementCount=1]').indexOf(this) + 1}]`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} url 网址
|
|
50
|
+
* @param {string} space 空白字符
|
|
51
|
+
* @param {string} text 链接文字
|
|
52
|
+
* @param {accum} accum
|
|
53
|
+
*/
|
|
54
|
+
constructor(url, space = '', text = '', config = Parser.getConfig(), accum = []) {
|
|
55
|
+
super(undefined, config, true, accum, {
|
|
56
|
+
MagicLinkToken: 0, Token: 1,
|
|
57
|
+
});
|
|
58
|
+
this.insertAt(new MagicLinkToken(url, true, config, accum));
|
|
59
|
+
this.#space = space;
|
|
60
|
+
if (text) {
|
|
61
|
+
const inner = new Token(text, config, true, accum, {
|
|
62
|
+
'Stage-7': ':', ConverterToken: ':',
|
|
63
|
+
});
|
|
64
|
+
inner.type = 'ext-link-text';
|
|
65
|
+
this.insertAt(inner.setAttribute('stage', Parser.MAX_STAGE - 1));
|
|
66
|
+
}
|
|
67
|
+
this.getAttribute('protectChildren')(0);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @override
|
|
72
|
+
* @param {string} selector
|
|
73
|
+
*/
|
|
74
|
+
toString(selector) {
|
|
75
|
+
if (selector && this.matches(selector)) {
|
|
76
|
+
return '';
|
|
77
|
+
} else if (this.length === 1) {
|
|
78
|
+
return `[${super.toString(selector)}${this.#space}]`;
|
|
79
|
+
}
|
|
80
|
+
this.#correct();
|
|
81
|
+
normalizeSpace(this.lastChild);
|
|
82
|
+
return `[${super.toString(selector, this.#space)}]`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @override */
|
|
86
|
+
text() {
|
|
87
|
+
normalizeSpace(this.childNodes[1]);
|
|
88
|
+
return `[${super.text(' ')}]`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** @override */
|
|
92
|
+
getPadding() {
|
|
93
|
+
return 1;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @override */
|
|
97
|
+
getGaps() {
|
|
98
|
+
this.#correct();
|
|
99
|
+
return this.#space.length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @override */
|
|
103
|
+
print() {
|
|
104
|
+
return super.print(
|
|
105
|
+
this.length > 1 ? {pre: '[', sep: this.#space, post: ']'} : {pre: '[', post: `${this.#space}]`},
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @override */
|
|
110
|
+
cloneNode() {
|
|
111
|
+
const [url, text] = this.cloneChildNodes();
|
|
112
|
+
return Parser.run(() => {
|
|
113
|
+
const token = new ExtLinkToken(undefined, '', '', this.getAttribute('config'));
|
|
114
|
+
token.firstChild.safeReplaceWith(url);
|
|
115
|
+
if (text) {
|
|
116
|
+
token.insertAt(text);
|
|
117
|
+
}
|
|
118
|
+
return token;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 修正空白字符 */
|
|
123
|
+
#correct() {
|
|
124
|
+
if (!this.#space && this.length > 1
|
|
125
|
+
// 都替换成`<`肯定不对,但无妨
|
|
126
|
+
&& /^[^[\]<>"{\0-\x1F\x7F\p{Zs}\uFFFD]/u.test(this.lastChild.text().replace(/&[lg]t;/u, '<'))
|
|
127
|
+
) {
|
|
128
|
+
this.#space = ' ';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 获取网址
|
|
134
|
+
* @this {{firstChild: MagicLinkToken}}
|
|
135
|
+
*/
|
|
136
|
+
getUrl() {
|
|
137
|
+
return this.firstChild.getUrl();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 设置链接目标
|
|
142
|
+
* @param {string|URL} url 网址
|
|
143
|
+
* @throws `SyntaxError` 非法的外链目标
|
|
144
|
+
*/
|
|
145
|
+
setTarget(url) {
|
|
146
|
+
url = String(url);
|
|
147
|
+
const root = Parser.parse(`[${url}]`, this.getAttribute('include'), 8, this.getAttribute('config')),
|
|
148
|
+
{length, firstChild: extLink} = root;
|
|
149
|
+
if (length !== 1 || extLink.type !== 'ext-link' || extLink.length !== 1) {
|
|
150
|
+
throw new SyntaxError(`非法的外链目标:${url}`);
|
|
151
|
+
}
|
|
152
|
+
const {firstChild} = extLink;
|
|
153
|
+
extLink.destroy(true);
|
|
154
|
+
this.firstChild.safeReplaceWith(firstChild);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 设置链接显示文字
|
|
159
|
+
* @param {string} text 链接显示文字
|
|
160
|
+
* @throws `SyntaxError` 非法的链接显示文字
|
|
161
|
+
*/
|
|
162
|
+
setLinkText(text) {
|
|
163
|
+
text = String(text);
|
|
164
|
+
const root = Parser.parse(`[//url ${text}]`, this.getAttribute('include'), 8, this.getAttribute('config')),
|
|
165
|
+
{length, firstChild: extLink} = root;
|
|
166
|
+
if (length !== 1 || extLink.type !== 'ext-link' || extLink.length !== 2) {
|
|
167
|
+
throw new SyntaxError(`非法的外链文字:${noWrap(text)}`);
|
|
168
|
+
}
|
|
169
|
+
const {lastChild} = extLink;
|
|
170
|
+
if (this.length === 1) {
|
|
171
|
+
this.insertAt(lastChild);
|
|
172
|
+
} else {
|
|
173
|
+
this.lastChild.safeReplaceWith(lastChild);
|
|
174
|
+
}
|
|
175
|
+
this.#space ||= ' ';
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
Parser.classes.ExtLinkToken = __filename;
|
|
180
|
+
module.exports = ExtLinkToken;
|
package/src/gallery.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..'),
|
|
4
|
+
Token = require('.'),
|
|
5
|
+
GalleryImageToken = require('./link/galleryImage'),
|
|
6
|
+
HiddenToken = require('./atom/hidden');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* gallery标签
|
|
10
|
+
* @classdesc `{childNodes: ...(GalleryImageToken|HiddenToken|AstText)}`
|
|
11
|
+
*/
|
|
12
|
+
class GalleryToken extends Token {
|
|
13
|
+
type = 'ext-inner';
|
|
14
|
+
name = 'gallery';
|
|
15
|
+
|
|
16
|
+
/** 所有图片 */
|
|
17
|
+
get images() {
|
|
18
|
+
return this.childNodes.filter(({type}) => type === 'gallery-image');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} inner 标签内部wikitext
|
|
23
|
+
* @param {accum} accum
|
|
24
|
+
*/
|
|
25
|
+
constructor(inner, config = Parser.getConfig(), accum = []) {
|
|
26
|
+
super(undefined, config, true, accum, {
|
|
27
|
+
AstText: ':', GalleryImageToken: ':', HiddenToken: ':',
|
|
28
|
+
});
|
|
29
|
+
for (const line of inner?.split('\n') ?? []) {
|
|
30
|
+
const matches = /^([^|]+)(?:\|(.*))?/u.exec(line);
|
|
31
|
+
if (!matches) {
|
|
32
|
+
super.insertAt(line.trim()
|
|
33
|
+
? new HiddenToken(line, undefined, config, [], {
|
|
34
|
+
AstText: ':',
|
|
35
|
+
})
|
|
36
|
+
: line);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const [, file, alt] = matches,
|
|
40
|
+
title = this.normalizeTitle(file, 6, true, true);
|
|
41
|
+
if (title.valid) {
|
|
42
|
+
super.insertAt(new GalleryImageToken(file, alt, config, accum));
|
|
43
|
+
} else {
|
|
44
|
+
super.insertAt(new HiddenToken(line, undefined, config, [], {
|
|
45
|
+
AstText: ':',
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @override
|
|
53
|
+
* @param {string} selector
|
|
54
|
+
*/
|
|
55
|
+
toString(selector) {
|
|
56
|
+
return super.toString(selector, '\n');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @override */
|
|
60
|
+
text() {
|
|
61
|
+
return super.text('\n').replace(/\n\s*\n/gu, '\n');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** @override */
|
|
65
|
+
getGaps() {
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @override */
|
|
70
|
+
print() {
|
|
71
|
+
return super.print({sep: '\n'});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @override
|
|
76
|
+
* @param {number} start 起始位置
|
|
77
|
+
*/
|
|
78
|
+
lint(start = this.getAbsoluteIndex()) {
|
|
79
|
+
const {top, left} = this.getRootNode().posFromIndex(start),
|
|
80
|
+
/** @type {LintError[]} */ errors = [];
|
|
81
|
+
for (let i = 0, startIndex = start; i < this.length; i++) {
|
|
82
|
+
const child = this.childNodes[i],
|
|
83
|
+
str = String(child),
|
|
84
|
+
{length} = str,
|
|
85
|
+
trimmed = str.trim(),
|
|
86
|
+
startLine = top + i,
|
|
87
|
+
startCol = i ? 0 : left;
|
|
88
|
+
if (child.type === 'hidden' && trimmed && !/^<!--.*-->$/u.test(trimmed)) {
|
|
89
|
+
errors.push({
|
|
90
|
+
message: Parser.msg('invalid content in <$1>', 'gallery'),
|
|
91
|
+
severity: 'error',
|
|
92
|
+
startIndex,
|
|
93
|
+
endIndex: startIndex + length,
|
|
94
|
+
startLine,
|
|
95
|
+
endLine: startLine,
|
|
96
|
+
startCol,
|
|
97
|
+
endCol: startCol + length,
|
|
98
|
+
excerpt: String(child).slice(0, 50),
|
|
99
|
+
});
|
|
100
|
+
} else if (child.type !== 'hidden' && child.type !== 'text') {
|
|
101
|
+
errors.push(...child.lint(startIndex));
|
|
102
|
+
}
|
|
103
|
+
startIndex += length + 1;
|
|
104
|
+
}
|
|
105
|
+
return errors;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @override */
|
|
109
|
+
cloneNode() {
|
|
110
|
+
const cloned = this.cloneChildNodes();
|
|
111
|
+
return Parser.run(() => {
|
|
112
|
+
const token = new GalleryToken(undefined, this.getAttribute('config'));
|
|
113
|
+
token.append(...cloned);
|
|
114
|
+
return token;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 插入图片
|
|
120
|
+
* @param {string} file 图片文件名
|
|
121
|
+
* @param {number} i 插入位置
|
|
122
|
+
* @throws `SyntaxError` 非法的文件名
|
|
123
|
+
*/
|
|
124
|
+
insertImage(file, i = this.length) {
|
|
125
|
+
const title = this.normalizeTitle(file, 6, true, true);
|
|
126
|
+
if (title.valid) {
|
|
127
|
+
const token = Parser.run(() => new GalleryImageToken(file, undefined, this.getAttribute('config')));
|
|
128
|
+
return this.insertAt(token, i);
|
|
129
|
+
}
|
|
130
|
+
throw new SyntaxError(`非法的文件名:${file}`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @override
|
|
135
|
+
* @template {string|Token} T
|
|
136
|
+
* @param {T} token 待插入的节点
|
|
137
|
+
* @param {number} i 插入位置
|
|
138
|
+
* @throws `RangeError` 插入不可见内容
|
|
139
|
+
*/
|
|
140
|
+
insertAt(token, i = 0) {
|
|
141
|
+
if (typeof token === 'string' && token.trim() || token instanceof HiddenToken) {
|
|
142
|
+
throw new RangeError('请勿向图库中插入不可见内容!');
|
|
143
|
+
}
|
|
144
|
+
return super.insertAt(token, i);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
Parser.classes.GalleryToken = __filename;
|
|
149
|
+
module.exports = GalleryToken;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('../..'),
|
|
4
|
+
Token = require('..'),
|
|
5
|
+
NoincludeToken = require('../nowiki/noinclude');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `<pre>`
|
|
9
|
+
* @classdesc `{childNodes: [...AstText|NoincludeToken]}`
|
|
10
|
+
*/
|
|
11
|
+
class HasNowikiToken extends Token {
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} wikitext wikitext
|
|
14
|
+
* @param {string} type type
|
|
15
|
+
* @param {accum} accum
|
|
16
|
+
*/
|
|
17
|
+
constructor(wikitext, type, config = Parser.getConfig(), accum = []) {
|
|
18
|
+
wikitext = wikitext.replace(
|
|
19
|
+
/(<nowiki>)(.*?)(<\/nowiki>)/giu,
|
|
20
|
+
/** @type {function(...string): string} */ (_, opening, inner, closing) => {
|
|
21
|
+
new NoincludeToken(opening, config, accum);
|
|
22
|
+
new NoincludeToken(closing, config, accum);
|
|
23
|
+
return `\0${accum.length - 1}c\x7F${inner}\0${accum.length}c\x7F`;
|
|
24
|
+
},
|
|
25
|
+
);
|
|
26
|
+
super(wikitext, config, true, accum, {
|
|
27
|
+
AstText: ':', NoincludeToken: ':',
|
|
28
|
+
});
|
|
29
|
+
this.type = type;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @override */
|
|
33
|
+
cloneNode() {
|
|
34
|
+
const cloned = this.cloneChildNodes();
|
|
35
|
+
return Parser.run(() => {
|
|
36
|
+
const token = new HasNowikiToken(undefined, this.type, this.getAttribute('config'));
|
|
37
|
+
token.append(...cloned);
|
|
38
|
+
return token;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
Parser.classes.HasNowikiToken = __filename;
|
|
44
|
+
module.exports = HasNowikiToken;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('../..'),
|
|
4
|
+
HasNowikiToken = require('.');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `<pre>`
|
|
8
|
+
* @classdesc `{childNodes: [...AstText|NoincludeToken|ConverterToken]}`
|
|
9
|
+
*/
|
|
10
|
+
class PreToken extends HasNowikiToken {
|
|
11
|
+
name = 'pre';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} wikitext wikitext
|
|
15
|
+
* @param {accum} accum
|
|
16
|
+
*/
|
|
17
|
+
constructor(wikitext, config = Parser.getConfig(), accum = []) {
|
|
18
|
+
super(wikitext, 'ext-inner', config, accum);
|
|
19
|
+
this.setAttribute('stage', Parser.MAX_STAGE - 1);
|
|
20
|
+
this.setAttribute('acceptable', {AstText: ':', NoincludeToken: ':', ConverterToken: ':'});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @override */
|
|
24
|
+
isPlain() {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @override */
|
|
29
|
+
cloneNode() {
|
|
30
|
+
const cloned = this.cloneChildNodes();
|
|
31
|
+
return Parser.run(() => {
|
|
32
|
+
const token = new PreToken(undefined, this.getAttribute('config'));
|
|
33
|
+
token.append(...cloned);
|
|
34
|
+
return token;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
Parser.classes.PreToken = __filename;
|
|
40
|
+
module.exports = PreToken;
|
package/src/heading.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {generateForSelf} = require('../util/lint'),
|
|
4
|
+
fixedToken = require('../mixin/fixedToken'),
|
|
5
|
+
sol = require('../mixin/sol'),
|
|
6
|
+
Parser = require('..'),
|
|
7
|
+
Token = require('.'),
|
|
8
|
+
SyntaxToken = require('./syntax');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 章节标题
|
|
12
|
+
* @classdesc `{childNodes: [Token, SyntaxToken]}`
|
|
13
|
+
*/
|
|
14
|
+
class HeadingToken extends fixedToken(sol(Token)) {
|
|
15
|
+
type = 'heading';
|
|
16
|
+
|
|
17
|
+
/** 内部wikitext */
|
|
18
|
+
get innerText() {
|
|
19
|
+
return this.firstChild.text();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {number} level 标题层级
|
|
24
|
+
* @param {string[]} input 标题文字
|
|
25
|
+
* @param {accum} accum
|
|
26
|
+
*/
|
|
27
|
+
constructor(level, input, config = Parser.getConfig(), accum = []) {
|
|
28
|
+
super(undefined, config, true, accum);
|
|
29
|
+
this.setAttribute('name', String(level));
|
|
30
|
+
const token = new Token(input[0], config, true, accum);
|
|
31
|
+
token.type = 'heading-title';
|
|
32
|
+
token.setAttribute('name', this.name);
|
|
33
|
+
token.setAttribute('stage', 2);
|
|
34
|
+
const trail = new SyntaxToken(input[1], /^[^\S\n]*$/u, 'heading-trail', config, accum, {
|
|
35
|
+
'Stage-1': ':', '!ExtToken': '',
|
|
36
|
+
});
|
|
37
|
+
this.append(token, trail);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @override
|
|
42
|
+
* @this {{prependNewLine(): ''|'\n'} & HeadingToken}
|
|
43
|
+
* @param {string} selector
|
|
44
|
+
* @returns {string}
|
|
45
|
+
*/
|
|
46
|
+
toString(selector) {
|
|
47
|
+
const equals = '='.repeat(Number(this.name));
|
|
48
|
+
return selector && this.matches(selector)
|
|
49
|
+
? ''
|
|
50
|
+
: `${this.prependNewLine()}${equals}${
|
|
51
|
+
this.firstChild.toString(selector)
|
|
52
|
+
}${equals}${this.lastChild.toString(selector)}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @override
|
|
57
|
+
* @this {HeadingToken & {prependNewLine(): ''|'\n'}}
|
|
58
|
+
* @returns {string}
|
|
59
|
+
*/
|
|
60
|
+
text() {
|
|
61
|
+
const equals = '='.repeat(Number(this.name));
|
|
62
|
+
return `${this.prependNewLine()}${equals}${this.firstChild.text()}${equals}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @override */
|
|
66
|
+
getPadding() {
|
|
67
|
+
return super.getPadding() + Number(this.name);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @override */
|
|
71
|
+
getGaps() {
|
|
72
|
+
return Number(this.name);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @override */
|
|
76
|
+
print() {
|
|
77
|
+
const equals = '='.repeat(Number(this.name));
|
|
78
|
+
return super.print({pre: equals, sep: equals});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @override
|
|
83
|
+
* @param {number} start 起始位置
|
|
84
|
+
*/
|
|
85
|
+
lint(start = this.getAbsoluteIndex()) {
|
|
86
|
+
const errors = super.lint(start),
|
|
87
|
+
innerText = String(this.firstChild);
|
|
88
|
+
let refError;
|
|
89
|
+
if (this.name === '1') {
|
|
90
|
+
refError = generateForSelf(this, {start}, '<h1>');
|
|
91
|
+
errors.push(refError);
|
|
92
|
+
}
|
|
93
|
+
if (innerText[0] === '=' || innerText.endsWith('=')) {
|
|
94
|
+
refError ||= generateForSelf(this, {start}, '');
|
|
95
|
+
errors.push({...refError, message: Parser.msg('unbalanced "=" in a section header')});
|
|
96
|
+
}
|
|
97
|
+
if (this.closest('html-attrs, table-attrs')) {
|
|
98
|
+
refError ||= generateForSelf(this, {start}, '');
|
|
99
|
+
errors.push({...refError, message: Parser.msg('section header in a HTML tag')});
|
|
100
|
+
}
|
|
101
|
+
return errors;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @override */
|
|
105
|
+
cloneNode() {
|
|
106
|
+
const [title, trail] = this.cloneChildNodes();
|
|
107
|
+
return Parser.run(() => {
|
|
108
|
+
const token = new HeadingToken(Number(this.name), [], this.getAttribute('config'));
|
|
109
|
+
token.firsthild.safeReplaceWith(title);
|
|
110
|
+
token.lastChild.safeReplaceWith(trail);
|
|
111
|
+
return token;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 设置标题层级
|
|
117
|
+
* @param {number} n 标题层级
|
|
118
|
+
*/
|
|
119
|
+
setLevel(n) {
|
|
120
|
+
if (!Number.isInteger(n)) {
|
|
121
|
+
this.typeError('setLevel', 'Number');
|
|
122
|
+
}
|
|
123
|
+
n = Math.min(Math.max(n, 1), 6);
|
|
124
|
+
this.setAttribute('name', String(n)).firstChild.setAttribute('name', this.name);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 移除标题后的不可见内容 */
|
|
128
|
+
removeTrail() {
|
|
129
|
+
this.lastChild.replaceChildren();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
Parser.classes.HeadingToken = __filename;
|
|
134
|
+
module.exports = HeadingToken;
|