wikiparser-node 0.7.0 → 0.7.1-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.
- package/bundle/bundle.min.js +40 -0
- package/extensions/editor.css +60 -0
- package/extensions/editor.js +324 -0
- package/extensions/ui.css +119 -0
- package/package.json +12 -11
- package/README.md +0 -39
- package/config/default.json +0 -832
- package/config/llwiki.json +0 -630
- package/config/moegirl.json +0 -728
- package/config/zhwiki.json +0 -1269
- package/index.js +0 -321
- package/lib/element.js +0 -611
- package/lib/node.js +0 -772
- package/lib/ranges.js +0 -130
- package/lib/text.js +0 -215
- package/lib/title.js +0 -80
- 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 -65
- package/parser/brackets.js +0 -120
- package/parser/commentAndExt.js +0 -62
- package/parser/converter.js +0 -46
- package/parser/externalLinks.js +0 -33
- package/parser/hrAndDoubleUnderscore.js +0 -38
- 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 -177
- package/parser/table.js +0 -114
- package/src/arg.js +0 -203
- package/src/atom/hidden.js +0 -13
- package/src/atom/index.js +0 -43
- package/src/attribute.js +0 -420
- package/src/attributes.js +0 -452
- package/src/charinsert.js +0 -97
- package/src/converter.js +0 -176
- package/src/converterFlags.js +0 -284
- package/src/converterRule.js +0 -258
- package/src/extLink.js +0 -179
- package/src/gallery.js +0 -151
- 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 -248
- package/src/imageParameter.js +0 -277
- package/src/imagemap.js +0 -199
- package/src/imagemapLink.js +0 -41
- package/src/index.js +0 -913
- package/src/link/category.js +0 -49
- package/src/link/file.js +0 -282
- package/src/link/galleryImage.js +0 -120
- package/src/link/index.js +0 -383
- package/src/magicLink.js +0 -149
- 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 -44
- package/src/parameter.js +0 -239
- package/src/syntax.js +0 -91
- package/src/table/index.js +0 -984
- package/src/table/td.js +0 -339
- package/src/table/tr.js +0 -319
- package/src/tagPair/ext.js +0 -138
- package/src/tagPair/include.js +0 -60
- package/src/tagPair/index.js +0 -126
- package/src/transclude.js +0 -824
- package/tool/index.js +0 -1202
- package/util/base.js +0 -17
- package/util/debug.js +0 -73
- package/util/diff.js +0 -76
- package/util/lint.js +0 -54
- package/util/string.js +0 -107
package/src/extLink.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
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
|
-
const {length} = this;
|
|
105
|
-
return super.print(length > 1 ? {pre: '[', sep: this.#space, post: ']'} : {pre: '[', post: `${this.#space}]`});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** @override */
|
|
109
|
-
cloneNode() {
|
|
110
|
-
const [url, text] = this.cloneChildNodes();
|
|
111
|
-
return Parser.run(() => {
|
|
112
|
-
const token = new ExtLinkToken(undefined, '', '', this.getAttribute('config'));
|
|
113
|
-
token.firstChild.safeReplaceWith(url);
|
|
114
|
-
if (text) {
|
|
115
|
-
token.insertAt(text);
|
|
116
|
-
}
|
|
117
|
-
return token;
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** 修正空白字符 */
|
|
122
|
-
#correct() {
|
|
123
|
-
if (!this.#space && this.length > 1
|
|
124
|
-
// 都替换成`<`肯定不对,但无妨
|
|
125
|
-
&& /^[^[\]<>"{\0-\x1F\x7F\p{Zs}\uFFFD]/u.test(this.lastChild.text().replace(/&[lg]t;/u, '<'))
|
|
126
|
-
) {
|
|
127
|
-
this.#space = ' ';
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 获取网址
|
|
133
|
-
* @this {{firstChild: MagicLinkToken}}
|
|
134
|
-
*/
|
|
135
|
-
getUrl() {
|
|
136
|
-
return this.firstChild.getUrl();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 设置链接目标
|
|
141
|
-
* @param {string|URL} url 网址
|
|
142
|
-
* @throws `SyntaxError` 非法的外链目标
|
|
143
|
-
*/
|
|
144
|
-
setTarget(url) {
|
|
145
|
-
url = String(url);
|
|
146
|
-
const root = Parser.parse(`[${url}]`, this.getAttribute('include'), 8, this.getAttribute('config')),
|
|
147
|
-
{length, firstChild: extLink} = root;
|
|
148
|
-
if (length !== 1 || extLink.type !== 'ext-link' || extLink.length !== 1) {
|
|
149
|
-
throw new SyntaxError(`非法的外链目标:${url}`);
|
|
150
|
-
}
|
|
151
|
-
const {firstChild} = extLink;
|
|
152
|
-
extLink.destroy(true);
|
|
153
|
-
this.firstChild.safeReplaceWith(firstChild);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 设置链接显示文字
|
|
158
|
-
* @param {string} text 链接显示文字
|
|
159
|
-
* @throws `SyntaxError` 非法的链接显示文字
|
|
160
|
-
*/
|
|
161
|
-
setLinkText(text) {
|
|
162
|
-
text = String(text);
|
|
163
|
-
const root = Parser.parse(`[//url ${text}]`, this.getAttribute('include'), 8, this.getAttribute('config')),
|
|
164
|
-
{length, firstChild: extLink} = root;
|
|
165
|
-
if (length !== 1 || extLink.type !== 'ext-link' || extLink.length !== 2) {
|
|
166
|
-
throw new SyntaxError(`非法的外链文字:${noWrap(text)}`);
|
|
167
|
-
}
|
|
168
|
-
const {lastChild} = extLink;
|
|
169
|
-
if (this.length === 1) {
|
|
170
|
-
this.insertAt(lastChild);
|
|
171
|
-
} else {
|
|
172
|
-
this.lastChild.safeReplaceWith(lastChild);
|
|
173
|
-
}
|
|
174
|
-
this.#space ||= ' ';
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
Parser.classes.ExtLinkToken = __filename;
|
|
179
|
-
module.exports = ExtLinkToken;
|
package/src/gallery.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
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
|
-
const newConfig = structuredClone(config);
|
|
30
|
-
newConfig.img = Object.fromEntries(Object.entries(config.img).filter(([, param]) => param !== 'width'));
|
|
31
|
-
for (const line of inner?.split('\n') ?? []) {
|
|
32
|
-
const matches = /^([^|]+)(?:\|(.*))?/u.exec(line);
|
|
33
|
-
if (!matches) {
|
|
34
|
-
super.insertAt(line.trim()
|
|
35
|
-
? new HiddenToken(line, undefined, config, [], {
|
|
36
|
-
AstText: ':',
|
|
37
|
-
})
|
|
38
|
-
: line);
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
const [, file, alt] = matches,
|
|
42
|
-
title = this.normalizeTitle(file, 6, true, true);
|
|
43
|
-
if (title.valid) {
|
|
44
|
-
super.insertAt(new GalleryImageToken(file, alt, title, newConfig, accum));
|
|
45
|
-
} else {
|
|
46
|
-
super.insertAt(new HiddenToken(line, undefined, config, [], {
|
|
47
|
-
AstText: ':',
|
|
48
|
-
}));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @override
|
|
55
|
-
* @param {string} selector
|
|
56
|
-
*/
|
|
57
|
-
toString(selector) {
|
|
58
|
-
return super.toString(selector, '\n');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** @override */
|
|
62
|
-
text() {
|
|
63
|
-
return super.text('\n').replace(/\n\s*\n/gu, '\n');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** @override */
|
|
67
|
-
getGaps() {
|
|
68
|
-
return 1;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** @override */
|
|
72
|
-
print() {
|
|
73
|
-
return super.print({sep: '\n'});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @override
|
|
78
|
-
* @param {number} start 起始位置
|
|
79
|
-
*/
|
|
80
|
-
lint(start = 0) {
|
|
81
|
-
const {top, left} = this.getRootNode().posFromIndex(start),
|
|
82
|
-
/** @type {LintError[]} */ errors = [];
|
|
83
|
-
for (let i = 0, startIndex = start; i < this.length; i++) {
|
|
84
|
-
const child = this.childNodes[i],
|
|
85
|
-
str = String(child),
|
|
86
|
-
{length} = str,
|
|
87
|
-
trimmed = str.trim(),
|
|
88
|
-
startLine = top + i,
|
|
89
|
-
startCol = i ? 0 : left;
|
|
90
|
-
if (child.type === 'hidden' && trimmed && !/^<!--.*-->$/u.test(trimmed)) {
|
|
91
|
-
errors.push({
|
|
92
|
-
message: '图库中的无效内容',
|
|
93
|
-
severity: 'error',
|
|
94
|
-
startIndex,
|
|
95
|
-
endIndex: startIndex + length,
|
|
96
|
-
startLine,
|
|
97
|
-
endLine: startLine,
|
|
98
|
-
startCol,
|
|
99
|
-
endCol: startCol + length,
|
|
100
|
-
excerpt: String(child).slice(0, 50),
|
|
101
|
-
});
|
|
102
|
-
} else if (child.type !== 'hidden' && child.type !== 'text') {
|
|
103
|
-
errors.push(...child.lint(startIndex));
|
|
104
|
-
}
|
|
105
|
-
startIndex += length + 1;
|
|
106
|
-
}
|
|
107
|
-
return errors;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** @override */
|
|
111
|
-
cloneNode() {
|
|
112
|
-
const cloned = this.cloneChildNodes();
|
|
113
|
-
return Parser.run(() => {
|
|
114
|
-
const token = new GalleryToken(undefined, this.getAttribute('config'));
|
|
115
|
-
token.append(...cloned);
|
|
116
|
-
return token;
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 插入图片
|
|
122
|
-
* @param {string} file 图片文件名
|
|
123
|
-
* @param {number} i 插入位置
|
|
124
|
-
* @throws `SyntaxError` 非法的文件名
|
|
125
|
-
*/
|
|
126
|
-
insertImage(file, i = this.length) {
|
|
127
|
-
const title = this.normalizeTitle(file, 6, true, true);
|
|
128
|
-
if (title.valid) {
|
|
129
|
-
const token = Parser.run(() => new GalleryImageToken(file, undefined, title, this.getAttribute('config')));
|
|
130
|
-
return this.insertAt(token, i);
|
|
131
|
-
}
|
|
132
|
-
throw new SyntaxError(`非法的文件名:${file}`);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @override
|
|
137
|
-
* @template {string|Token} T
|
|
138
|
-
* @param {T} token 待插入的节点
|
|
139
|
-
* @param {number} i 插入位置
|
|
140
|
-
* @throws `RangeError` 插入不可见内容
|
|
141
|
-
*/
|
|
142
|
-
insertAt(token, i = 0) {
|
|
143
|
-
if (typeof token === 'string' && token.trim() || token instanceof HiddenToken) {
|
|
144
|
-
throw new RangeError('请勿向图库中插入不可见内容!');
|
|
145
|
-
}
|
|
146
|
-
return super.insertAt(token, i);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
Parser.classes.GalleryToken = __filename;
|
|
151
|
-
module.exports = GalleryToken;
|
package/src/hasNowiki/index.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
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;
|
package/src/hasNowiki/pre.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
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', appendNewLine(): ''|'\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)}${this.appendNewLine()}`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @override
|
|
57
|
-
* @this {HeadingToken & {prependNewLine(): ''|'\n', appendNewLine(): ''|'\n'}}
|
|
58
|
-
* @returns {string}
|
|
59
|
-
*/
|
|
60
|
-
text() {
|
|
61
|
-
const equals = '='.repeat(Number(this.name));
|
|
62
|
-
return `${this.prependNewLine()}${equals}${this.firstChild.text()}${equals}${this.appendNewLine()}`;
|
|
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 = 0) {
|
|
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.at(-1) === '=') {
|
|
94
|
-
refError ||= generateForSelf(this, {start}, '');
|
|
95
|
-
errors.push({...refError, message: '段落标题中不平衡的"="'});
|
|
96
|
-
}
|
|
97
|
-
if (this.closest('html-attrs, table-attrs')) {
|
|
98
|
-
refError ||= generateForSelf(this, {start}, '');
|
|
99
|
-
errors.push({...refError, message: 'HTML标签属性中的段落标题'});
|
|
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;
|