wikiparser-node 0.11.0-b → 0.11.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/config/.schema.json +18 -0
- package/config/default.json +1 -0
- package/config/llwiki.json +35 -0
- package/config/moegirl.json +44 -0
- package/config/zhwiki.json +466 -0
- package/dist/index.d.ts +114 -0
- package/dist/lib/element.d.ts +162 -0
- package/dist/lib/node.d.ts +291 -0
- package/dist/lib/ranges.d.ts +37 -0
- package/dist/lib/text.d.ts +64 -0
- package/dist/lib/title.d.ts +21 -0
- package/dist/mixin/attributeParent.d.ts +9 -0
- package/dist/mixin/fixedToken.d.ts +8 -0
- package/dist/mixin/hidden.d.ts +8 -0
- package/dist/mixin/singleLine.d.ts +8 -0
- package/dist/mixin/sol.d.ts +8 -0
- package/dist/parser/brackets.d.ts +12 -0
- package/dist/parser/commentAndExt.d.ts +8 -0
- package/dist/parser/converter.d.ts +7 -0
- package/dist/parser/externalLinks.d.ts +7 -0
- package/dist/parser/hrAndDoubleUnderscore.d.ts +11 -0
- package/dist/parser/html.d.ts +7 -0
- package/dist/parser/links.d.ts +7 -0
- package/dist/parser/list.d.ts +7 -0
- package/dist/parser/magicLinks.d.ts +7 -0
- package/dist/parser/quotes.d.ts +7 -0
- package/dist/parser/selector.d.ts +12 -0
- package/dist/parser/table.d.ts +11 -0
- package/dist/src/arg.d.ts +54 -0
- package/dist/src/atom/hidden.d.ts +5 -0
- package/dist/src/atom/index.d.ts +15 -0
- package/dist/src/attribute.d.ts +65 -0
- package/dist/src/attributes.d.ts +112 -0
- package/dist/src/charinsert.d.ts +32 -0
- package/dist/src/converter.d.ts +103 -0
- package/dist/src/converterFlags.d.ts +83 -0
- package/dist/src/converterRule.d.ts +75 -0
- package/dist/src/extLink.d.ts +62 -0
- package/dist/src/gallery.d.ts +33 -0
- package/dist/src/hasNowiki/index.d.ts +14 -0
- package/dist/src/hasNowiki/pre.d.ts +13 -0
- package/dist/src/heading.d.ts +44 -0
- package/dist/src/html.d.ts +56 -0
- package/dist/src/imageParameter.d.ts +65 -0
- package/dist/src/imagemap.d.ts +37 -0
- package/dist/src/imagemapLink.d.ts +21 -0
- package/dist/src/index.d.ts +186 -0
- package/dist/src/link/category.d.ts +16 -0
- package/dist/src/link/file.d.ts +85 -0
- package/dist/src/link/galleryImage.d.ts +15 -0
- package/dist/src/link/index.d.ts +88 -0
- package/dist/src/magicLink.d.ts +36 -0
- package/dist/src/nested/choose.d.ts +13 -0
- package/dist/src/nested/combobox.d.ts +13 -0
- package/dist/src/nested/index.d.ts +18 -0
- package/dist/src/nested/references.d.ts +13 -0
- package/dist/src/nowiki/comment.d.ts +31 -0
- package/dist/src/nowiki/dd.d.ts +17 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +22 -0
- package/dist/src/nowiki/hr.d.ts +13 -0
- package/dist/src/nowiki/index.d.ts +27 -0
- package/dist/src/nowiki/list.d.ts +8 -0
- package/dist/src/nowiki/noinclude.d.ts +8 -0
- package/dist/src/nowiki/quote.d.ts +13 -0
- package/dist/src/onlyinclude.d.ts +24 -0
- package/dist/src/paramTag/index.d.ts +29 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/parameter.d.ts +75 -0
- package/dist/src/syntax.d.ts +20 -0
- package/dist/src/table/index.d.ts +273 -0
- package/dist/src/table/td.d.ts +100 -0
- package/dist/src/table/tr.d.ts +91 -0
- package/dist/src/tagPair/ext.d.ts +18 -0
- package/dist/src/tagPair/include.d.ts +25 -0
- package/dist/src/tagPair/index.d.ts +41 -0
- package/dist/src/transclude.d.ts +199 -0
- package/dist/tool/index.d.ts +420 -0
- package/dist/util/base.d.ts +10 -0
- package/dist/util/debug.d.ts +20 -0
- package/dist/util/diff.d.ts +8 -0
- package/dist/util/lint.d.ts +28 -0
- package/dist/util/string.d.ts +55 -0
- package/index.js +333 -0
- package/lib/element.js +618 -0
- package/lib/node.js +730 -0
- package/lib/ranges.js +130 -0
- package/lib/text.js +265 -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 +17 -13
- package/parser/brackets.js +128 -0
- package/parser/commentAndExt.js +62 -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 +180 -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 +472 -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 +938 -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 +151 -0
- package/src/nested/choose.js +24 -0
- package/src/nested/combobox.js +23 -0
- package/src/nested/index.js +96 -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 +985 -0
- package/src/table/td.js +343 -0
- package/src/table/tr.js +319 -0
- package/src/tagPair/ext.js +146 -0
- package/src/tagPair/include.js +50 -0
- package/src/tagPair/index.js +131 -0
- package/src/transclude.js +843 -0
- package/tool/index.js +1209 -0
- package/typings/api.d.ts +9 -0
- package/typings/array.d.ts +29 -0
- package/typings/event.d.ts +22 -0
- package/typings/index.d.ts +118 -0
- package/typings/node.d.ts +35 -0
- package/typings/parser.d.ts +12 -0
- package/typings/table.d.ts +10 -0
- package/typings/token.d.ts +31 -0
- package/typings/tool.d.ts +6 -0
- package/util/base.js +17 -0
- package/util/debug.js +73 -0
- package/util/diff.js +76 -0
- package/util/lint.js +57 -0
- package/util/string.js +126 -0
- package/bundle/bundle.min.js +0 -38
- package/config/minimum.json +0 -135
- package/extensions/base.js +0 -154
- package/extensions/editor.css +0 -63
- package/extensions/editor.js +0 -183
- package/extensions/highlight.js +0 -40
- package/extensions/ui.css +0 -119
package/lib/ranges.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..');
|
|
4
|
+
|
|
5
|
+
/** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
|
|
6
|
+
class Range {
|
|
7
|
+
start;
|
|
8
|
+
end;
|
|
9
|
+
step;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string|Range} str 表达式
|
|
13
|
+
* @throws `RangeError` 起点、终点和步长均应为整数
|
|
14
|
+
* @throws `RangeError` n的系数不能为0
|
|
15
|
+
* @throws `RangeError` 应使用CSS选择器或Python切片的格式
|
|
16
|
+
*/
|
|
17
|
+
constructor(str) {
|
|
18
|
+
if (str instanceof Range) {
|
|
19
|
+
Object.assign(this, str);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
str = str.trim();
|
|
23
|
+
if (str === 'odd') {
|
|
24
|
+
Object.assign(this, {start: 1, end: Infinity, step: 2});
|
|
25
|
+
} else if (str === 'even') {
|
|
26
|
+
Object.assign(this, {start: 0, end: Infinity, step: 2});
|
|
27
|
+
} else if (str.includes(':')) {
|
|
28
|
+
const [start, end, step = '1'] = str.split(':', 3);
|
|
29
|
+
this.start = Number(start);
|
|
30
|
+
this.end = Number(end || Infinity);
|
|
31
|
+
this.step = Math.max(Number(step), 1);
|
|
32
|
+
if (!Number.isInteger(this.start)) {
|
|
33
|
+
throw new RangeError(`起点 ${this.start} 应为整数!`);
|
|
34
|
+
} else if (this.end !== Infinity && !Number.isInteger(this.end)) {
|
|
35
|
+
throw new RangeError(`终点 ${this.end} 应为整数!`);
|
|
36
|
+
} else if (!Number.isInteger(this.step)) {
|
|
37
|
+
throw new RangeError(`步长 ${this.step} 应为整数!`);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
const mt = /^([+-])?(\d+)?n(?:([+-])(\d+))?$/u.exec(str);
|
|
41
|
+
if (mt) {
|
|
42
|
+
const [, sgnA = '+', a = 1, sgnB = '+'] = mt,
|
|
43
|
+
b = Number(mt[4] ?? 0);
|
|
44
|
+
this.step = Number(a);
|
|
45
|
+
if (this.step === 0) {
|
|
46
|
+
throw new RangeError(`参数 ${str} 中 "n" 的系数不允许为 0!`);
|
|
47
|
+
} else if (sgnA === '+') {
|
|
48
|
+
this.start = sgnB === '+' || b === 0 ? b : this.step - 1 - (b - 1) % this.step;
|
|
49
|
+
this.end = Infinity;
|
|
50
|
+
} else if (sgnB === '-') {
|
|
51
|
+
this.start = 0;
|
|
52
|
+
this.end = b > 0 ? 0 : this.step;
|
|
53
|
+
} else {
|
|
54
|
+
this.start = b % this.step;
|
|
55
|
+
this.end = this.step + b;
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
throw new RangeError(`参数 ${str} 应写作CSS选择器的 "an+b" 形式或Python切片!`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 将Range转换为针对特定数组的下标集
|
|
65
|
+
* @param {number|*[]} arr 参考数组
|
|
66
|
+
* @complexity `n`
|
|
67
|
+
*/
|
|
68
|
+
applyTo(arr) {
|
|
69
|
+
return new Array(typeof arr === 'number' ? arr : arr.length).fill().map((_, i) => i).slice(this.start, this.end)
|
|
70
|
+
.filter((_, j) => j % this.step === 0);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** @extends {Array<number|Range>} */
|
|
75
|
+
class Ranges extends Array {
|
|
76
|
+
/** @param {number|string|Range|(number|string|Range)[]} arr 表达式数组 */
|
|
77
|
+
constructor(arr) {
|
|
78
|
+
super();
|
|
79
|
+
if (arr === undefined) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
arr = Array.isArray(arr) ? arr : [arr];
|
|
83
|
+
for (const ele of arr) {
|
|
84
|
+
if (ele instanceof Range) {
|
|
85
|
+
this.push(new Range(ele));
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const number = Number(ele);
|
|
89
|
+
if (Number.isInteger(number)) {
|
|
90
|
+
this.push(number);
|
|
91
|
+
} else if (typeof ele === 'string' && Number.isNaN(number)) {
|
|
92
|
+
try {
|
|
93
|
+
const range = new Range(ele);
|
|
94
|
+
this.push(range);
|
|
95
|
+
} catch {}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 将Ranges转换为针对特定Array的下标集
|
|
102
|
+
* @param {number|*[]} arr 参考数组
|
|
103
|
+
* @complexity `n`
|
|
104
|
+
*/
|
|
105
|
+
applyTo(arr) {
|
|
106
|
+
const length = typeof arr === 'number' ? arr : arr.length;
|
|
107
|
+
return [
|
|
108
|
+
...new Set(
|
|
109
|
+
[...this].flatMap(ele => {
|
|
110
|
+
if (typeof ele === 'number') {
|
|
111
|
+
return ele < 0 ? ele + length : ele;
|
|
112
|
+
}
|
|
113
|
+
return ele.applyTo(length);
|
|
114
|
+
}),
|
|
115
|
+
),
|
|
116
|
+
].filter(i => i >= 0 && i < length).sort();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 检查某个下标是否符合表达式
|
|
121
|
+
* @param {string} str 表达式
|
|
122
|
+
* @param {number} i 待检查的下标
|
|
123
|
+
*/
|
|
124
|
+
static nth(str, i) {
|
|
125
|
+
return new Ranges(str.split(',')).applyTo(i + 1).includes(i);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
Parser.classes.Ranges = __filename;
|
|
130
|
+
module.exports = Ranges;
|
package/lib/text.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/** @typedef {import('../typings/token').LintError} LintError */
|
|
4
|
+
|
|
5
|
+
const Parser = require('..'),
|
|
6
|
+
AstNode = require('./node'),
|
|
7
|
+
AstElement = require('./element');
|
|
8
|
+
|
|
9
|
+
const errorSyntax = /https?:\/\/|\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu,
|
|
10
|
+
errorSyntaxUrl = /\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu,
|
|
11
|
+
disallowedTags = [
|
|
12
|
+
'html',
|
|
13
|
+
'base',
|
|
14
|
+
'head',
|
|
15
|
+
'style',
|
|
16
|
+
'title',
|
|
17
|
+
'body',
|
|
18
|
+
'menu',
|
|
19
|
+
'a',
|
|
20
|
+
'area',
|
|
21
|
+
'audio',
|
|
22
|
+
'img',
|
|
23
|
+
'map',
|
|
24
|
+
'track',
|
|
25
|
+
'video',
|
|
26
|
+
'embed',
|
|
27
|
+
'iframe',
|
|
28
|
+
'object',
|
|
29
|
+
'picture',
|
|
30
|
+
'source',
|
|
31
|
+
'canvas',
|
|
32
|
+
'script',
|
|
33
|
+
'col',
|
|
34
|
+
'colgroup',
|
|
35
|
+
'tbody',
|
|
36
|
+
'tfoot',
|
|
37
|
+
'thead',
|
|
38
|
+
'button',
|
|
39
|
+
'datalist',
|
|
40
|
+
'fieldset',
|
|
41
|
+
'form',
|
|
42
|
+
'input',
|
|
43
|
+
'label',
|
|
44
|
+
'legend',
|
|
45
|
+
'meter',
|
|
46
|
+
'optgroup',
|
|
47
|
+
'option',
|
|
48
|
+
'output',
|
|
49
|
+
'progress',
|
|
50
|
+
'select',
|
|
51
|
+
'textarea',
|
|
52
|
+
'details',
|
|
53
|
+
'dialog',
|
|
54
|
+
'slot',
|
|
55
|
+
'template',
|
|
56
|
+
'dir',
|
|
57
|
+
'frame',
|
|
58
|
+
'frameset',
|
|
59
|
+
'marquee',
|
|
60
|
+
'param',
|
|
61
|
+
'xmp',
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
/** 文本节点 */
|
|
65
|
+
class AstText extends AstNode {
|
|
66
|
+
type = 'text';
|
|
67
|
+
/** @type {string} */ data;
|
|
68
|
+
|
|
69
|
+
/** 文本长度 */
|
|
70
|
+
get length() {
|
|
71
|
+
return this.data.length;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** @param {string} text 包含文本 */
|
|
75
|
+
constructor(text = '') {
|
|
76
|
+
super();
|
|
77
|
+
Object.defineProperties(this, {
|
|
78
|
+
data: {value: text, writable: false},
|
|
79
|
+
childNodes: {enumerable: false, configurable: false},
|
|
80
|
+
type: {enumerable: false, writable: false, configurable: false},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 输出字符串 */
|
|
85
|
+
toString() {
|
|
86
|
+
return this.data;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @override */
|
|
90
|
+
text() {
|
|
91
|
+
return this.data;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Linter
|
|
96
|
+
* @this {AstText & {parentNode: AstElement}}
|
|
97
|
+
* @param {number} start 起始位置
|
|
98
|
+
* @returns {LintError[]}
|
|
99
|
+
*/
|
|
100
|
+
lint(start = this.getAbsoluteIndex()) {
|
|
101
|
+
const {data, parentNode, nextSibling, previousSibling} = this,
|
|
102
|
+
type = parentNode?.type,
|
|
103
|
+
name = parentNode?.name,
|
|
104
|
+
nextType = nextSibling?.type,
|
|
105
|
+
previousType = previousSibling?.type,
|
|
106
|
+
errorRegex
|
|
107
|
+
= type === 'free-ext-link' || type === 'ext-link-url' || type === 'image-parameter' && name === 'link'
|
|
108
|
+
? errorSyntaxUrl
|
|
109
|
+
: errorSyntax,
|
|
110
|
+
errors = [...data.matchAll(errorRegex)],
|
|
111
|
+
{ext, html} = this.getRootNode().getAttribute('config');
|
|
112
|
+
if (errors.length > 0) {
|
|
113
|
+
const root = this.getRootNode(),
|
|
114
|
+
{top, left} = root.posFromIndex(start),
|
|
115
|
+
tags = new Set([ext, html, disallowedTags].flat(2));
|
|
116
|
+
return errors.map(/** @returns {LintError} */ ({0: error, 1: prefix, 2: tag, index}) => {
|
|
117
|
+
if (prefix) {
|
|
118
|
+
index += prefix.length;
|
|
119
|
+
error = error.slice(prefix.length);
|
|
120
|
+
}
|
|
121
|
+
const startIndex = start + index,
|
|
122
|
+
lines = data.slice(0, index).split('\n'),
|
|
123
|
+
startLine = lines.length + top - 1,
|
|
124
|
+
line = lines.at(-1),
|
|
125
|
+
startCol = lines.length > 1 ? line.length : left + line.length,
|
|
126
|
+
{0: char, length} = error,
|
|
127
|
+
endIndex = startIndex + length,
|
|
128
|
+
end = char === '}' || char === ']' ? endIndex + 1 : startIndex + 49,
|
|
129
|
+
rootStr = String(root),
|
|
130
|
+
nextChar = rootStr[endIndex],
|
|
131
|
+
previousChar = rootStr[startIndex - 1],
|
|
132
|
+
severity = length > 1 && (char !== '<' || /[\s/>]/u.test(nextChar))
|
|
133
|
+
|| char === '{' && (nextChar === char || previousChar === '-')
|
|
134
|
+
|| char === '}' && (previousChar === char || nextChar === '-')
|
|
135
|
+
|| char === '[' && (
|
|
136
|
+
nextChar === char || type === 'ext-link-text'
|
|
137
|
+
|| !data.slice(index + 1).trim() && nextType === 'free-ext-link'
|
|
138
|
+
)
|
|
139
|
+
|| char === ']' && (
|
|
140
|
+
previousChar === char
|
|
141
|
+
|| !data.slice(0, index).trim() && previousType === 'free-ext-link'
|
|
142
|
+
)
|
|
143
|
+
? 'error'
|
|
144
|
+
: 'warning';
|
|
145
|
+
return (char !== 'h' || index > 0) && (char !== '<' || tags.has(tag.toLowerCase())) && {
|
|
146
|
+
message: Parser.msg('lonely "$1"', char === 'h' ? error : char),
|
|
147
|
+
severity,
|
|
148
|
+
startIndex,
|
|
149
|
+
endIndex,
|
|
150
|
+
startLine,
|
|
151
|
+
endLine: startLine,
|
|
152
|
+
startCol,
|
|
153
|
+
endCol: startCol + length,
|
|
154
|
+
excerpt: rootStr.slice(Math.max(0, end - 50), end),
|
|
155
|
+
};
|
|
156
|
+
}).filter(Boolean);
|
|
157
|
+
}
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 修改内容
|
|
163
|
+
* @param {string} text 新内容
|
|
164
|
+
*/
|
|
165
|
+
#setData(text) {
|
|
166
|
+
text = String(text);
|
|
167
|
+
const {data} = this,
|
|
168
|
+
e = new Event('text', {bubbles: true});
|
|
169
|
+
this.setAttribute('data', text);
|
|
170
|
+
if (data !== text) {
|
|
171
|
+
this.dispatchEvent(e, {oldText: data, newText: text});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 替换字符串
|
|
177
|
+
* @param {string} text 替换的字符串
|
|
178
|
+
*/
|
|
179
|
+
replaceData(text = '') {
|
|
180
|
+
this.#setData(text);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 复制 */
|
|
184
|
+
cloneNode() {
|
|
185
|
+
return new AstText(this.data);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @override
|
|
190
|
+
* @template {string} T
|
|
191
|
+
* @param {T} key 属性键
|
|
192
|
+
* @returns {import('../typings/node').TokenAttribute<T>}
|
|
193
|
+
* @throws `Error` 文本节点没有子节点
|
|
194
|
+
*/
|
|
195
|
+
getAttribute(key) {
|
|
196
|
+
return key === 'verifyChild'
|
|
197
|
+
? () => {
|
|
198
|
+
throw new Error('文本节点没有子节点!');
|
|
199
|
+
}
|
|
200
|
+
: super.getAttribute(key);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* 在后方添加字符串
|
|
205
|
+
* @param {string} text 添加的字符串
|
|
206
|
+
*/
|
|
207
|
+
appendData(text) {
|
|
208
|
+
this.#setData(this.data + text);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 删减字符串
|
|
213
|
+
* @param {number} offset 起始位置
|
|
214
|
+
* @param {number} count 删减字符数
|
|
215
|
+
*/
|
|
216
|
+
deleteData(offset, count) {
|
|
217
|
+
this.#setData(this.data.slice(0, offset) + this.data.slice(offset + count));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 插入字符串
|
|
222
|
+
* @param {number} offset 插入位置
|
|
223
|
+
* @param {string} text 待插入的字符串
|
|
224
|
+
*/
|
|
225
|
+
insertData(offset, text) {
|
|
226
|
+
this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 提取子串
|
|
231
|
+
* @param {number} offset 起始位置
|
|
232
|
+
* @param {number} count 字符数
|
|
233
|
+
*/
|
|
234
|
+
substringData(offset, count) {
|
|
235
|
+
return this.data.slice(offset, offset + count);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 将文本子节点分裂为两部分
|
|
240
|
+
* @param {number} offset 分裂位置
|
|
241
|
+
* @throws `RangeError` 错误的断开位置
|
|
242
|
+
* @throws `Error` 没有父节点
|
|
243
|
+
*/
|
|
244
|
+
splitText(offset) {
|
|
245
|
+
if (!Number.isInteger(offset)) {
|
|
246
|
+
this.typeError('splitText', 'Number');
|
|
247
|
+
} else if (offset > this.length || offset < -this.length) {
|
|
248
|
+
throw new RangeError(`错误的断开位置!${offset}`);
|
|
249
|
+
}
|
|
250
|
+
const {parentNode, data} = this;
|
|
251
|
+
if (!parentNode) {
|
|
252
|
+
throw new Error('待分裂的文本节点没有父节点!');
|
|
253
|
+
}
|
|
254
|
+
const newText = new AstText(data.slice(offset)),
|
|
255
|
+
childNodes = [...parentNode.childNodes];
|
|
256
|
+
this.setAttribute('data', data.slice(0, offset));
|
|
257
|
+
childNodes.splice(childNodes.indexOf(this) + 1, 0, newText);
|
|
258
|
+
newText.setAttribute('parentNode', parentNode);
|
|
259
|
+
parentNode.setAttribute('childNodes', childNodes);
|
|
260
|
+
return newText;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
Parser.classes.AstText = __filename;
|
|
265
|
+
module.exports = AstText;
|
package/lib/title.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {decodeHtml} = require('../util/string'),
|
|
4
|
+
Parser = require('..');
|
|
5
|
+
|
|
6
|
+
/** MediaWiki页面标题对象 */
|
|
7
|
+
class Title {
|
|
8
|
+
valid = true;
|
|
9
|
+
ns = 0;
|
|
10
|
+
fragment;
|
|
11
|
+
encoded = false;
|
|
12
|
+
title = '';
|
|
13
|
+
main = '';
|
|
14
|
+
prefix = '';
|
|
15
|
+
interwiki = '';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} title 标题(含或不含命名空间前缀)
|
|
19
|
+
* @param {number} defaultNs 命名空间
|
|
20
|
+
* @param {boolean} decode 是否需要解码
|
|
21
|
+
* @param {boolean} selfLink 是否允许selfLink
|
|
22
|
+
*/
|
|
23
|
+
constructor(title, defaultNs = 0, config = Parser.getConfig(), decode = false, selfLink = false) {
|
|
24
|
+
const {namespaces, nsid} = config;
|
|
25
|
+
let namespace = namespaces[defaultNs];
|
|
26
|
+
title = decodeHtml(title);
|
|
27
|
+
if (decode && title.includes('%')) {
|
|
28
|
+
try {
|
|
29
|
+
const encoded = /%(?!21|3[ce]|5[bd]|7[b-d])[\da-f]{2}/iu.test(title);
|
|
30
|
+
title = decodeURIComponent(title);
|
|
31
|
+
this.encoded = encoded;
|
|
32
|
+
} catch {}
|
|
33
|
+
}
|
|
34
|
+
title = title.replaceAll('_', ' ').trim();
|
|
35
|
+
if (title[0] === ':') {
|
|
36
|
+
namespace = '';
|
|
37
|
+
title = title.slice(1).trim();
|
|
38
|
+
}
|
|
39
|
+
const iw = defaultNs ? undefined : Parser.isInterwiki(title, config);
|
|
40
|
+
if (iw) {
|
|
41
|
+
this.interwiki = iw[1].toLowerCase();
|
|
42
|
+
title = title.slice(iw[0].length);
|
|
43
|
+
}
|
|
44
|
+
const m = title.split(':');
|
|
45
|
+
if (m.length > 1) {
|
|
46
|
+
const id = namespaces[nsid[m[0].trim().toLowerCase()]];
|
|
47
|
+
if (id !== undefined) {
|
|
48
|
+
namespace = id;
|
|
49
|
+
title = m.slice(1).join(':').trim();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
this.ns = nsid[namespace.toLowerCase()];
|
|
53
|
+
const i = title.indexOf('#');
|
|
54
|
+
let fragment;
|
|
55
|
+
if (i !== -1) {
|
|
56
|
+
fragment = title.slice(i + 1).trimEnd();
|
|
57
|
+
if (fragment.includes('%')) {
|
|
58
|
+
try {
|
|
59
|
+
fragment = decodeURIComponent(fragment);
|
|
60
|
+
} catch {}
|
|
61
|
+
} else if (fragment.includes('.')) {
|
|
62
|
+
try {
|
|
63
|
+
fragment = decodeURIComponent(fragment.replaceAll('.', '%'));
|
|
64
|
+
} catch {}
|
|
65
|
+
}
|
|
66
|
+
title = title.slice(0, i).trim();
|
|
67
|
+
}
|
|
68
|
+
this.valid = Boolean(title || selfLink && fragment !== undefined || this.interwiki)
|
|
69
|
+
&& !/\0\d+[eh!+-]\x7F|[<>[\]{}|]|%[\da-f]{2}/iu.test(title);
|
|
70
|
+
this.fragment = fragment;
|
|
71
|
+
this.main = title && `${title[0].toUpperCase()}${title.slice(1)}`;
|
|
72
|
+
this.prefix = `${namespace}${namespace && ':'}`;
|
|
73
|
+
this.title = `${iw ? `${this.interwiki}:` : ''}${this.prefix}${this.main.replaceAll(' ', '_')}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @override */
|
|
77
|
+
toString() {
|
|
78
|
+
return `${this.title}${this.fragment === undefined ? '' : `#${this.fragment}`}`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
Parser.classes.Title = __filename;
|
|
83
|
+
module.exports = Title;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..'),
|
|
4
|
+
AttributesToken = require('../src/attributes');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 子节点含有AttributesToken的类
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {T} Constructor 基类
|
|
10
|
+
* @param {number} i AttributesToken子节点的位置
|
|
11
|
+
* @returns {T}
|
|
12
|
+
*/
|
|
13
|
+
const attributeParent = (Constructor, i = 0) => class extends Constructor {
|
|
14
|
+
/** getAttrs()方法的getter写法 */
|
|
15
|
+
get attributes() {
|
|
16
|
+
return this.getAttrs();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 以字符串表示的class属性 */
|
|
20
|
+
get className() {
|
|
21
|
+
const attr = this.getAttr('class');
|
|
22
|
+
return typeof attr === 'string' ? attr : '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
set className(className) {
|
|
26
|
+
this.setAttr('class', className);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 以Set表示的class属性 */
|
|
30
|
+
get classList() {
|
|
31
|
+
return new Set(this.className.split(/\s/u));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** id属性 */
|
|
35
|
+
get id() {
|
|
36
|
+
const attr = this.getAttr('id');
|
|
37
|
+
return typeof attr === 'string' ? attr : '';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
set id(id) {
|
|
41
|
+
this.setAttr('id', id);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* AttributesToken子节点是否具有某属性
|
|
46
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
47
|
+
* @param {string} key 属性键
|
|
48
|
+
*/
|
|
49
|
+
hasAttr(key) {
|
|
50
|
+
return this.childNodes.at(i).hasAttr(key);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 获取AttributesToken子节点的属性
|
|
55
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
56
|
+
* @param {string} key 属性键
|
|
57
|
+
*/
|
|
58
|
+
getAttr(key) {
|
|
59
|
+
return this.childNodes.at(i).getAttr(key);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 列举AttributesToken子节点的属性键
|
|
64
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
65
|
+
*/
|
|
66
|
+
getAttrNames() {
|
|
67
|
+
return this.childNodes.at(i).getAttrNames();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* AttributesToken子节点是否具有任意属性
|
|
72
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
73
|
+
*/
|
|
74
|
+
hasAttrs() {
|
|
75
|
+
return this.childNodes.at(i).hasAttrs();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 获取AttributesToken子节点的全部标签属性
|
|
80
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
81
|
+
*/
|
|
82
|
+
getAttrs() {
|
|
83
|
+
return this.childNodes.at(i).getAttrs();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 对AttributesToken子节点设置属性
|
|
88
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
89
|
+
* @param {string} key 属性键
|
|
90
|
+
* @param {string|boolean} value 属性值
|
|
91
|
+
*/
|
|
92
|
+
setAttr(key, value) {
|
|
93
|
+
return this.childNodes.at(i).setAttr(key, value);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 移除AttributesToken子节点的某属性
|
|
98
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
99
|
+
* @param {string} key 属性键
|
|
100
|
+
*/
|
|
101
|
+
removeAttr(key) {
|
|
102
|
+
this.childNodes.at(i).removeAttr(key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 开关AttributesToken子节点的某属性
|
|
107
|
+
* @this {{childNodes: AttributesToken[]}}
|
|
108
|
+
* @param {string} key 属性键
|
|
109
|
+
* @param {boolean|undefined} force 强制开启或关闭
|
|
110
|
+
*/
|
|
111
|
+
toggleAttr(key, force) {
|
|
112
|
+
this.childNodes.at(i).toggleAttr(key, force);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
Parser.mixins.attributeParent = __filename;
|
|
117
|
+
module.exports = attributeParent;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..'),
|
|
4
|
+
Token = require('../src');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 不可增删子节点的类
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {T} Constructor 基类
|
|
10
|
+
* @returns {T}
|
|
11
|
+
*/
|
|
12
|
+
const fixedToken = Constructor => class extends Constructor {
|
|
13
|
+
static fixed = true;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 移除子节点
|
|
17
|
+
* @throws `Error`
|
|
18
|
+
*/
|
|
19
|
+
removeAt() {
|
|
20
|
+
throw new Error(`${this.constructor.name} 不可删除元素!`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 插入子节点
|
|
25
|
+
* @template {Token} T
|
|
26
|
+
* @param {T} token 待插入的子节点
|
|
27
|
+
* @param {number} i 插入位置
|
|
28
|
+
* @throws `Error`
|
|
29
|
+
*/
|
|
30
|
+
insertAt(token, i = this.length) {
|
|
31
|
+
if (Parser.running) {
|
|
32
|
+
super.insertAt(token, i);
|
|
33
|
+
return token;
|
|
34
|
+
}
|
|
35
|
+
throw new Error(`${this.constructor.name} 不可插入元素!`);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
Parser.mixins.fixedToken = __filename;
|
|
40
|
+
module.exports = fixedToken;
|
package/mixin/hidden.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 解析后不可见的类
|
|
7
|
+
* @template T
|
|
8
|
+
* @param {T} Constructor 基类
|
|
9
|
+
* @returns {T}
|
|
10
|
+
*/
|
|
11
|
+
const hidden = Constructor => class extends Constructor {
|
|
12
|
+
static hidden = true;
|
|
13
|
+
|
|
14
|
+
/** 没有可见部分 */
|
|
15
|
+
text() { // eslint-disable-line class-methods-use-this
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
Parser.mixins.hidden = __filename;
|
|
21
|
+
module.exports = hidden;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Parser = require('..');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 不可包含换行符的类
|
|
7
|
+
* @template {Function} T
|
|
8
|
+
* @param {T} Constructor 基类
|
|
9
|
+
* @returns {T}
|
|
10
|
+
*/
|
|
11
|
+
const singleLine = Constructor => {
|
|
12
|
+
const SingleLineConstructor = class extends Constructor {
|
|
13
|
+
/**
|
|
14
|
+
* @override
|
|
15
|
+
* @param {string} selector
|
|
16
|
+
*/
|
|
17
|
+
toString(selector) {
|
|
18
|
+
return super.toString(selector).replaceAll('\n', ' ');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @override */
|
|
22
|
+
text() {
|
|
23
|
+
return super.text().replaceAll('\n', ' ');
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(SingleLineConstructor, 'name', {value: `SingleLine${Constructor.name}`});
|
|
27
|
+
return SingleLineConstructor;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
Parser.mixins.singleLine = __filename;
|
|
31
|
+
module.exports = singleLine;
|