wikiparser-node 1.0.1-b → 1.0.3-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.
@@ -1,11 +1,10 @@
1
1
  (() => {
2
2
  const MAX_STAGE = 11;
3
- /** web worker */
4
3
  const workerJS = () => {
5
- self.importScripts('https://fastly.jsdelivr.net/gh/bhsd-harry/wikiparser-node@1.0.1-b/bundle/bundle.min.js');
4
+ self.importScripts('https://fastly.jsdelivr.net/gh/bhsd-harry/wikiparser-node@1.0.3-b/bundle/bundle.min.js');
6
5
  const { Parser } = self, entities = { '&': 'amp', '<': 'lt', '>': 'gt' };
7
- /** @implements */
8
6
  self.onmessage = ({ data }) => {
7
+ var _a;
9
8
  const [command, qid, ...args] = data;
10
9
  switch (command) {
11
10
  case 'setI18N':
@@ -20,9 +19,8 @@
20
19
  case 'lint':
21
20
  self.postMessage([qid, Parser.parse(...args).lint(), args[0]]);
22
21
  break;
23
- // case 'print':
24
22
  default: {
25
- const stage = args[2] === undefined ? MAX_STAGE : args[2];
23
+ const stage = (_a = args[2]) !== null && _a !== void 0 ? _a : MAX_STAGE;
26
24
  self.postMessage([
27
25
  qid,
28
26
  Parser.parse(...args).childNodes.map(child => [
@@ -39,18 +37,7 @@
39
37
  };
40
38
  const blob = new Blob([`(${String(workerJS).replace(/MAX_STAGE/gu, String(MAX_STAGE))})()`], { type: 'text/javascript' }), url = URL.createObjectURL(blob), worker = new Worker(url);
41
39
  URL.revokeObjectURL(url);
42
- /**
43
- * 生成事件监听函数
44
- * @param qid 输入id
45
- * @param resolve Promise对象的resolve函数
46
- * @param raw 原始文本
47
- */
48
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
40
  const getListener = (qid, resolve, raw) => {
50
- /**
51
- * 事件监听函数
52
- * @param {{data: [number, unknown, string]}} e 消息事件
53
- */
54
41
  const listener = ({ data: [rid, res, resRaw] }) => {
55
42
  if (rid === qid && (raw === undefined || raw === resRaw)) {
56
43
  worker.removeEventListener('message', listener);
@@ -59,42 +46,20 @@
59
46
  };
60
47
  return listener;
61
48
  };
62
- /**
63
- * 更新I18N消息
64
- * @param i18n I18N消息
65
- */
66
49
  const setI18N = (i18n) => {
67
50
  worker.postMessage(['setI18N', i18n]);
68
51
  };
69
- /**
70
- * 更新解析设置
71
- * @param config 设置
72
- */
73
52
  const setConfig = (config) => {
74
53
  worker.postMessage(['setConfig', config]);
75
54
  };
76
- /** 获取Parser.minConfig */
77
55
  const getConfig = () => new Promise(resolve => {
78
56
  worker.addEventListener('message', getListener(-3, resolve));
79
57
  worker.postMessage(['getConfig', -3]);
80
58
  });
81
- /**
82
- * 将解析改为异步执行
83
- * @param wikitext wikitext
84
- * @param include 是否嵌入
85
- * @param stage 解析层级
86
- * @param qid Printer编号
87
- */
88
59
  const print = (wikitext, include, stage, qid = -1) => new Promise(resolve => {
89
60
  worker.addEventListener('message', getListener(qid, resolve));
90
61
  worker.postMessage(['print', qid, wikitext, include, stage]);
91
62
  });
92
- /**
93
- * 将语法分析改为异步执行
94
- * @param wikitext wikitext
95
- * @param include 是否嵌入
96
- * @param qid Linter编号,暂时固定为`-2`
97
- */
98
63
  const lint = (wikitext, include, qid = -2) => new Promise(resolve => {
99
64
  worker.addEventListener('message', getListener(qid, resolve, wikitext));
100
65
  worker.postMessage(['lint', qid, wikitext, include]);
@@ -7,87 +7,74 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
11
+ if (kind === "m") throw new TypeError("Private method is not writable");
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
13
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
14
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
15
+ };
16
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
17
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
18
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
19
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
+ };
10
21
  (() => {
22
+ var _Printer_instances, _Printer_id, _Printer_preview, _Printer_textbox, _Printer_root, _Printer_viewportChanged, _Printer_tick, _Printer_paint;
11
23
  const { wikiparse } = window, { MAX_STAGE } = wikiparse;
12
- /** 用于打印AST */
13
24
  class Printer {
14
- /* eslint-enable es-x/no-class-fields */
15
- /**
16
- * @param preview 置于下层的代码高亮
17
- * @param textbox 置于上层的文本框
18
- * @param include 是否嵌入
19
- */
20
25
  constructor(preview, textbox, include) {
21
- this.id = wikiparse.id++;
26
+ _Printer_instances.add(this);
27
+ _Printer_id.set(this, void 0);
28
+ _Printer_preview.set(this, void 0);
29
+ _Printer_textbox.set(this, void 0);
30
+ _Printer_root.set(this, void 0);
31
+ _Printer_viewportChanged.set(this, void 0);
32
+ __classPrivateFieldSet(this, _Printer_id, wikiparse.id++, "f");
33
+ __classPrivateFieldSet(this, _Printer_preview, preview, "f");
34
+ __classPrivateFieldSet(this, _Printer_textbox, textbox, "f");
35
+ __classPrivateFieldSet(this, _Printer_root, [], "f");
36
+ __classPrivateFieldSet(this, _Printer_viewportChanged, false, "f");
22
37
  this.include = Boolean(include);
23
- this.preview = preview;
24
- this.textbox = textbox;
25
- this.root = [];
26
38
  this.running = undefined;
27
- this.viewportChanged = false;
28
39
  this.ticks = [0, undefined];
29
40
  }
30
- /** 倒计时 */
31
- tick() {
32
- setTimeout(() => {
33
- const { ticks } = this;
34
- if (ticks[0] > 0) {
35
- ticks[0] -= 500;
36
- this[ticks[0] <= 0 ? ticks[1] : 'tick']();
37
- }
38
- }, 500);
39
- }
40
- /**
41
- * 用于debounce
42
- * @param delay 延迟
43
- * @param method 方法
44
- */
45
41
  queue(delay, method) {
46
42
  const { ticks } = this, [state] = ticks;
47
43
  if (state <= 0 || method === 'coarsePrint' || ticks[1] !== 'coarsePrint') {
48
44
  ticks[0] = delay;
49
45
  ticks[1] = method;
50
46
  if (state <= 0) {
51
- this.tick();
47
+ __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_tick).call(this);
52
48
  }
53
49
  }
54
50
  }
55
- /** 渲染 */
56
- paint() {
57
- this.preview.innerHTML = `<span class="wpb-root">${this.root.map(([, , printed]) => printed).join('')}</span> `;
58
- this.preview.scrollTop = this.textbox.scrollTop;
59
- this.preview.classList.remove('active');
60
- this.textbox.style.color = 'transparent';
61
- }
62
- /** 初步解析 */
63
51
  coarsePrint() {
64
52
  return __awaiter(this, void 0, void 0, function* () {
65
53
  if (this.running) {
66
54
  return undefined;
67
55
  }
68
- const { include, textbox: { value } } = this, parsed = yield wikiparse.print(value, include, 2, this.id);
69
- if (this.include !== include || this.textbox.value !== value) {
56
+ const { include } = this, { value } = __classPrivateFieldGet(this, _Printer_textbox, "f"), parsed = yield wikiparse.print(value, include, 2, __classPrivateFieldGet(this, _Printer_id, "f"));
57
+ if (this.include !== include || __classPrivateFieldGet(this, _Printer_textbox, "f").value !== value) {
70
58
  this.running = undefined;
71
59
  this.running = this.coarsePrint();
72
60
  return this.running;
73
61
  }
74
- this.root = parsed;
75
- this.paint();
62
+ __classPrivateFieldSet(this, _Printer_root, parsed, "f");
63
+ __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_paint).call(this);
76
64
  this.running = undefined;
77
65
  this.running = this.finePrint();
78
66
  return this.running;
79
67
  });
80
68
  }
81
- /** 根据可见范围精细解析 */
82
69
  finePrint() {
83
70
  return __awaiter(this, void 0, void 0, function* () {
84
71
  if (this.running) {
85
- this.viewportChanged = true;
72
+ __classPrivateFieldSet(this, _Printer_viewportChanged, true, "f");
86
73
  return undefined;
87
74
  }
88
- this.viewportChanged = false;
89
- const { root, preview: { scrollHeight, offsetHeight: parentHeight, scrollTop, children: [rootNode] }, include, textbox: { value }, } = this;
90
- let text = value, start = 0, { length: end } = root;
75
+ __classPrivateFieldSet(this, _Printer_viewportChanged, false, "f");
76
+ const { include } = this, { value } = __classPrivateFieldGet(this, _Printer_textbox, "f"), { scrollHeight, offsetHeight: parentHeight, scrollTop, children: [rootNode] } = __classPrivateFieldGet(this, _Printer_preview, "f");
77
+ let text = value, start = 0, { length: end } = __classPrivateFieldGet(this, _Printer_root, "f");
91
78
  if (scrollHeight > parentHeight) {
92
79
  const childNodes = [...rootNode.childNodes], headings = childNodes.filter(({ className }) => className === 'wpb-heading'), { length } = headings;
93
80
  if (length > 0) {
@@ -96,26 +83,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
96
83
  let j = headings.slice(i).findIndex(({ offsetTop }) => offsetTop >= scrollTop + parentHeight);
97
84
  j = j === -1 ? length : i + j;
98
85
  start = i ? childNodes.indexOf(headings[i - 1]) : 0;
99
- while (i <= j && root[start][0] === MAX_STAGE) {
86
+ while (i <= j && __classPrivateFieldGet(this, _Printer_root, "f")[start][0] === MAX_STAGE) {
100
87
  start = childNodes.indexOf(headings[i++]);
101
88
  }
102
89
  end = j === length ? end : childNodes.indexOf(headings[j]);
103
- while (i <= j && root[end - 1][0] === MAX_STAGE) {
90
+ while (i <= j && __classPrivateFieldGet(this, _Printer_root, "f")[end - 1][0] === MAX_STAGE) {
104
91
  end = childNodes.indexOf(headings[--j]);
105
92
  }
106
- text = root.slice(start, end).map(([, str]) => str).join('');
93
+ text = __classPrivateFieldGet(this, _Printer_root, "f").slice(start, end).map(([, str]) => str).join('');
107
94
  }
108
95
  }
109
96
  if (start === end) {
110
97
  this.running = undefined;
111
98
  return undefined;
112
99
  }
113
- const parsed = yield wikiparse.print(text, include, MAX_STAGE, this.id);
114
- if (this.include === include && this.textbox.value === value) {
115
- this.root.splice(start, end - start, ...parsed);
116
- this.paint();
100
+ const parsed = yield wikiparse.print(text, include, MAX_STAGE, __classPrivateFieldGet(this, _Printer_id, "f"));
101
+ if (this.include === include && __classPrivateFieldGet(this, _Printer_textbox, "f").value === value) {
102
+ __classPrivateFieldGet(this, _Printer_root, "f").splice(start, end - start, ...parsed);
103
+ __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_paint).call(this);
117
104
  this.running = undefined;
118
- if (this.viewportChanged) {
105
+ if (__classPrivateFieldGet(this, _Printer_viewportChanged, "f")) {
119
106
  this.running = this.finePrint();
120
107
  }
121
108
  }
@@ -127,12 +114,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
127
114
  });
128
115
  }
129
116
  }
130
- /**
131
- * 高亮textarea
132
- * @param textbox textarea元素
133
- * @param include 是否嵌入
134
- * @throws `TypeError` 不是textarea
135
- */
117
+ _Printer_id = new WeakMap(), _Printer_preview = new WeakMap(), _Printer_textbox = new WeakMap(), _Printer_root = new WeakMap(), _Printer_viewportChanged = new WeakMap(), _Printer_instances = new WeakSet(), _Printer_tick = function _Printer_tick() {
118
+ setTimeout(() => {
119
+ const { ticks } = this, [t, method] = ticks;
120
+ if (t > 0) {
121
+ ticks[0] -= 500;
122
+ if (t <= 500) {
123
+ this[method]();
124
+ }
125
+ else {
126
+ __classPrivateFieldGet(this, _Printer_instances, "m", _Printer_tick).call(this);
127
+ }
128
+ }
129
+ }, 500);
130
+ }, _Printer_paint = function _Printer_paint() {
131
+ __classPrivateFieldGet(this, _Printer_preview, "f").innerHTML = `<span class="wpb-root">${__classPrivateFieldGet(this, _Printer_root, "f").map(([, , printed]) => printed).join('')}</span> `;
132
+ __classPrivateFieldGet(this, _Printer_preview, "f").scrollTop = __classPrivateFieldGet(this, _Printer_textbox, "f").scrollTop;
133
+ __classPrivateFieldGet(this, _Printer_preview, "f").classList.remove('active');
134
+ __classPrivateFieldGet(this, _Printer_textbox, "f").style.color = 'transparent';
135
+ };
136
136
  const edit = (textbox, include) => {
137
137
  if (!(textbox instanceof HTMLTextAreaElement)) {
138
138
  throw new TypeError('wikiparse.edit方法仅可用于textarea元素!');
@@ -9,12 +9,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  (() => {
11
11
  const { wikiparse } = window;
12
- /**
13
- * 高亮代码块
14
- * @param ele 代码块
15
- * @param linenums 是否添加行号
16
- * @param start 起始行号
17
- */
18
12
  const highlight = (ele, linenums, start = 1) => __awaiter(void 0, void 0, void 0, function* () {
19
13
  if (ele.classList.contains('highlighted')) {
20
14
  return;
@@ -22,7 +16,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22
16
  const html = (yield wikiparse.print(ele.innerText)).map(([, , printed]) => printed).join('');
23
17
  ele.classList.add('highlighted');
24
18
  if (linenums) {
25
- // 添加行号。这里不使用<table>排版,而是使用<ol>
26
19
  const lines = html.split('\n').map((line, i) => {
27
20
  const li = document.createElement('li');
28
21
  li.id = `L${i + start}`;
@@ -34,7 +27,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
34
27
  }
35
28
  const ol = document.createElement('ol');
36
29
  ol.start = start;
37
- ol.style.paddingLeft = `${(lines.length + start - 1).toString().length + 2.5}ch`;
30
+ ol.style.paddingLeft = `${String(lines.length + start - 1).length + 2.5}ch`;
38
31
  ol.append(...lines);
39
32
  ele.replaceChildren(ol);
40
33
  }
@@ -7,37 +7,42 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
11
+ if (kind === "m") throw new TypeError("Private method is not writable");
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
13
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
14
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
15
+ };
16
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
17
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
18
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
19
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
+ };
10
21
  (() => {
22
+ var _Linter_instances, _Linter_id, _Linter_wikitext, _Linter_running, _Linter_lint;
11
23
  const { wikiparse } = window;
12
- /** 用于语法分析 */
13
24
  class Linter {
14
- /* eslint-enable es-x/no-class-fields */
15
- /** @param include 是否嵌入 */
16
25
  constructor(include) {
17
- this.id = wikiparse.id++;
26
+ _Linter_instances.add(this);
27
+ _Linter_id.set(this, void 0);
28
+ _Linter_wikitext.set(this, void 0);
29
+ _Linter_running.set(this, void 0);
30
+ __classPrivateFieldSet(this, _Linter_id, wikiparse.id++, "f");
31
+ __classPrivateFieldSet(this, _Linter_wikitext, undefined, "f");
32
+ __classPrivateFieldSet(this, _Linter_running, undefined, "f");
18
33
  this.include = Boolean(include);
19
- this.wikitext = undefined;
20
- this.running = undefined;
21
34
  }
22
- /**
23
- * 提交语法分析
24
- * @param wikitext 待分析的文本
25
- */
26
35
  queue(wikitext) {
27
- this.wikitext = wikitext;
28
- this.running = this.lint(wikitext);
29
- return this.running;
30
- }
31
- /**
32
- * 执行语法分析
33
- * @param wikitext 待分析的文本
34
- */
35
- lint(wikitext) {
36
- return __awaiter(this, void 0, void 0, function* () {
37
- const { include } = this, errors = yield wikiparse.lint(wikitext, include, this.id);
38
- return this.include === include && this.wikitext === wikitext ? errors : this.running;
39
- });
36
+ __classPrivateFieldSet(this, _Linter_wikitext, wikitext, "f");
37
+ __classPrivateFieldSet(this, _Linter_running, __classPrivateFieldGet(this, _Linter_instances, "m", _Linter_lint).call(this, wikitext), "f");
38
+ return __classPrivateFieldGet(this, _Linter_running, "f");
40
39
  }
41
40
  }
41
+ _Linter_id = new WeakMap(), _Linter_wikitext = new WeakMap(), _Linter_running = new WeakMap(), _Linter_instances = new WeakSet(), _Linter_lint = function _Linter_lint(wikitext) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const { include } = this, errors = yield wikiparse.lint(wikitext, include, __classPrivateFieldGet(this, _Linter_id, "f"));
44
+ return this.include === include && __classPrivateFieldGet(this, _Linter_wikitext, "f") === wikitext ? errors : __classPrivateFieldGet(this, _Linter_running, "f");
45
+ });
46
+ };
42
47
  wikiparse.Linter = Linter;
43
48
  })();
package/i18n/zh-hans.json CHANGED
@@ -27,6 +27,8 @@
27
27
  "invalid self-closing tag": "无效自封闭标签",
28
28
  "invisible content inside triple brackets": "三重括号内的不可见部分",
29
29
  "lonely \"$1\"": "孤立的\"$1\"",
30
+ "missing module function": "缺少模块函数",
31
+ "missing module name": "缺少模块名称",
30
32
  "nonzero tabindex": "不为0的tabindex",
31
33
  "nothing should be in <$1>": "<$1>标签内不应有任何内容",
32
34
  "section header in a HTML tag": "HTML标签属性中的段落标题",
package/i18n/zh-hant.json CHANGED
@@ -27,6 +27,8 @@
27
27
  "invalid self-closing tag": "無效自封閉標籤",
28
28
  "invisible content inside triple brackets": "三重括號內的不可見部分",
29
29
  "lonely \"$1\"": "孤立的\"$1\"",
30
+ "missing module function": "缺少模組函式",
31
+ "missing module name": "缺少模組名稱",
30
32
  "nonzero tabindex": "不為0的tabindex",
31
33
  "nothing should be in <$1>": "<$1>標籤內不應有任何內容",
32
34
  "section header in a HTML tag": "HTML標籤屬性中的段落標題",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "1.0.1-b",
3
+ "version": "1.0.3-b",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -31,12 +31,12 @@
31
31
  },
32
32
  "scripts": {
33
33
  "prepublishOnly": "webpack build --mode production && npm run build:ext",
34
- "build:core": "rm bundle/*.js; webpack build --mode development; webpack build --mode production",
35
- "build:ext": "rm -rf extensions/dist/; tsc --project extensions/tsconfig.json; ls extensions/dist/*.js | xargs gsed -i '/export {};/d'",
34
+ "build:core": "rm bundle/*.js; webpack build --mode development; webpack build --mode production; eslint --no-eslintrc -c .eslintrc.browser.json bundle/",
35
+ "build:ext": "rm -rf extensions/dist/; tsc --project extensions/tsconfig.json; ls extensions/dist/*.js | xargs gsed -i '/export {};/d'; eslint --no-eslintrc -c .eslintrc.browser.json extensions/dist/",
36
36
  "build:test": "rm -rf dist/; tsc; gsed -i 's|./config/minimum|../config/minimum|' dist/index.js",
37
37
  "build": "npm run build:core && npm run build:ext",
38
38
  "diff": "git diff --ignore-all-space --color-moved",
39
- "lint:ts": "eslint --cache --ignore-pattern 'dist/' .",
39
+ "lint:ts": "tsc --noEmit && eslint --cache .",
40
40
  "lint:json": "ajv -s config/.schema.json -d 'config/*.json' --strict=true --strict-required=false",
41
41
  "lint:css": "stylelint extensions/*.css",
42
42
  "lint": "npm run lint:ts && npm run lint:json && npm run lint:css",
@@ -61,10 +61,10 @@
61
61
  "eslint-plugin-regexp": "^2.1.1",
62
62
  "eslint-plugin-unicorn": "^49.0.0",
63
63
  "stylelint": "^15.11.0",
64
- "stylelint-config-recommended": "^10.0.1",
65
- "typescript": "^5.0.3",
64
+ "stylelint-config-recommended": "^13.0.0",
66
65
  "webpack-cli": "^5.1.4",
67
- "webpack": "^5.89.0"
66
+ "webpack": "^5.89.0",
67
+ "typescript": "^5.1.6"
68
68
  },
69
69
  "engines": {
70
70
  "node": "^20.9.0"