wikiparser-node 0.0.1 → 0.0.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.
package/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![npm version](https://badge.fury.io/js/wikiparser-node.svg)](https://www.npmjs.com/package/wikiparser-node)
1
2
  # 目录
2
3
  <details>
3
4
  <summary>展开</summary>
@@ -161,17 +162,18 @@
161
162
  6. [getKeys](#filetoken.getkeys)
162
163
  7. [getValue](#filetoken.getvalue)
163
164
  8. [setValue](#filetoken.setvalue)
165
+ 2. [原型属性](#filetoken.prototype.properties)
166
+ 1. [link](#filetoken.link)
167
+ 2. [size](#filetoken.size)
168
+ 3. [width](#filetoken.width)
169
+ 4. [height](#filetoken.height)
164
170
  18. [ImageParameterToken](#imageparametertoken)
165
171
  1. [原型方法](#imageparametertoken.prototype.methods)
166
172
  1. [getValue](#imageparametertoken.getvalue)
167
173
  2. [setValue](#imageparametertoken.setvalue)
168
174
  19. [ExtLinkToken](#extlinktoken)
169
175
  1. [原型方法](#extlinktoken.prototype.methods)
170
- 1. [getUrl](#extlinktoken.geturl)
171
- 2. [setTarget](#extlinktoken.settarget)
172
- 3. [setLinkText](#extlinktoken.setlinktext)
173
- 2. [原型属性](#extlinktoken.prototype.properties)
174
- 1. [protocol](#extlinktoken.protocol)
176
+ 1. [setLinkText](#extlinktoken.setlinktext)
175
177
  20. [MagicLinkToken](#magiclinktoken)
176
178
  1. [原型方法](#magiclinktoken.prototype.methods)
177
179
  1. [getUrl](#magiclinktoken.geturl)
@@ -1654,6 +1656,53 @@ assert(root.toString() === '[[file:a|100px|framed]]');
1654
1656
  ```
1655
1657
  </details>
1656
1658
 
1659
+ ## 原型属性<a id="filetoken.prototype.properties"></a>
1660
+ <details>
1661
+ <summary>展开</summary>
1662
+
1663
+ **link**: string\|symbol<a id="filetoken.link"></a>
1664
+ - 链接目标。
1665
+
1666
+ ```js
1667
+ var root = Parser.parse('[[file:a|link=[[talk:b]]]]'),
1668
+ file = root.firstChild;
1669
+ assert(file.link === 'Talk:B');
1670
+ file.link = '//c';
1671
+ assert(root.toString() === '[[file:a|link=//c]]');
1672
+ ```
1673
+
1674
+ **size**: {width: string, height: string}<a id="filetoken.size"></a>
1675
+ - 图片尺寸。
1676
+
1677
+ ```js
1678
+ var root = Parser.parse('[[file:a|x1px]]'),
1679
+ file = root.firstChild;
1680
+ assert.deepStrictEqual(file.size, {width: '', height: '1'});
1681
+ ```
1682
+
1683
+ **width**: number<a id="filetoken.width"></a>
1684
+ - 图片宽度。
1685
+
1686
+ ```js
1687
+ var root = Parser.parse('[[file:a|1x1px]]'),
1688
+ file = root.firstChild;
1689
+ assert(file.width === '1');
1690
+ file.width = undefined;
1691
+ assert(root.toString() === '[[file:a|x1px]]');
1692
+ ```
1693
+
1694
+ **height**: number<a id="filetoken.height"></a>
1695
+ - 图片高度。
1696
+
1697
+ ```js
1698
+ var root = Parser.parse('[[file:a|1x1px]]'),
1699
+ file = root.firstChild;
1700
+ assert(file.height === '1');
1701
+ file.height = undefined;
1702
+ assert(root.toString() === '[[file:a|1px]]');
1703
+ ```
1704
+ </details>
1705
+
1657
1706
  [返回目录](#目录)
1658
1707
 
1659
1708
  # ImageParameterToken
@@ -1688,18 +1737,12 @@ assert(root.toString() === '[[file:a|x100px]]');
1688
1737
  [返回目录](#目录)
1689
1738
 
1690
1739
  # ExtLinkToken
1691
- `[]`内的外部链接。
1740
+ `[]`内的外部链接。这个类同时混合了 [MagicLinkToken](#magiclinktoken) 类的方法和原型属性。
1692
1741
 
1693
1742
  ## 原型方法<a id="extlinktoken.prototype.methods"></a>
1694
1743
  <details>
1695
1744
  <summary>展开</summary>
1696
1745
 
1697
- **getUrl**(): URL<a id="extlinktoken.geturl"></a>
1698
- - 混合自 [MagicLinkToken.getUrl](#magiclinktoken.geturl)。
1699
-
1700
- **setTarget**(url: string\|URL): void<a id="extlinktoken.settarget"></a>
1701
- - 混合自 [MagicLinkToken.setTarget](#magiclinktoken.settarget)。
1702
-
1703
1746
  **setLinkText**(text: string): void<a id="extlinktoken.setlinktext"></a>
1704
1747
  - 修改外链文本。
1705
1748
 
@@ -1711,14 +1754,6 @@ assert(root.toString() === '[//example.org]');
1711
1754
  ```
1712
1755
  </details>
1713
1756
 
1714
- ## 原型属性<a id="extlinktoken.prototype.properties"></a>
1715
- <details>
1716
- <summary>展开</summary>
1717
-
1718
- **protocol**: string<a id="extlinktoken.protocol"></a>
1719
- - 混合自 [MagicLinkToken.protocol](#magiclinktoken.protocol)。
1720
- </details>
1721
-
1722
1757
  [返回目录](#目录)
1723
1758
 
1724
1759
  # MagicLinkToken
@@ -119,6 +119,7 @@
119
119
  "829": "Module talk"
120
120
  },
121
121
  "nsid": {
122
+ "": 0,
122
123
  "media": -2,
123
124
  "媒体": -2,
124
125
  "媒体文件": -2,
@@ -110,6 +110,7 @@
110
110
  "829": "Module talk"
111
111
  },
112
112
  "nsid": {
113
+ "": 0,
113
114
  "media": -2,
114
115
  "媒体": -2,
115
116
  "媒体文件": -2,
@@ -114,6 +114,7 @@
114
114
  "829": "Module talk"
115
115
  },
116
116
  "nsid": {
117
+ "": 0,
117
118
  "media": -2,
118
119
  "媒体": -2,
119
120
  "媒体文件": -2,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
package/src/extLink.js CHANGED
@@ -12,16 +12,13 @@ class ExtLinkToken extends Token {
12
12
  type = 'ext-link';
13
13
  #space;
14
14
 
15
- /** @this {ExtLinkToken & {firstElementChild: MagicLinkToken}} */
15
+ /** @this {{firstChild: MagicLinkToken}} */
16
16
  get protocol() {
17
- return this.firstElementChild.protocol;
17
+ return this.firstChild.protocol;
18
18
  }
19
- /**
20
- * @this {ExtLinkToken & {firstElementChild: MagicLinkToken}}
21
- * @param {string} value
22
- */
19
+ /** @this {{firstChild: MagicLinkToken}} */
23
20
  set protocol(value) {
24
- this.firstElementChild.protocol = value;
21
+ this.firstChild.protocol = value;
25
22
  }
26
23
 
27
24
  /**
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {typeError} = require('../util/debug'),
4
- {extUrlChar} = require('../util/string'),
4
+ {text, extUrlChar} = require('../util/string'),
5
5
  Title = require('../lib/title'),
6
6
  /** @type {Parser} */ Parser = require('..'),
7
7
  Token = require('.');
@@ -14,9 +14,13 @@ class ImageParameterToken extends Token {
14
14
  type = 'image-parameter';
15
15
  #syntax = '';
16
16
 
17
+ static #noLink = Symbol('no-link');
18
+
17
19
  /**
18
- * @param {string} key
20
+ * @template {string} T
21
+ * @param {T} key
19
22
  * @param {string} value
23
+ * @returns {T extends 'link' ? string|Symbol : boolean}
20
24
  */
21
25
  static #validate(key, value, config = Parser.getConfig()) {
22
26
  value = value.replace(/\x00\d+t\x7f/g, '').trim();
@@ -26,11 +30,11 @@ class ImageParameterToken extends Token {
26
30
  return true;
27
31
  } else if (key === 'link') {
28
32
  if (!value) {
29
- return true;
33
+ return this.#noLink;
30
34
  }
31
35
  const regex = new RegExp(`(?:${config.protocol}|//)${extUrlChar}`, 'ui');
32
36
  if (regex.test(value)) {
33
- return true;
37
+ return value;
34
38
  }
35
39
  if (/^\[\[.+]]$/.test(value)) {
36
40
  value = value.slice(2, -2);
@@ -40,11 +44,62 @@ class ImageParameterToken extends Token {
40
44
  value = decodeURIComponent(value);
41
45
  } catch {}
42
46
  }
43
- return new Title(value, 0, config).valid;
47
+ const {title, fragment, valid} = new Title(value, 0, config);
48
+ return valid && `${title}${fragment && '#'}${fragment}`;
44
49
  }
45
50
  return !isNaN(value);
46
51
  }
47
52
 
53
+ get link() {
54
+ if (this.name === 'link') {
55
+ return ImageParameterToken.#validate('link', this.getValue(), this.getAttribute('config'));
56
+ }
57
+ return undefined;
58
+ }
59
+ set link(value) {
60
+ if (this.name === 'link') {
61
+ value = value === ImageParameterToken.#noLink ? '' : value;
62
+ this.setValue(value);
63
+ }
64
+ }
65
+ get size() {
66
+ if (this.name === 'width') {
67
+ const /** @type {string} */ size = this.getValue().trim();
68
+ if (!size.includes('{{')) {
69
+ const [width, height = ''] = size.split('x');
70
+ return {width, height};
71
+ }
72
+ const token = Parser.parse(size, false, 2, this.getAttribute('config')),
73
+ {childNodes} = token,
74
+ i = childNodes.findIndex(child => typeof child === 'string' && child.includes('x'));
75
+ if (i === -1) {
76
+ return {width: size, height: ''};
77
+ }
78
+ token.splitText(i, childNodes[i].indexOf('x'));
79
+ token.splitText(i + 1, 1);
80
+ return {width: text(token.childNodes.slice(0, i + 1)), height: text(token.childNodes.slice(i + 2))};
81
+ }
82
+ return undefined;
83
+ }
84
+ get width() {
85
+ return this.size?.width;
86
+ }
87
+ set width(width) {
88
+ if (this.name === 'width') {
89
+ const {height} = this;
90
+ this.setValue(`${String(width || '')}${height && 'x'}${height}`);
91
+ }
92
+ }
93
+ get height() {
94
+ return this.size?.height;
95
+ }
96
+ set height(height) {
97
+ height = String(height || '');
98
+ if (this.name === 'width') {
99
+ this.setValue(`${this.width}${height && 'x'}${height}`);
100
+ }
101
+ }
102
+
48
103
  /**
49
104
  * @param {string} str
50
105
  * @param {accum} accum
package/src/link/file.js CHANGED
@@ -20,6 +20,38 @@ class FileToken extends LinkToken {
20
20
  setLinkText = undefined;
21
21
  pipeTrick = undefined;
22
22
 
23
+ get link() {
24
+ return this.getArg('link')?.link;
25
+ }
26
+ set link(value) {
27
+ this.setValue('link', value);
28
+ }
29
+ get size() {
30
+ return this.getArg('width')?.size;
31
+ }
32
+ get width() {
33
+ return this.size?.width;
34
+ }
35
+ set width(width) {
36
+ const arg = this.getArg('width');
37
+ if (arg) {
38
+ arg.width = width;
39
+ } else {
40
+ this.setValue('width', width);
41
+ }
42
+ }
43
+ get height() {
44
+ return this.size?.height;
45
+ }
46
+ set height(height) {
47
+ const arg = this.getArg('width');
48
+ if (arg) {
49
+ arg.height = height;
50
+ } else {
51
+ this.setValue('width', `x${height}`);
52
+ }
53
+ }
54
+
23
55
  /**
24
56
  * @param {string} link
25
57
  * @param {string|undefined} text
package/src/magicLink.js CHANGED
@@ -15,7 +15,6 @@ class MagicLinkToken extends Token {
15
15
  get protocol() {
16
16
  return this.text().match(this.#protocolRegex)?.[0];
17
17
  }
18
- /** @param {string} value */
19
18
  set protocol(value) {
20
19
  if (typeof value !== 'string') {
21
20
  typeError(this, 'protocol', 'String');
@@ -38,6 +37,14 @@ class MagicLinkToken extends Token {
38
37
  this.#protocolRegex = new RegExp(`^(?:${config.protocol}${doubleSlash ? '|//' : ''})`, 'i');
39
38
  }
40
39
 
40
+ afterBuild() {
41
+ const ParameterToken = require('./parameter'), // eslint-disable-line no-unused-vars
42
+ /** @type {ParameterToken} */ parameter = this.closest('parameter');
43
+ if (parameter?.getValue() === this.text()) {
44
+ this.replaceWith(this.toString());
45
+ }
46
+ }
47
+
41
48
  getUrl() {
42
49
  const url = this.text();
43
50
  try {