wikiparser-node 1.0.0-beta.0 → 1.0.0-beta.2

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 (133) hide show
  1. package/config/.schema.json +26 -0
  2. package/dist/index.d.ts +11 -9
  3. package/dist/index.js +22 -24
  4. package/dist/internal.d.ts +44 -0
  5. package/dist/lib/element.d.ts +42 -32
  6. package/dist/lib/element.js +59 -56
  7. package/dist/lib/node.d.ts +29 -36
  8. package/dist/lib/node.js +33 -48
  9. package/dist/lib/range.d.ts +132 -0
  10. package/dist/lib/range.js +387 -0
  11. package/dist/lib/ranges.d.ts +2 -12
  12. package/dist/lib/ranges.js +6 -11
  13. package/dist/lib/text.d.ts +11 -5
  14. package/dist/lib/text.js +25 -13
  15. package/dist/lib/title.d.ts +4 -5
  16. package/dist/lib/title.js +17 -7
  17. package/dist/mixin/attributesParent.js +6 -3
  18. package/dist/mixin/fixed.js +8 -5
  19. package/dist/mixin/hidden.js +7 -3
  20. package/dist/mixin/singleLine.js +6 -3
  21. package/dist/mixin/sol.js +6 -3
  22. package/dist/parser/{brackets.js → braces.js} +34 -26
  23. package/dist/parser/commentAndExt.js +25 -15
  24. package/dist/parser/converter.js +16 -9
  25. package/dist/parser/externalLinks.js +15 -9
  26. package/dist/parser/hrAndDoubleUnderscore.js +17 -10
  27. package/dist/parser/html.js +18 -9
  28. package/dist/parser/links.js +22 -14
  29. package/dist/parser/list.js +20 -10
  30. package/dist/parser/magicLinks.js +14 -7
  31. package/dist/parser/quotes.js +21 -11
  32. package/dist/parser/selector.js +19 -12
  33. package/dist/parser/table.js +25 -16
  34. package/dist/src/arg.d.ts +7 -8
  35. package/dist/src/arg.js +27 -24
  36. package/dist/src/atom.d.ts +4 -5
  37. package/dist/src/atom.js +9 -7
  38. package/dist/src/attribute.d.ts +13 -13
  39. package/dist/src/attribute.js +28 -25
  40. package/dist/src/attributes.d.ts +11 -10
  41. package/dist/src/attributes.js +40 -45
  42. package/dist/src/converter.d.ts +5 -7
  43. package/dist/src/converter.js +22 -16
  44. package/dist/src/converterFlags.d.ts +11 -12
  45. package/dist/src/converterFlags.js +17 -14
  46. package/dist/src/converterRule.d.ts +9 -10
  47. package/dist/src/converterRule.js +27 -27
  48. package/dist/src/extLink.d.ts +4 -6
  49. package/dist/src/extLink.js +25 -22
  50. package/dist/src/gallery.d.ts +9 -13
  51. package/dist/src/gallery.js +28 -23
  52. package/dist/src/heading.d.ts +11 -11
  53. package/dist/src/heading.js +19 -19
  54. package/dist/src/hidden.d.ts +4 -5
  55. package/dist/src/hidden.js +10 -8
  56. package/dist/src/html.d.ts +15 -12
  57. package/dist/src/html.js +18 -15
  58. package/dist/src/imageParameter.d.ts +11 -13
  59. package/dist/src/imageParameter.js +21 -16
  60. package/dist/src/imagemap.d.ts +11 -15
  61. package/dist/src/imagemap.js +26 -21
  62. package/dist/src/imagemapLink.d.ts +12 -17
  63. package/dist/src/imagemapLink.js +19 -14
  64. package/dist/src/index.d.ts +24 -20
  65. package/dist/src/index.js +65 -75
  66. package/dist/src/link/base.d.ts +8 -10
  67. package/dist/src/link/base.js +28 -22
  68. package/dist/src/link/category.d.ts +2 -3
  69. package/dist/src/link/category.js +7 -5
  70. package/dist/src/link/file.d.ts +9 -11
  71. package/dist/src/link/file.js +53 -26
  72. package/dist/src/link/galleryImage.d.ts +8 -8
  73. package/dist/src/link/galleryImage.js +18 -16
  74. package/dist/src/link/index.d.ts +6 -6
  75. package/dist/src/link/index.js +14 -16
  76. package/dist/src/magicLink.d.ts +9 -6
  77. package/dist/src/magicLink.js +23 -18
  78. package/dist/src/nested.d.ts +10 -10
  79. package/dist/src/nested.js +25 -17
  80. package/dist/src/nowiki/base.d.ts +7 -8
  81. package/dist/src/nowiki/base.js +10 -8
  82. package/dist/src/nowiki/comment.d.ts +7 -7
  83. package/dist/src/nowiki/comment.js +12 -9
  84. package/dist/src/nowiki/dd.d.ts +2 -3
  85. package/dist/src/nowiki/dd.js +7 -11
  86. package/dist/src/nowiki/doubleUnderscore.d.ts +5 -7
  87. package/dist/src/nowiki/doubleUnderscore.js +11 -8
  88. package/dist/src/nowiki/hr.d.ts +6 -7
  89. package/dist/src/nowiki/hr.js +11 -8
  90. package/dist/src/nowiki/index.d.ts +5 -7
  91. package/dist/src/nowiki/index.js +8 -6
  92. package/dist/src/nowiki/list.d.ts +4 -4
  93. package/dist/src/nowiki/list.js +8 -6
  94. package/dist/src/nowiki/noinclude.d.ts +3 -3
  95. package/dist/src/nowiki/noinclude.js +8 -6
  96. package/dist/src/nowiki/quote.d.ts +6 -7
  97. package/dist/src/nowiki/quote.js +11 -8
  98. package/dist/src/onlyinclude.d.ts +3 -5
  99. package/dist/src/onlyinclude.js +9 -7
  100. package/dist/src/paramTag/index.d.ts +8 -10
  101. package/dist/src/paramTag/index.js +15 -13
  102. package/dist/src/paramTag/inputbox.d.ts +4 -6
  103. package/dist/src/paramTag/inputbox.js +10 -8
  104. package/dist/src/parameter.d.ts +11 -12
  105. package/dist/src/parameter.js +25 -22
  106. package/dist/src/pre.d.ts +5 -10
  107. package/dist/src/pre.js +16 -11
  108. package/dist/src/syntax.d.ts +6 -7
  109. package/dist/src/syntax.js +12 -10
  110. package/dist/src/table/base.d.ts +14 -9
  111. package/dist/src/table/base.js +18 -15
  112. package/dist/src/table/index.d.ts +24 -17
  113. package/dist/src/table/index.js +39 -67
  114. package/dist/src/table/td.d.ts +17 -21
  115. package/dist/src/table/td.js +20 -28
  116. package/dist/src/table/tr.d.ts +6 -11
  117. package/dist/src/table/tr.js +8 -6
  118. package/dist/src/table/trBase.d.ts +12 -22
  119. package/dist/src/table/trBase.js +23 -19
  120. package/dist/src/tagPair/ext.d.ts +10 -10
  121. package/dist/src/tagPair/ext.js +47 -24
  122. package/dist/src/tagPair/include.d.ts +6 -7
  123. package/dist/src/tagPair/include.js +11 -8
  124. package/dist/src/tagPair/index.d.ts +12 -11
  125. package/dist/src/tagPair/index.js +13 -11
  126. package/dist/src/transclude.d.ts +8 -10
  127. package/dist/src/transclude.js +70 -77
  128. package/dist/util/debug.js +2 -12
  129. package/dist/util/diff.js +4 -2
  130. package/dist/util/lint.js +5 -5
  131. package/dist/util/string.js +2 -31
  132. package/package.json +13 -13
  133. package/dist/util/base.js +0 -26
package/dist/lib/node.js CHANGED
@@ -1,8 +1,19 @@
1
1
  "use strict";
2
- const debug_1 = require("../util/debug");
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AstNode = void 0;
3
4
  const assert = require("assert/strict");
4
5
  const EventEmitter = require("events");
5
- const Parser = require("../index");
6
+ const index_1 = require("../index");
7
+ /**
8
+ * 定制TypeError消息
9
+ * @param {Function} Constructor 类
10
+ * @param method
11
+ * @param args 可接受的参数类型
12
+ * @throws `TypeError`
13
+ */
14
+ const typeError = ({ name }, method, ...args) => {
15
+ throw new TypeError(`${name}.${method} 方法仅接受 ${args.join('、')} 作为输入参数!`);
16
+ };
6
17
  /** 类似Node */
7
18
  class AstNode {
8
19
  /** @browser */
@@ -89,7 +100,7 @@ class AstNode {
89
100
  return true;
90
101
  }
91
102
  let { nextSibling } = this;
92
- while (nextSibling?.type === 'text' && String(nextSibling).trim() === '') {
103
+ while (nextSibling?.type === 'text' && nextSibling.data.trim() === '') {
93
104
  ({ nextSibling } = nextSibling);
94
105
  }
95
106
  return nextSibling === undefined && this.parentNode?.eof;
@@ -116,7 +127,7 @@ class AstNode {
116
127
  }
117
128
  /** @private */
118
129
  hasAttribute(key) {
119
- return typeof key === 'string' ? key in this : this.typeError('hasAttribute', 'String');
130
+ return key in this;
120
131
  }
121
132
  /** @private */
122
133
  getAttribute(key) {
@@ -124,6 +135,7 @@ class AstNode {
124
135
  return new Set(this.#optional);
125
136
  }
126
137
  return this.hasAttribute(key)
138
+ // @ts-expect-error noImplicitAny
127
139
  ? String(this[key])
128
140
  : undefined;
129
141
  }
@@ -137,6 +149,7 @@ class AstNode {
137
149
  if (this.#optional.has(key)) {
138
150
  descriptor.enumerable = Boolean(value);
139
151
  }
152
+ // @ts-expect-error noImplicitAny
140
153
  const oldValue = this[key], frozen = oldValue !== null && typeof oldValue === 'object' && Object.isFrozen(oldValue);
141
154
  Object.defineProperty(this, key, { ...descriptor, value });
142
155
  if (frozen && typeof value === 'object') {
@@ -144,6 +157,7 @@ class AstNode {
144
157
  }
145
158
  }
146
159
  else {
160
+ // @ts-expect-error noImplicitAny
147
161
  this[key] = value;
148
162
  }
149
163
  return this;
@@ -165,9 +179,6 @@ class AstNode {
165
179
  * @param index 字符位置
166
180
  */
167
181
  posFromIndex(index) {
168
- if (!Number.isInteger(index)) {
169
- this.typeError('posFromIndex', 'Number');
170
- }
171
182
  const str = String(this);
172
183
  if (index >= -str.length && index <= str.length) {
173
184
  const lines = str.slice(0, index).split('\n');
@@ -188,6 +199,7 @@ class AstNode {
188
199
  return 0;
189
200
  }
190
201
  /** @private */
202
+ // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
191
203
  getGaps(i) {
192
204
  return 0;
193
205
  }
@@ -227,11 +239,12 @@ class AstNode {
227
239
  }
228
240
  /** @private */
229
241
  typeError(method, ...types) {
230
- return (0, debug_1.typeError)(this.constructor, method, ...types);
242
+ return typeError(this.constructor, method, ...types);
231
243
  }
232
244
  /** @private */
233
245
  seal(key) {
234
246
  this.#optional.add(key);
247
+ // @ts-expect-error noImplicitAny
235
248
  Object.defineProperty(this, key, { writable: false, enumerable: Boolean(this[key]), configurable: true });
236
249
  }
237
250
  /**
@@ -305,15 +318,10 @@ class AstNode {
305
318
  * @param node 待检测节点
306
319
  */
307
320
  contains(node) {
308
- return node instanceof AstNode
309
- ? node === this || this.childNodes.some(child => child.contains(node))
310
- : this.typeError('contains', 'AstNode');
321
+ return node === this || this.childNodes.some(child => child.contains(node));
311
322
  }
312
323
  /** @private */
313
324
  verifyChild(i, addition = 0) {
314
- if (!Number.isInteger(i)) {
315
- this.typeError('verifyChild', 'Number');
316
- }
317
325
  const { childNodes: { length } } = this;
318
326
  if (i < -length || i >= length + addition) {
319
327
  throw new RangeError(`不存在第 ${i} 个子节点!`);
@@ -332,11 +340,8 @@ class AstNode {
332
340
  this.addEventListener(type, listener, options);
333
341
  }
334
342
  }
335
- else if (typeof types === 'string' && typeof listener === 'function') {
336
- this.#events[options?.once ? 'once' : 'on'](types, listener);
337
- }
338
343
  else {
339
- this.typeError('addEventListener', 'String', 'Function');
344
+ this.#events[options?.once ? 'once' : 'on'](types, listener);
340
345
  }
341
346
  }
342
347
  /**
@@ -350,11 +355,8 @@ class AstNode {
350
355
  this.removeEventListener(type, listener);
351
356
  }
352
357
  }
353
- else if (typeof types === 'string' && typeof listener === 'function') {
354
- this.#events.off(types, listener);
355
- }
356
358
  else {
357
- this.typeError('removeEventListener', 'String', 'Function');
359
+ this.#events.off(types, listener);
358
360
  }
359
361
  }
360
362
  /**
@@ -367,11 +369,8 @@ class AstNode {
367
369
  this.removeAllEventListeners(type);
368
370
  }
369
371
  }
370
- else if (types === undefined || typeof types === 'string') {
371
- this.#events.removeAllListeners(types);
372
- }
373
372
  else {
374
- this.typeError('removeAllEventListeners', 'String');
373
+ this.#events.removeAllListeners(types);
375
374
  }
376
375
  }
377
376
  /**
@@ -379,7 +378,7 @@ class AstNode {
379
378
  * @param type 事件类型
380
379
  */
381
380
  listEventListeners(type) {
382
- return typeof type === 'string' ? this.#events.listeners(type) : this.typeError('listEventListeners', 'String');
381
+ return this.#events.listeners(type);
383
382
  }
384
383
  /**
385
384
  * 触发事件
@@ -387,10 +386,7 @@ class AstNode {
387
386
  * @param data 事件数据
388
387
  */
389
388
  dispatchEvent(e, data) {
390
- if (!(e instanceof Event)) {
391
- this.typeError('dispatchEvent', 'Event');
392
- }
393
- else if (!e.target) { // 初始化
389
+ if (!e.target) { // 初始化
394
390
  Object.defineProperty(e, 'target', { value: this, enumerable: true });
395
391
  /** 终止冒泡 */
396
392
  e.stopPropagation = function () {
@@ -422,10 +418,7 @@ class AstNode {
422
418
  * @throws `Error` 不在同一个语法树
423
419
  */
424
420
  compareDocumentPosition(other) {
425
- if (!(other instanceof AstNode)) {
426
- this.typeError('compareDocumentPosition', 'AstNode');
427
- }
428
- else if (this === other) {
421
+ if (this === other) {
429
422
  return 0;
430
423
  }
431
424
  else if (this.contains(other)) {
@@ -447,27 +440,19 @@ class AstNode {
447
440
  * @param left 列号
448
441
  */
449
442
  indexFromPos(top, left) {
450
- if (!Number.isInteger(top) || !Number.isInteger(left)) {
451
- this.typeError('indexFromPos', 'Number');
452
- }
453
443
  const lines = String(this).split('\n');
454
444
  return top >= 0 && left >= 0 && lines.length >= top + 1 && lines[top].length >= left
455
445
  ? lines.slice(0, top).reduce((acc, curLine) => acc + curLine.length + 1, 0) + left
456
446
  : undefined;
457
447
  }
458
- /**
459
- * 获取当前节点的相对位置,或其第`j`个子节点的相对位置
460
- * @param j 子节点序号
461
- */
462
- #getPosition(j) {
463
- return j === undefined
464
- ? this.parentNode?.posFromIndex(this.getRelativeIndex()) ?? { top: 0, left: 0 }
465
- : this.posFromIndex(this.getRelativeIndex(j));
448
+ /** 获取当前节点的相对位置 */
449
+ #getPosition() {
450
+ return this.parentNode?.posFromIndex(this.getRelativeIndex()) ?? { top: 0, left: 0 };
466
451
  }
467
452
  /** 获取当前节点的行列位置和大小 */
468
453
  getBoundingClientRect() {
469
454
  return { ...this.#getDimension(), ...this.getRootNode().posFromIndex(this.getAbsoluteIndex()) };
470
455
  }
471
456
  }
472
- Parser.classes['AstNode'] = __filename;
473
- module.exports = AstNode;
457
+ exports.AstNode = AstNode;
458
+ index_1.default.classes['AstNode'] = __filename;
@@ -0,0 +1,132 @@
1
+ import type { AstNodes, Token } from '../internal';
2
+ import type { Dimension, Position } from './node';
3
+ /** 模拟Range对象 */
4
+ export declare class AstRange {
5
+ #private;
6
+ /** 起点容器 */
7
+ get startContainer(): AstNodes;
8
+ /** 起点位置 */
9
+ get startOffset(): number;
10
+ /** 起点绝对位置 */
11
+ get startIndex(): number;
12
+ /** 终点容器 */
13
+ get endContainer(): AstNodes;
14
+ /** 终点位置 */
15
+ get endOffset(): number;
16
+ /** 终点绝对位置 */
17
+ get endIndex(): number;
18
+ /** 起始和终止位置是否重合 */
19
+ get collapsed(): boolean;
20
+ /** 最近的公共祖先 */
21
+ get commonAncestorContainer(): AstNodes;
22
+ /**
23
+ * 设置起点
24
+ * @param startNode 起点容器
25
+ * @param startOffset 起点位置
26
+ * @throws `RangeError` 不在同一个文档
27
+ * @throws `RangeError` offset取值超出范围
28
+ */
29
+ setStart(startNode: AstNodes, startOffset: number): void;
30
+ /**
31
+ * 设置终点
32
+ * @param endNode 终点容器
33
+ * @param endOffset 终点位置
34
+ * @throws `RangeError` 不在同一个文档
35
+ * @throws `RangeError` offset取值超出范围
36
+ */
37
+ setEnd(endNode: AstNodes, endOffset: number): void;
38
+ /**
39
+ * 在节点后设置起点
40
+ * @param referenceNode 节点
41
+ */
42
+ setStartAfter(referenceNode: AstNodes): void;
43
+ /**
44
+ * 在节点后设置终点
45
+ * @param referenceNode 节点
46
+ */
47
+ setEndAfter(referenceNode: AstNodes): void;
48
+ /**
49
+ * 在节点前设置起点
50
+ * @param referenceNode 节点
51
+ */
52
+ setStartBefore(referenceNode: AstNodes): void;
53
+ /**
54
+ * 在节点前设置终点
55
+ * @param referenceNode 节点
56
+ */
57
+ setEndBefore(referenceNode: AstNodes): void;
58
+ /**
59
+ * 根据字符位置设置起点
60
+ * @param root 根节点
61
+ * @param index 字符位置
62
+ */
63
+ setStartIndex(root: Token, index: number): void;
64
+ /**
65
+ * 根据字符位置设置终点
66
+ * @param root 根节点
67
+ * @param index 字符位置
68
+ */
69
+ setEndIndex(root: Token, index: number): void;
70
+ /**
71
+ * 根据行列号设置起点
72
+ * @param root 根节点
73
+ * @param x 列
74
+ * @param y 行
75
+ */
76
+ setStartPoint(root: Token, x: number, y: number): void;
77
+ /**
78
+ * 根据行列号设置终点
79
+ * @param root 根节点
80
+ * @param x 列
81
+ * @param y 行
82
+ */
83
+ setEndPoint(root: Token, x: number, y: number): void;
84
+ /**
85
+ * 设置Range包含整个节点的内容
86
+ * @param referenceNode 节点
87
+ */
88
+ selectNodeContents(referenceNode: AstNodes): void;
89
+ /**
90
+ * 设置Range包含整个节点
91
+ * @param referenceNode 节点
92
+ */
93
+ selectNode(referenceNode: AstNodes): void;
94
+ /**
95
+ * 使起始和终止位置重合
96
+ * @param toStart 重合至起始位置
97
+ */
98
+ collapse(toStart?: boolean): void;
99
+ /**
100
+ * 比较端点和Range的位置
101
+ * @param referenceNode 端点容器
102
+ * @param offset 端点位置
103
+ * @throws `RangeError` 不在同一个文档
104
+ */
105
+ comparePoint(referenceNode: AstNodes, offset: number): -1 | 0 | 1;
106
+ /**
107
+ * 端点是否在Range中
108
+ * @param referenceNode 端点容器
109
+ * @param offset 端点位置
110
+ * @throws `RangeError` 不在同一个文档
111
+ */
112
+ isPointInRange(referenceNode: AstNodes, offset: number): boolean;
113
+ /**
114
+ * 是否与节点相交
115
+ * @param referenceNode 节点
116
+ * @throws `RangeError` 不在同一个文档
117
+ */
118
+ intersectsNode(referenceNode: AstNodes): boolean;
119
+ /** 复制 */
120
+ cloneRange(): AstRange;
121
+ /** 删除 */
122
+ deleteContents(): void;
123
+ /** 获取行列位置和大小 */
124
+ getBoundingClientRect(): Dimension & Position;
125
+ /**
126
+ * 在起始位置插入节点
127
+ * @param newNode 插入的节点
128
+ */
129
+ insertNode(newNode: AstNodes): void;
130
+ /** 范围内的全部文本 */
131
+ toString(): string;
132
+ }