wikiparser-node 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -5
- package/errors/README +1 -0
- package/index.js +42 -9
- package/lib/element.js +70 -12
- package/lib/node.js +17 -9
- package/package.json +1 -1
- package/parser/externalLinks.js +1 -1
- package/parser/table.js +2 -2
- package/printed/README +1 -0
- package/src/arg.js +3 -3
- package/src/attribute.js +6 -6
- package/src/extLink.js +3 -2
- package/src/heading.js +2 -3
- package/src/html.js +4 -3
- package/src/imageParameter.js +5 -6
- package/src/index.js +5 -5
- package/src/link/file.js +7 -7
- package/src/link/index.js +2 -4
- package/src/magicLink.js +2 -3
- package/src/nowiki/comment.js +1 -1
- package/src/parameter.js +3 -3
- package/src/table/index.js +26 -21
- package/src/table/td.js +2 -2
- package/src/table/tr.js +2 -6
- package/src/tagPair/index.js +1 -1
- package/src/transclude.js +14 -11
- package/tool/index.js +50 -40
- package/typings/index.d.ts +1 -0
- package/util/debug.js +3 -3
- package/util/string.js +4 -1
package/tool/index.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {typeError, externalUse} = require('../util/debug'),
|
|
4
|
-
{text} = require('../util/string'),
|
|
4
|
+
{text, noWrap} = require('../util/string'),
|
|
5
5
|
Token = require('../src'),
|
|
6
6
|
assert = require('assert/strict');
|
|
7
7
|
|
|
8
8
|
const /** @type {WeakMap<Token, Record<string, any>>} */ dataStore = new WeakMap();
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
+
* @param {string} method
|
|
11
12
|
* @param {string|Token|Token[]} selector
|
|
12
13
|
* @returns {(token: Token) => boolean}
|
|
13
14
|
*/
|
|
14
|
-
const matchesGenerator = selector => {
|
|
15
|
+
const matchesGenerator = (method, selector) => {
|
|
15
16
|
if (typeof selector === 'string') {
|
|
16
17
|
return token => token instanceof Token && token.matches(selector);
|
|
17
18
|
} else if (Array.isArray(selector)) {
|
|
@@ -19,7 +20,7 @@ const matchesGenerator = selector => {
|
|
|
19
20
|
} else if (selector instanceof Token) {
|
|
20
21
|
return token => token === selector;
|
|
21
22
|
}
|
|
22
|
-
typeError('String', 'Token', 'Array');
|
|
23
|
+
typeError(TokenCollection, method, 'String', 'Token', 'Array');
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
/** @extends {Array<Token>} */
|
|
@@ -39,7 +40,7 @@ class TokenCollection extends Array {
|
|
|
39
40
|
} else if (typeof token === 'string') {
|
|
40
41
|
this.push(token);
|
|
41
42
|
} else if (!(token instanceof Token)) {
|
|
42
|
-
typeError('String', 'Token');
|
|
43
|
+
this.typeError('constructor', 'String', 'Token');
|
|
43
44
|
} else if (!this.includes(token)) {
|
|
44
45
|
this.#roots.add(token.getRootNode());
|
|
45
46
|
this.push(token);
|
|
@@ -49,6 +50,14 @@ class TokenCollection extends Array {
|
|
|
49
50
|
this.#sort();
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
/**
|
|
54
|
+
* @param {string} method
|
|
55
|
+
* @param {...string} types
|
|
56
|
+
*/
|
|
57
|
+
typeError(method, ...types) {
|
|
58
|
+
return typeError(this.constructor, method, ...types);
|
|
59
|
+
}
|
|
60
|
+
|
|
52
61
|
#sort() {
|
|
53
62
|
if (this.some(token => typeof token === 'string')) {
|
|
54
63
|
return;
|
|
@@ -171,7 +180,7 @@ class TokenCollection extends Array {
|
|
|
171
180
|
if (typeof selector === 'function') {
|
|
172
181
|
return this.some((ele, i) => selector.call(ele, i, ele));
|
|
173
182
|
}
|
|
174
|
-
const matches = matchesGenerator(selector);
|
|
183
|
+
const matches = matchesGenerator('is', selector);
|
|
175
184
|
return this.some(matches);
|
|
176
185
|
}
|
|
177
186
|
|
|
@@ -182,7 +191,7 @@ class TokenCollection extends Array {
|
|
|
182
191
|
if (typeof selector === 'function') {
|
|
183
192
|
$arr = $(arr.filter((ele, i) => selector.call(ele, i, ele)));
|
|
184
193
|
} else {
|
|
185
|
-
const matches = matchesGenerator(selector);
|
|
194
|
+
const matches = matchesGenerator('filter', selector);
|
|
186
195
|
$arr = $(arr.filter(matches));
|
|
187
196
|
}
|
|
188
197
|
$arr.prevObject = this;
|
|
@@ -198,7 +207,7 @@ class TokenCollection extends Array {
|
|
|
198
207
|
} else if (typeof selector === 'string') {
|
|
199
208
|
$arr = $(arr.filter(ele => ele instanceof Token && !ele.matches(selector)));
|
|
200
209
|
} else {
|
|
201
|
-
const matches = matchesGenerator(selector);
|
|
210
|
+
const matches = matchesGenerator('not', selector);
|
|
202
211
|
$arr = $(arr.filter(ele => !matches(ele)));
|
|
203
212
|
}
|
|
204
213
|
$arr.prevObject = this;
|
|
@@ -215,7 +224,7 @@ class TokenCollection extends Array {
|
|
|
215
224
|
} else if (selector instanceof Token) {
|
|
216
225
|
map = arr => arr.some(token => token.contains(selector)) ? [selector] : [];
|
|
217
226
|
} else {
|
|
218
|
-
typeError('String', 'Token', 'Array');
|
|
227
|
+
this.typeError('find', 'String', 'Token', 'Array');
|
|
219
228
|
}
|
|
220
229
|
return this._create(map);
|
|
221
230
|
}
|
|
@@ -228,13 +237,13 @@ class TokenCollection extends Array {
|
|
|
228
237
|
} else if (selector instanceof Token) {
|
|
229
238
|
return arr.some(ele => ele.contains(selector));
|
|
230
239
|
}
|
|
231
|
-
typeError('String', 'Token');
|
|
240
|
+
this.typeError('has', 'String', 'Token');
|
|
232
241
|
}
|
|
233
242
|
|
|
234
243
|
/** @param {string} selector */
|
|
235
244
|
closest(selector) {
|
|
236
245
|
if (typeof selector !== 'string') {
|
|
237
|
-
typeError('String');
|
|
246
|
+
this.typeError('closest', 'String');
|
|
238
247
|
}
|
|
239
248
|
return this._create(arr => arr.map(ele => ele.closest(selector)));
|
|
240
249
|
}
|
|
@@ -314,7 +323,7 @@ class TokenCollection extends Array {
|
|
|
314
323
|
* @param {string|Token|Token[]} selector
|
|
315
324
|
*/
|
|
316
325
|
_until(method, selector, filter = '') {
|
|
317
|
-
const matches = matchesGenerator(selector);
|
|
326
|
+
const matches = matchesGenerator(`${method.replace(/All$/, '')}Until`, selector);
|
|
318
327
|
return this._create(arr => arr.flatMap(ele => {
|
|
319
328
|
const tokens = $(ele)[method]().toArray(),
|
|
320
329
|
tokenArray = method === 'nextAll' ? tokens : tokens.reverse(),
|
|
@@ -349,7 +358,7 @@ class TokenCollection extends Array {
|
|
|
349
358
|
/** @param {[string|Record<string, any>]} key */
|
|
350
359
|
data(key, value) {
|
|
351
360
|
if (value !== undefined && typeof key !== 'string') {
|
|
352
|
-
typeError('String');
|
|
361
|
+
this.typeError('data', 'String');
|
|
353
362
|
} else if (value === undefined && typeof key !== 'object') {
|
|
354
363
|
const data = $.dataStore.get(this._find());
|
|
355
364
|
return key === undefined ? data : data?.[key];
|
|
@@ -371,7 +380,7 @@ class TokenCollection extends Array {
|
|
|
371
380
|
/** @param {string|string[]} name */
|
|
372
381
|
removeData(name) {
|
|
373
382
|
if (name !== undefined && typeof name !== 'string' && !Array.isArray(name)) {
|
|
374
|
-
typeError('String', 'Array');
|
|
383
|
+
this.typeError('removeData', 'String', 'Array');
|
|
375
384
|
}
|
|
376
385
|
name = typeof name === 'string' ? name.split(/\s/) : name;
|
|
377
386
|
for (const token of this._filter()) {
|
|
@@ -396,7 +405,7 @@ class TokenCollection extends Array {
|
|
|
396
405
|
*/
|
|
397
406
|
_addEventListener(events, selector, handler, once = false) {
|
|
398
407
|
if (!['string', 'object'].includes(typeof events)) {
|
|
399
|
-
typeError('String', 'Object');
|
|
408
|
+
this.typeError(once ? 'once' : 'on', 'String', 'Object');
|
|
400
409
|
} else if (typeof selector === 'function') {
|
|
401
410
|
handler = selector;
|
|
402
411
|
selector = undefined;
|
|
@@ -437,7 +446,7 @@ class TokenCollection extends Array {
|
|
|
437
446
|
*/
|
|
438
447
|
off(events, selector, handler) {
|
|
439
448
|
if (!['string', 'object', 'undefined'].includes(typeof events)) {
|
|
440
|
-
typeError('String', 'Object');
|
|
449
|
+
this.typeError('off', 'String', 'Object');
|
|
441
450
|
}
|
|
442
451
|
handler = typeof selector === 'function' ? selector : handler;
|
|
443
452
|
let eventPair;
|
|
@@ -452,7 +461,7 @@ class TokenCollection extends Array {
|
|
|
452
461
|
} else {
|
|
453
462
|
for (const [event, listener] of eventPair) {
|
|
454
463
|
if (typeof event !== 'string' || !['function', 'undefined'].includes(typeof listener)) {
|
|
455
|
-
typeError('String', 'Function');
|
|
464
|
+
this.typeError('off', 'String', 'Function');
|
|
456
465
|
} else if (listener) {
|
|
457
466
|
token.removeEventListener(event, listener);
|
|
458
467
|
} else {
|
|
@@ -503,7 +512,7 @@ class TokenCollection extends Array {
|
|
|
503
512
|
} else if (Array.isArray(result)) {
|
|
504
513
|
token[method](...result);
|
|
505
514
|
} else {
|
|
506
|
-
typeError('String', 'Token');
|
|
515
|
+
this.typeError(method, 'String', 'Token');
|
|
507
516
|
}
|
|
508
517
|
}
|
|
509
518
|
}
|
|
@@ -604,7 +613,7 @@ class TokenCollection extends Array {
|
|
|
604
613
|
}
|
|
605
614
|
}
|
|
606
615
|
} else {
|
|
607
|
-
typeError('Token', 'Array');
|
|
616
|
+
this.typeError(method, 'Token', 'Array');
|
|
608
617
|
}
|
|
609
618
|
return this;
|
|
610
619
|
}
|
|
@@ -648,7 +657,7 @@ class TokenCollection extends Array {
|
|
|
648
657
|
} else if (Array.isArray(value)) {
|
|
649
658
|
toValue = i => value[i];
|
|
650
659
|
} else {
|
|
651
|
-
typeError('String', 'Array', 'Function');
|
|
660
|
+
this.typeError('val', 'String', 'Array', 'Function');
|
|
652
661
|
}
|
|
653
662
|
for (const [i, token] of this.entries()) {
|
|
654
663
|
if (token instanceof Token && typeof token.setValue === 'function' && token.setValue.length === 1) {
|
|
@@ -730,7 +739,7 @@ class TokenCollection extends Array {
|
|
|
730
739
|
*/
|
|
731
740
|
wrapAll(wrapper) {
|
|
732
741
|
if (typeof wrapper !== 'function' && !Array.isArray(wrapper)) {
|
|
733
|
-
typeError('Array', 'Function');
|
|
742
|
+
this.typeError('wrapAll', 'Array', 'Function');
|
|
734
743
|
}
|
|
735
744
|
const firstToken = this._find(),
|
|
736
745
|
error = new Error('wrapAll 的主体应为同一个父节点下的连续子节点!');
|
|
@@ -757,7 +766,7 @@ class TokenCollection extends Array {
|
|
|
757
766
|
config = firstToken.getRootNode().getAttribute('config'),
|
|
758
767
|
token = new Token(`${pre}${this.toString()}${post}`, config).parse();
|
|
759
768
|
if (token.childNodes.length !== 1) {
|
|
760
|
-
throw new RangeError(`非法的 wrapper:\n${pre}\n${post}`);
|
|
769
|
+
throw new RangeError(`非法的 wrapper:\n${noWrap(pre)}\n${noWrap(post)}`);
|
|
761
770
|
}
|
|
762
771
|
for (let j = i + this.length - 1; j >= i; j--) {
|
|
763
772
|
parentNode.removeAt(j);
|
|
@@ -772,7 +781,7 @@ class TokenCollection extends Array {
|
|
|
772
781
|
*/
|
|
773
782
|
_wrap(method, wrapper) {
|
|
774
783
|
if (typeof wrapper !== 'function' && !Array.isArray(wrapper)) {
|
|
775
|
-
typeError('Array', 'Function');
|
|
784
|
+
this.typeError(method, 'Array', 'Function');
|
|
776
785
|
}
|
|
777
786
|
return this[method](
|
|
778
787
|
/**
|
|
@@ -788,7 +797,7 @@ class TokenCollection extends Array {
|
|
|
788
797
|
config = this.getRootNode().getAttribute('config'),
|
|
789
798
|
token = new Token(`${pre}${string}${post}`, config).parse();
|
|
790
799
|
if (token.childNodes.length !== 1) {
|
|
791
|
-
throw new RangeError(`非法的 wrapper:\n${pre}\n${post}`);
|
|
800
|
+
throw new RangeError(`非法的 wrapper:\n${noWrap(pre)}\n${noWrap(post)}`);
|
|
792
801
|
}
|
|
793
802
|
return token.firstChild;
|
|
794
803
|
},
|
|
@@ -852,36 +861,37 @@ const $ = tokens => {
|
|
|
852
861
|
},
|
|
853
862
|
});
|
|
854
863
|
};
|
|
855
|
-
|
|
864
|
+
/* eslint-disable func-names */
|
|
865
|
+
$.hasData = /** @param {Token} element */ function hasData(element) {
|
|
856
866
|
if (!(element instanceof Token)) {
|
|
857
|
-
typeError('Token');
|
|
867
|
+
typeError(this, 'hasData', 'Token');
|
|
858
868
|
}
|
|
859
|
-
return
|
|
869
|
+
return this.dataStore.has(element);
|
|
860
870
|
};
|
|
861
|
-
$.data = /** @type {(
|
|
871
|
+
$.data = /** @type {function(Token, string, any): any} */ function data(element, key, value) {
|
|
862
872
|
if (!(element instanceof Token)) {
|
|
863
|
-
typeError('Token');
|
|
873
|
+
typeError(this, 'data', 'Token');
|
|
864
874
|
} else if (key === undefined) {
|
|
865
|
-
return
|
|
875
|
+
return this.dataStore.get(element);
|
|
866
876
|
} else if (typeof key !== 'string') {
|
|
867
|
-
typeError('String');
|
|
877
|
+
typeError(this, 'data', 'String');
|
|
868
878
|
} else if (value === undefined) {
|
|
869
|
-
return
|
|
870
|
-
} else if (
|
|
871
|
-
|
|
879
|
+
return this.dataStore.get(element)?.[key];
|
|
880
|
+
} else if (!this.dataStore.has(element)) {
|
|
881
|
+
this.dataStore.set(element, {});
|
|
872
882
|
}
|
|
873
|
-
|
|
883
|
+
this.dataStore.get(element)[key] = value;
|
|
874
884
|
return value;
|
|
875
885
|
};
|
|
876
|
-
$.removeData = /** @type {(
|
|
886
|
+
$.removeData = /** @type {function(Token, string): void} */ function removeData(element, name) {
|
|
877
887
|
if (!(element instanceof Token)) {
|
|
878
|
-
typeError('Token');
|
|
888
|
+
typeError(this, 'removeData', 'Token');
|
|
879
889
|
} else if (name === undefined) {
|
|
880
|
-
|
|
890
|
+
this.dataStore.delete(element);
|
|
881
891
|
} else if (typeof name !== 'string') {
|
|
882
|
-
typeError('String');
|
|
883
|
-
} else if (
|
|
884
|
-
const data =
|
|
892
|
+
typeError(this, 'removeData', 'String');
|
|
893
|
+
} else if (this.dataStore.has(element)) {
|
|
894
|
+
const data = this.dataStore.get(element);
|
|
885
895
|
delete data[name];
|
|
886
896
|
}
|
|
887
897
|
};
|
package/typings/index.d.ts
CHANGED
package/util/debug.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @param {
|
|
4
|
+
* @param {function}
|
|
5
5
|
* @param {string} method
|
|
6
6
|
* @param {...string} args
|
|
7
7
|
*/
|
|
8
|
-
const typeError = ({
|
|
9
|
-
throw new TypeError(`${
|
|
8
|
+
const typeError = ({name}, method, ...args) => {
|
|
9
|
+
throw new TypeError(`${name}.${method} 方法仅接受 ${args.join('、')} 作为输入参数!`);
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
/**
|
package/util/string.js
CHANGED
|
@@ -54,7 +54,10 @@ const explode = (start, end, separator, str) => {
|
|
|
54
54
|
return exploded;
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
+
/** @param {string} str */
|
|
58
|
+
const noWrap = str => str.replaceAll('\n', '\\n');
|
|
59
|
+
|
|
57
60
|
const extUrlChar = '(?:[\\d.]+|\\[[\\da-f:.]+\\]|[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd])'
|
|
58
61
|
+ '[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]*';
|
|
59
62
|
|
|
60
|
-
module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, extUrlChar};
|
|
63
|
+
module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, noWrap, extUrlChar};
|