wikiparser-node 0.9.0 → 0.9.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.
Files changed (87) hide show
  1. package/bundle/bundle.min.js +38 -0
  2. package/extensions/editor.css +62 -0
  3. package/extensions/editor.js +328 -0
  4. package/extensions/ui.css +119 -0
  5. package/package.json +13 -12
  6. package/README.md +0 -39
  7. package/config/default.json +0 -832
  8. package/config/llwiki.json +0 -630
  9. package/config/moegirl.json +0 -729
  10. package/config/zhwiki.json +0 -1269
  11. package/index.js +0 -333
  12. package/lib/element.js +0 -611
  13. package/lib/node.js +0 -770
  14. package/lib/ranges.js +0 -130
  15. package/lib/text.js +0 -245
  16. package/lib/title.js +0 -83
  17. package/mixin/attributeParent.js +0 -117
  18. package/mixin/fixedToken.js +0 -40
  19. package/mixin/hidden.js +0 -21
  20. package/mixin/singleLine.js +0 -31
  21. package/mixin/sol.js +0 -54
  22. package/parser/brackets.js +0 -126
  23. package/parser/commentAndExt.js +0 -59
  24. package/parser/converter.js +0 -46
  25. package/parser/externalLinks.js +0 -33
  26. package/parser/hrAndDoubleUnderscore.js +0 -38
  27. package/parser/html.js +0 -42
  28. package/parser/links.js +0 -94
  29. package/parser/list.js +0 -59
  30. package/parser/magicLinks.js +0 -41
  31. package/parser/quotes.js +0 -64
  32. package/parser/selector.js +0 -177
  33. package/parser/table.js +0 -114
  34. package/src/arg.js +0 -207
  35. package/src/atom/hidden.js +0 -13
  36. package/src/atom/index.js +0 -43
  37. package/src/attribute.js +0 -470
  38. package/src/attributes.js +0 -453
  39. package/src/charinsert.js +0 -97
  40. package/src/converter.js +0 -176
  41. package/src/converterFlags.js +0 -284
  42. package/src/converterRule.js +0 -256
  43. package/src/extLink.js +0 -180
  44. package/src/gallery.js +0 -149
  45. package/src/hasNowiki/index.js +0 -44
  46. package/src/hasNowiki/pre.js +0 -40
  47. package/src/heading.js +0 -134
  48. package/src/html.js +0 -254
  49. package/src/imageParameter.js +0 -301
  50. package/src/imagemap.js +0 -199
  51. package/src/imagemapLink.js +0 -41
  52. package/src/index.js +0 -936
  53. package/src/link/category.js +0 -44
  54. package/src/link/file.js +0 -287
  55. package/src/link/galleryImage.js +0 -120
  56. package/src/link/index.js +0 -384
  57. package/src/magicLink.js +0 -149
  58. package/src/nested/choose.js +0 -24
  59. package/src/nested/combobox.js +0 -23
  60. package/src/nested/index.js +0 -93
  61. package/src/nested/references.js +0 -23
  62. package/src/nowiki/comment.js +0 -71
  63. package/src/nowiki/dd.js +0 -59
  64. package/src/nowiki/doubleUnderscore.js +0 -56
  65. package/src/nowiki/hr.js +0 -41
  66. package/src/nowiki/index.js +0 -56
  67. package/src/nowiki/list.js +0 -16
  68. package/src/nowiki/noinclude.js +0 -28
  69. package/src/nowiki/quote.js +0 -69
  70. package/src/onlyinclude.js +0 -64
  71. package/src/paramTag/index.js +0 -89
  72. package/src/paramTag/inputbox.js +0 -35
  73. package/src/parameter.js +0 -239
  74. package/src/syntax.js +0 -91
  75. package/src/table/index.js +0 -983
  76. package/src/table/td.js +0 -338
  77. package/src/table/tr.js +0 -319
  78. package/src/tagPair/ext.js +0 -148
  79. package/src/tagPair/include.js +0 -50
  80. package/src/tagPair/index.js +0 -126
  81. package/src/transclude.js +0 -843
  82. package/tool/index.js +0 -1202
  83. package/util/base.js +0 -17
  84. package/util/debug.js +0 -73
  85. package/util/diff.js +0 -76
  86. package/util/lint.js +0 -55
  87. package/util/string.js +0 -126
package/src/extLink.js DELETED
@@ -1,180 +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
- 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 DELETED
@@ -1,149 +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
- 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;
@@ -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;
@@ -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'} & 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;