wikiparser-node 0.11.0 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/.schema.json +26 -0
- package/dist/index.d.ts +61 -113
- package/dist/index.js +314 -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 +26 -12
- package/dist/lib/title.js +108 -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 +118 -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 +35 -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 +169 -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 +21 -24
- 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
package/dist/lib/text.d.ts
CHANGED
|
@@ -1,64 +1,72 @@
|
|
|
1
|
-
|
|
1
|
+
import * as Parser from '../index';
|
|
2
|
+
import * as AstNode from './node';
|
|
2
3
|
/** 文本节点 */
|
|
3
4
|
declare class AstText extends AstNode {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
#private;
|
|
6
|
+
/** @browser */
|
|
7
|
+
readonly type = "text";
|
|
8
|
+
name: undefined;
|
|
9
|
+
/** @browser */
|
|
10
|
+
data: string;
|
|
7
11
|
/** 文本长度 */
|
|
8
12
|
get length(): number;
|
|
9
|
-
/**
|
|
10
|
-
|
|
13
|
+
/**
|
|
14
|
+
* @browser
|
|
15
|
+
* @param text 包含文本
|
|
16
|
+
*/
|
|
17
|
+
constructor(text?: string);
|
|
18
|
+
/**
|
|
19
|
+
* 输出字符串
|
|
20
|
+
* @browser
|
|
21
|
+
*/
|
|
22
|
+
toString(): string;
|
|
23
|
+
/**
|
|
24
|
+
* 可见部分
|
|
25
|
+
* @browser
|
|
26
|
+
*/
|
|
27
|
+
text(): string;
|
|
11
28
|
/**
|
|
12
29
|
* Linter
|
|
13
|
-
* @
|
|
14
|
-
* @param {number} start 起始位置
|
|
15
|
-
* @returns {LintError[]}
|
|
30
|
+
* @browser
|
|
16
31
|
*/
|
|
17
|
-
lint(
|
|
18
|
-
parentNode: AstElement;
|
|
19
|
-
}, start?: number): LintError[];
|
|
32
|
+
lint(start?: number): Parser.LintError[];
|
|
20
33
|
/**
|
|
21
34
|
* 替换字符串
|
|
22
|
-
* @
|
|
35
|
+
* @browser
|
|
36
|
+
* @param text 替换的字符串
|
|
23
37
|
*/
|
|
24
38
|
replaceData(text?: string): void;
|
|
25
39
|
/** 复制 */
|
|
26
40
|
cloneNode(): AstText;
|
|
27
41
|
/**
|
|
28
42
|
* 在后方添加字符串
|
|
29
|
-
* @param
|
|
43
|
+
* @param text 添加的字符串
|
|
30
44
|
*/
|
|
31
45
|
appendData(text: string): void;
|
|
32
46
|
/**
|
|
33
47
|
* 删减字符串
|
|
34
|
-
* @param
|
|
35
|
-
* @param
|
|
48
|
+
* @param offset 起始位置
|
|
49
|
+
* @param count 删减字符数
|
|
36
50
|
*/
|
|
37
51
|
deleteData(offset: number, count: number): void;
|
|
38
52
|
/**
|
|
39
53
|
* 插入字符串
|
|
40
|
-
* @param
|
|
41
|
-
* @param
|
|
54
|
+
* @param offset 插入位置
|
|
55
|
+
* @param text 待插入的字符串
|
|
42
56
|
*/
|
|
43
57
|
insertData(offset: number, text: string): void;
|
|
44
58
|
/**
|
|
45
59
|
* 提取子串
|
|
46
|
-
* @param
|
|
47
|
-
* @param
|
|
60
|
+
* @param offset 起始位置
|
|
61
|
+
* @param count 字符数
|
|
48
62
|
*/
|
|
49
63
|
substringData(offset: number, count: number): string;
|
|
50
64
|
/**
|
|
51
65
|
* 将文本子节点分裂为两部分
|
|
52
|
-
* @param
|
|
66
|
+
* @param offset 分裂位置
|
|
53
67
|
* @throws `RangeError` 错误的断开位置
|
|
54
68
|
* @throws `Error` 没有父节点
|
|
55
69
|
*/
|
|
56
70
|
splitText(offset: number): AstText;
|
|
57
|
-
#private;
|
|
58
|
-
}
|
|
59
|
-
declare namespace AstText {
|
|
60
|
-
export { LintError };
|
|
61
71
|
}
|
|
62
|
-
|
|
63
|
-
import AstElement = require("./element");
|
|
64
|
-
type LintError = import('../../typings/token').LintError;
|
|
72
|
+
export = AstText;
|
package/dist/lib/text.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Parser = require("../index");
|
|
3
|
+
const AstNode = require("./node");
|
|
4
|
+
const errorSyntax = /https?:\/\/|\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, errorSyntaxUrl = /\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, disallowedTags = [
|
|
5
|
+
'html',
|
|
6
|
+
'base',
|
|
7
|
+
'head',
|
|
8
|
+
'style',
|
|
9
|
+
'title',
|
|
10
|
+
'body',
|
|
11
|
+
'menu',
|
|
12
|
+
'a',
|
|
13
|
+
'area',
|
|
14
|
+
'audio',
|
|
15
|
+
'img',
|
|
16
|
+
'map',
|
|
17
|
+
'track',
|
|
18
|
+
'video',
|
|
19
|
+
'embed',
|
|
20
|
+
'iframe',
|
|
21
|
+
'object',
|
|
22
|
+
'picture',
|
|
23
|
+
'source',
|
|
24
|
+
'canvas',
|
|
25
|
+
'script',
|
|
26
|
+
'col',
|
|
27
|
+
'colgroup',
|
|
28
|
+
'tbody',
|
|
29
|
+
'tfoot',
|
|
30
|
+
'thead',
|
|
31
|
+
'button',
|
|
32
|
+
'datalist',
|
|
33
|
+
'fieldset',
|
|
34
|
+
'form',
|
|
35
|
+
'input',
|
|
36
|
+
'label',
|
|
37
|
+
'legend',
|
|
38
|
+
'meter',
|
|
39
|
+
'optgroup',
|
|
40
|
+
'option',
|
|
41
|
+
'output',
|
|
42
|
+
'progress',
|
|
43
|
+
'select',
|
|
44
|
+
'textarea',
|
|
45
|
+
'details',
|
|
46
|
+
'dialog',
|
|
47
|
+
'slot',
|
|
48
|
+
'template',
|
|
49
|
+
'dir',
|
|
50
|
+
'frame',
|
|
51
|
+
'frameset',
|
|
52
|
+
'marquee',
|
|
53
|
+
'param',
|
|
54
|
+
'xmp',
|
|
55
|
+
];
|
|
56
|
+
/** 文本节点 */
|
|
57
|
+
class AstText extends AstNode {
|
|
58
|
+
/** @browser */
|
|
59
|
+
type = 'text';
|
|
60
|
+
/** @browser */
|
|
61
|
+
data;
|
|
62
|
+
/** 文本长度 */
|
|
63
|
+
get length() {
|
|
64
|
+
return this.data.length;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* @browser
|
|
68
|
+
* @param text 包含文本
|
|
69
|
+
*/
|
|
70
|
+
constructor(text = '') {
|
|
71
|
+
super();
|
|
72
|
+
Object.defineProperties(this, {
|
|
73
|
+
data: { value: text, writable: false },
|
|
74
|
+
childNodes: { enumerable: false, configurable: false },
|
|
75
|
+
type: { enumerable: false, writable: false, configurable: false },
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 输出字符串
|
|
80
|
+
* @browser
|
|
81
|
+
*/
|
|
82
|
+
toString() {
|
|
83
|
+
return this.data;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 可见部分
|
|
87
|
+
* @browser
|
|
88
|
+
*/
|
|
89
|
+
text() {
|
|
90
|
+
return this.data;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Linter
|
|
94
|
+
* @browser
|
|
95
|
+
*/
|
|
96
|
+
lint(start = this.getAbsoluteIndex()) {
|
|
97
|
+
const { data, parentNode, nextSibling, previousSibling } = this, type = parentNode?.type, name = parentNode?.name, nextType = nextSibling?.type, previousType = previousSibling?.type, errorRegex = type === 'free-ext-link' || type === 'ext-link-url' || type === 'image-parameter' && name === 'link'
|
|
98
|
+
? errorSyntaxUrl
|
|
99
|
+
: errorSyntax, errors = [...data.matchAll(errorRegex)], { ext, html } = this.getRootNode().getAttribute('config');
|
|
100
|
+
if (errors.length > 0) {
|
|
101
|
+
const root = this.getRootNode(), { top, left } = root.posFromIndex(start), tags = new Set([ext, html, disallowedTags].flat(2));
|
|
102
|
+
return errors
|
|
103
|
+
.map(({ 0: error, 1: prefix, 2: tag, index }) => {
|
|
104
|
+
if (prefix) {
|
|
105
|
+
/* eslint-disable no-param-reassign */
|
|
106
|
+
index += prefix.length;
|
|
107
|
+
error = error.slice(prefix.length);
|
|
108
|
+
/* eslint-enable no-param-reassign */
|
|
109
|
+
}
|
|
110
|
+
const startIndex = start + index, lines = data.slice(0, index).split('\n'), startLine = lines.length + top - 1, line = lines.at(-1), startCol = lines.length > 1 ? line.length : left + line.length, { 0: char, length } = error, endIndex = startIndex + length, end = char === '}' || char === ']' ? endIndex + 1 : startIndex + 49, rootStr = String(root), nextChar = rootStr[endIndex], previousChar = rootStr[startIndex - 1], severity = length > 1 && (char !== '<' || /[\s/>]/u.test(nextChar ?? ''))
|
|
111
|
+
|| char === '{' && (nextChar === char || previousChar === '-')
|
|
112
|
+
|| char === '}' && (previousChar === char || nextChar === '-')
|
|
113
|
+
|| char === '[' && (nextChar === char || type === 'ext-link-text'
|
|
114
|
+
|| !data.slice(index + 1).trim() && nextType === 'free-ext-link')
|
|
115
|
+
|| char === ']' && (previousChar === char
|
|
116
|
+
|| !data.slice(0, index).trim() && previousType === 'free-ext-link')
|
|
117
|
+
? 'error'
|
|
118
|
+
: 'warning';
|
|
119
|
+
return (char !== 'h' || index > 0) && (char !== '<' || tags.has(tag.toLowerCase())) && {
|
|
120
|
+
message: Parser.msg('lonely "$1"', char === 'h' ? error : char),
|
|
121
|
+
severity,
|
|
122
|
+
startIndex,
|
|
123
|
+
endIndex,
|
|
124
|
+
startLine,
|
|
125
|
+
endLine: startLine,
|
|
126
|
+
startCol,
|
|
127
|
+
endCol: startCol + length,
|
|
128
|
+
excerpt: rootStr.slice(Math.max(0, end - 50), end),
|
|
129
|
+
};
|
|
130
|
+
}).filter(Boolean);
|
|
131
|
+
}
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 修改内容
|
|
136
|
+
* @browser
|
|
137
|
+
* @param text 新内容
|
|
138
|
+
*/
|
|
139
|
+
#setData(text) {
|
|
140
|
+
const { data } = this, e = new Event('text', { bubbles: true });
|
|
141
|
+
this.setAttribute('data', text);
|
|
142
|
+
if (data !== text) {
|
|
143
|
+
this.dispatchEvent(e, { oldText: data, newText: text });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 替换字符串
|
|
148
|
+
* @browser
|
|
149
|
+
* @param text 替换的字符串
|
|
150
|
+
*/
|
|
151
|
+
replaceData(text = '') {
|
|
152
|
+
this.#setData(text);
|
|
153
|
+
}
|
|
154
|
+
/** 复制 */
|
|
155
|
+
cloneNode() {
|
|
156
|
+
return new AstText(this.data);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* 在后方添加字符串
|
|
160
|
+
* @param text 添加的字符串
|
|
161
|
+
*/
|
|
162
|
+
appendData(text) {
|
|
163
|
+
this.#setData(this.data + text);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 删减字符串
|
|
167
|
+
* @param offset 起始位置
|
|
168
|
+
* @param count 删减字符数
|
|
169
|
+
*/
|
|
170
|
+
deleteData(offset, count) {
|
|
171
|
+
this.#setData(this.data.slice(0, offset) + (offset < 0 && offset + count >= 0 ? '' : this.data.slice(offset + count)));
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 插入字符串
|
|
175
|
+
* @param offset 插入位置
|
|
176
|
+
* @param text 待插入的字符串
|
|
177
|
+
*/
|
|
178
|
+
insertData(offset, text) {
|
|
179
|
+
this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 提取子串
|
|
183
|
+
* @param offset 起始位置
|
|
184
|
+
* @param count 字符数
|
|
185
|
+
*/
|
|
186
|
+
substringData(offset, count) {
|
|
187
|
+
return this.data.substr(offset, count);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 将文本子节点分裂为两部分
|
|
191
|
+
* @param offset 分裂位置
|
|
192
|
+
* @throws `RangeError` 错误的断开位置
|
|
193
|
+
* @throws `Error` 没有父节点
|
|
194
|
+
*/
|
|
195
|
+
splitText(offset) {
|
|
196
|
+
if (!Number.isInteger(offset)) {
|
|
197
|
+
this.typeError('splitText', 'Number');
|
|
198
|
+
}
|
|
199
|
+
else if (offset > this.length || offset < -this.length) {
|
|
200
|
+
throw new RangeError(`错误的断开位置:${offset}`);
|
|
201
|
+
}
|
|
202
|
+
const { parentNode, data } = this;
|
|
203
|
+
if (!parentNode) {
|
|
204
|
+
throw new Error('待分裂的文本节点没有父节点!');
|
|
205
|
+
}
|
|
206
|
+
const newText = new AstText(data.slice(offset)), childNodes = [...parentNode.childNodes];
|
|
207
|
+
this.setAttribute('data', data.slice(0, offset));
|
|
208
|
+
childNodes.splice(childNodes.indexOf(this) + 1, 0, newText);
|
|
209
|
+
newText.setAttribute('parentNode', parentNode);
|
|
210
|
+
parentNode.setAttribute('childNodes', childNodes);
|
|
211
|
+
return newText;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
Parser.classes['AstText'] = __filename;
|
|
215
|
+
module.exports = AstText;
|
package/dist/lib/title.d.ts
CHANGED
|
@@ -1,21 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
import * as Parser from '../index';
|
|
2
2
|
/** MediaWiki页面标题对象 */
|
|
3
3
|
declare class Title {
|
|
4
|
-
/**
|
|
5
|
-
* @param {string} title 标题(含或不含命名空间前缀)
|
|
6
|
-
* @param {number} defaultNs 命名空间
|
|
7
|
-
* @param {boolean} decode 是否需要解码
|
|
8
|
-
* @param {boolean} selfLink 是否允许selfLink
|
|
9
|
-
*/
|
|
10
|
-
constructor(title: string, defaultNs?: number, config?: import("../../typings/token").ParserConfig, decode?: boolean, selfLink?: boolean);
|
|
4
|
+
/** @browser */
|
|
11
5
|
valid: boolean;
|
|
6
|
+
/** @browser */
|
|
12
7
|
ns: number;
|
|
13
|
-
|
|
8
|
+
/** @browser */
|
|
9
|
+
fragment: string | undefined;
|
|
10
|
+
/** @browser */
|
|
14
11
|
encoded: boolean;
|
|
15
|
-
title: string;
|
|
16
12
|
main: string;
|
|
17
13
|
prefix: string;
|
|
18
14
|
interwiki: string;
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
conversionTable: Map<string, string>;
|
|
16
|
+
redirects: Map<string, string>;
|
|
17
|
+
/** 完整标题 */
|
|
18
|
+
get title(): string;
|
|
19
|
+
/**
|
|
20
|
+
* @browser
|
|
21
|
+
* @param str 标题(含或不含命名空间前缀)
|
|
22
|
+
* @param defaultNs 命名空间
|
|
23
|
+
* @param decode 是否需要解码
|
|
24
|
+
* @param selfLink 是否允许selfLink
|
|
25
|
+
*/
|
|
26
|
+
constructor(str: string, defaultNs?: number, config?: Parser.Config, decode?: boolean, selfLink?: boolean);
|
|
27
|
+
/** 完整链接 */
|
|
28
|
+
toString(): string;
|
|
29
|
+
/**
|
|
30
|
+
* 转换
|
|
31
|
+
* @param conversionTable 单向转换表
|
|
32
|
+
*/
|
|
33
|
+
autoConvert(): void;
|
|
21
34
|
}
|
|
35
|
+
export = Title;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const string_1 = require("../util/string");
|
|
3
|
+
const Parser = require("../index");
|
|
4
|
+
/** MediaWiki页面标题对象 */
|
|
5
|
+
class Title {
|
|
6
|
+
/** @browser */
|
|
7
|
+
valid = true;
|
|
8
|
+
/** @browser */
|
|
9
|
+
ns = 0;
|
|
10
|
+
/** @browser */
|
|
11
|
+
fragment;
|
|
12
|
+
/** @browser */
|
|
13
|
+
encoded = false;
|
|
14
|
+
main = '';
|
|
15
|
+
prefix = '';
|
|
16
|
+
interwiki = '';
|
|
17
|
+
conversionTable = new Map();
|
|
18
|
+
redirects = new Map();
|
|
19
|
+
/** 完整标题 */
|
|
20
|
+
get title() {
|
|
21
|
+
let title = `${this.interwiki && `${this.interwiki}:`}${this.prefix}${this.main.replaceAll(' ', '_')}`;
|
|
22
|
+
const redirected = this.redirects.get(title);
|
|
23
|
+
if (redirected) {
|
|
24
|
+
return redirected;
|
|
25
|
+
}
|
|
26
|
+
this.autoConvert();
|
|
27
|
+
title = `${this.interwiki && `${this.interwiki}:`}${this.prefix}${this.main.replaceAll(' ', '_')}`;
|
|
28
|
+
return this.redirects.get(title) ?? title;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @browser
|
|
32
|
+
* @param str 标题(含或不含命名空间前缀)
|
|
33
|
+
* @param defaultNs 命名空间
|
|
34
|
+
* @param decode 是否需要解码
|
|
35
|
+
* @param selfLink 是否允许selfLink
|
|
36
|
+
*/
|
|
37
|
+
constructor(str, defaultNs = 0, config = Parser.getConfig(), decode = false, selfLink = false) {
|
|
38
|
+
const { namespaces, nsid } = config;
|
|
39
|
+
let namespace = namespaces[defaultNs], title = (0, string_1.decodeHtml)(str);
|
|
40
|
+
if (decode && title.includes('%')) {
|
|
41
|
+
try {
|
|
42
|
+
const encoded = /%(?!21|3[ce]|5[bd]|7[b-d])[\da-f]{2}/iu.test(title);
|
|
43
|
+
title = decodeURIComponent(title);
|
|
44
|
+
this.encoded = encoded;
|
|
45
|
+
}
|
|
46
|
+
catch { }
|
|
47
|
+
}
|
|
48
|
+
title = title.replaceAll('_', ' ').trim();
|
|
49
|
+
if (title.startsWith(':')) {
|
|
50
|
+
namespace = '';
|
|
51
|
+
title = title.slice(1).trim();
|
|
52
|
+
}
|
|
53
|
+
const iw = defaultNs ? undefined : Parser.isInterwiki(title, config);
|
|
54
|
+
if (iw) {
|
|
55
|
+
this.interwiki = iw[1].toLowerCase();
|
|
56
|
+
title = title.slice(iw[0].length);
|
|
57
|
+
}
|
|
58
|
+
const m = title.split(':');
|
|
59
|
+
if (m.length > 1) {
|
|
60
|
+
const ns = nsid[m[0].trim().toLowerCase()], id = ns === undefined ? undefined : namespaces[ns];
|
|
61
|
+
if (id !== undefined) {
|
|
62
|
+
namespace = id;
|
|
63
|
+
title = m.slice(1).join(':').trim();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
this.ns = nsid[namespace.toLowerCase()];
|
|
67
|
+
const i = title.indexOf('#');
|
|
68
|
+
let fragment;
|
|
69
|
+
if (i !== -1) {
|
|
70
|
+
fragment = title.slice(i + 1).trimEnd();
|
|
71
|
+
if (fragment.includes('%')) {
|
|
72
|
+
try {
|
|
73
|
+
fragment = decodeURIComponent(fragment);
|
|
74
|
+
}
|
|
75
|
+
catch { }
|
|
76
|
+
}
|
|
77
|
+
else if (fragment.includes('.')) {
|
|
78
|
+
try {
|
|
79
|
+
fragment = decodeURIComponent(fragment.replaceAll('.', '%'));
|
|
80
|
+
}
|
|
81
|
+
catch { }
|
|
82
|
+
}
|
|
83
|
+
title = title.slice(0, i).trim();
|
|
84
|
+
}
|
|
85
|
+
this.valid = Boolean(title || selfLink && fragment !== undefined || this.interwiki)
|
|
86
|
+
&& !/\0\d+[eh!+-]\x7F|[<>[\]{}|]|%[\da-f]{2}/iu.test(title);
|
|
87
|
+
this.fragment = fragment;
|
|
88
|
+
this.main = title && `${title[0].toUpperCase()}${title.slice(1)}`;
|
|
89
|
+
this.prefix = `${namespace}${namespace && ':'}`;
|
|
90
|
+
}
|
|
91
|
+
/** 完整链接 */
|
|
92
|
+
toString() {
|
|
93
|
+
return `${this.title}${this.fragment === undefined ? '' : `#${this.fragment}`}`;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 转换
|
|
97
|
+
* @param conversionTable 单向转换表
|
|
98
|
+
*/
|
|
99
|
+
autoConvert() {
|
|
100
|
+
const { conversionTable } = this;
|
|
101
|
+
if (conversionTable.size > 0) {
|
|
102
|
+
const regex = new RegExp([...conversionTable.keys()].sort().reverse().map(string_1.escapeRegExp).join('|'), 'gu');
|
|
103
|
+
this.main = this.main.replace(regex, p => conversionTable.get(p));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
Parser.classes['Title'] = __filename;
|
|
108
|
+
module.exports = Title;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Parser = require("../index");
|
|
3
|
+
/**
|
|
4
|
+
* 子节点含有AttributesToken的类
|
|
5
|
+
* @param constructor 基类
|
|
6
|
+
* @param i AttributesToken子节点的位置
|
|
7
|
+
*/
|
|
8
|
+
const attributesParent = (constructor, i = 0) => {
|
|
9
|
+
/** 子节点含有AttributesToken的类 */
|
|
10
|
+
class AttributesParent extends constructor {
|
|
11
|
+
/** AttributesToken子节点 */
|
|
12
|
+
get #attributesChild() {
|
|
13
|
+
return this.childNodes.at(i);
|
|
14
|
+
}
|
|
15
|
+
/** getAttrs()方法的getter写法 */
|
|
16
|
+
get attributes() {
|
|
17
|
+
return this.#attributesChild.attributes;
|
|
18
|
+
}
|
|
19
|
+
/** 以字符串表示的class属性 */
|
|
20
|
+
get className() {
|
|
21
|
+
return this.#attributesChild.className;
|
|
22
|
+
}
|
|
23
|
+
set className(className) {
|
|
24
|
+
this.#attributesChild.className = className;
|
|
25
|
+
}
|
|
26
|
+
/** 以Set表示的class属性 */
|
|
27
|
+
get classList() {
|
|
28
|
+
return this.#attributesChild.classList;
|
|
29
|
+
}
|
|
30
|
+
/** id属性 */
|
|
31
|
+
get id() {
|
|
32
|
+
return this.#attributesChild.id;
|
|
33
|
+
}
|
|
34
|
+
set id(id) {
|
|
35
|
+
this.#attributesChild.id = id;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* AttributesToken子节点是否具有某属性
|
|
39
|
+
* @param key 属性键
|
|
40
|
+
*/
|
|
41
|
+
hasAttr(key) {
|
|
42
|
+
return this.#attributesChild.hasAttr(key);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 获取AttributesToken子节点的属性
|
|
46
|
+
* @param key 属性键
|
|
47
|
+
*/
|
|
48
|
+
getAttr(key) {
|
|
49
|
+
return this.#attributesChild.getAttr(key);
|
|
50
|
+
}
|
|
51
|
+
/** 列举AttributesToken子节点的属性键 */
|
|
52
|
+
getAttrNames() {
|
|
53
|
+
return this.#attributesChild.getAttrNames();
|
|
54
|
+
}
|
|
55
|
+
/** AttributesToken子节点是否具有任意属性 */
|
|
56
|
+
hasAttrs() {
|
|
57
|
+
return this.#attributesChild.hasAttrs();
|
|
58
|
+
}
|
|
59
|
+
/** 获取AttributesToken子节点的全部标签属性 */
|
|
60
|
+
getAttrs() {
|
|
61
|
+
return this.#attributesChild.getAttrs();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 对AttributesToken子节点设置属性
|
|
65
|
+
* @param key 属性键
|
|
66
|
+
* @param value 属性值
|
|
67
|
+
*/
|
|
68
|
+
setAttr(key, value) {
|
|
69
|
+
this.#attributesChild.setAttr(key, value);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 移除AttributesToken子节点的某属性
|
|
73
|
+
* @param key 属性键
|
|
74
|
+
*/
|
|
75
|
+
removeAttr(key) {
|
|
76
|
+
this.#attributesChild.removeAttr(key);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 开关AttributesToken子节点的某属性
|
|
80
|
+
* @param key 属性键
|
|
81
|
+
* @param force 强制开启或关闭
|
|
82
|
+
*/
|
|
83
|
+
toggleAttr(key, force) {
|
|
84
|
+
this.#attributesChild.toggleAttr(key, force);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return AttributesParent;
|
|
88
|
+
};
|
|
89
|
+
Parser.mixins['attributesParent'] = __filename;
|
|
90
|
+
module.exports = attributesParent;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Parser = require("../index");
|
|
3
|
+
/**
|
|
4
|
+
* 不可增删子节点的类
|
|
5
|
+
* @param constructor 基类
|
|
6
|
+
*/
|
|
7
|
+
const fixed = (constructor) => {
|
|
8
|
+
/** 不可增删子节点的类 */
|
|
9
|
+
class FixedToken extends constructor {
|
|
10
|
+
static fixed = true;
|
|
11
|
+
/**
|
|
12
|
+
* @override
|
|
13
|
+
* @throws `Error`
|
|
14
|
+
*/
|
|
15
|
+
removeAt() {
|
|
16
|
+
throw new Error(`${this.constructor.name} 不可删除元素!`);
|
|
17
|
+
}
|
|
18
|
+
/** @ignore */
|
|
19
|
+
insertAt(token, i = this.length) {
|
|
20
|
+
if (Parser.running) {
|
|
21
|
+
return super.insertAt(token, i);
|
|
22
|
+
}
|
|
23
|
+
throw new Error(`${this.constructor.name} 不可插入元素!`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return FixedToken;
|
|
27
|
+
};
|
|
28
|
+
Parser.mixins['fixed'] = __filename;
|
|
29
|
+
module.exports = fixed;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Parser = require("../index");
|
|
3
|
+
/**
|
|
4
|
+
* 解析后不可见的类
|
|
5
|
+
* @param constructor 基类
|
|
6
|
+
*/
|
|
7
|
+
const hidden = (constructor) => {
|
|
8
|
+
/** 解析后不可见的类 */
|
|
9
|
+
class AnyHiddenToken extends constructor {
|
|
10
|
+
static hidden = true;
|
|
11
|
+
/** 没有可见部分 */
|
|
12
|
+
text() {
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return AnyHiddenToken;
|
|
17
|
+
};
|
|
18
|
+
Parser.mixins['hidden'] = __filename;
|
|
19
|
+
module.exports = hidden;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Parser = require("../index");
|
|
3
|
+
/**
|
|
4
|
+
* 不可包含换行符的类
|
|
5
|
+
* @param constructor 基类
|
|
6
|
+
*/
|
|
7
|
+
const singleLine = (constructor) => {
|
|
8
|
+
/** 不可包含换行符的类 */
|
|
9
|
+
class SingleLineToken extends constructor {
|
|
10
|
+
/** @override */
|
|
11
|
+
toString(selector) {
|
|
12
|
+
return super.toString(selector).replaceAll('\n', ' ');
|
|
13
|
+
}
|
|
14
|
+
/** @override */
|
|
15
|
+
text() {
|
|
16
|
+
return super.text().replaceAll('\n', ' ');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(SingleLineToken, 'name', { value: `SingleLine${constructor.name}` });
|
|
20
|
+
return SingleLineToken;
|
|
21
|
+
};
|
|
22
|
+
Parser.mixins['singleLine'] = __filename;
|
|
23
|
+
module.exports = singleLine;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const Parser = require("../index");
|
|
3
|
+
/**
|
|
4
|
+
* 只能位于行首的类
|
|
5
|
+
* @param constructor 基类
|
|
6
|
+
*/
|
|
7
|
+
const sol = (constructor) => {
|
|
8
|
+
/** 只能位于行首的类 */
|
|
9
|
+
class SolToken extends constructor {
|
|
10
|
+
/** 是否可以视为root节点 */
|
|
11
|
+
#isRoot() {
|
|
12
|
+
const { parentNode, type } = this;
|
|
13
|
+
return parentNode?.type === 'root'
|
|
14
|
+
|| type !== 'heading' && parentNode?.type === 'ext-inner' && parentNode.name === 'poem';
|
|
15
|
+
}
|
|
16
|
+
/** 在前方插入newline */
|
|
17
|
+
prependNewLine() {
|
|
18
|
+
const { previousVisibleSibling } = this;
|
|
19
|
+
return (previousVisibleSibling ?? !this.#isRoot()) && !String(previousVisibleSibling).endsWith('\n')
|
|
20
|
+
? '\n'
|
|
21
|
+
: '';
|
|
22
|
+
}
|
|
23
|
+
/** @override */
|
|
24
|
+
toString(selector) {
|
|
25
|
+
return selector && this.matches(selector)
|
|
26
|
+
? ''
|
|
27
|
+
: `${this.prependNewLine()}${super.toString(selector)}`;
|
|
28
|
+
}
|
|
29
|
+
/** @override */
|
|
30
|
+
getPadding() {
|
|
31
|
+
return this.prependNewLine().length;
|
|
32
|
+
}
|
|
33
|
+
/** @override */
|
|
34
|
+
text() {
|
|
35
|
+
return `${this.prependNewLine()}${super.text()}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return SolToken;
|
|
39
|
+
};
|
|
40
|
+
Parser.mixins['sol'] = __filename;
|
|
41
|
+
module.exports = sol;
|