From 8ca07ded99badebdc90cbf39f0f4eab026bba243 Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 09:53:21 +0800 Subject: [PATCH 1/9] fix(Title): order of decoding close #37 --- lib/title.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/title.ts b/lib/title.ts index c2e4c0a1..ecea3507 100644 --- a/lib/title.ts +++ b/lib/title.ts @@ -96,13 +96,12 @@ export class Title { if (fragment === undefined) { this.#fragment = undefined; } else { - fragment = decodeHtml(fragment); if (fragment.includes('%')) { try { fragment = rawurldecode(fragment); } catch {} } - this.#fragment = fragment.replace(/[_ ]+/gu, ' ').trimEnd().replaceAll(' ', '_'); + this.#fragment = decodeHtml(fragment).replace(/[_ ]+/gu, ' ').trimEnd().replaceAll(' ', '_'); } } @@ -118,7 +117,6 @@ export class Title { */ constructor(title: string, defaultNs: number, config: Config, decode: boolean, selfLink: boolean) { const subpage = title.trim().startsWith('../'); - title = decodeHtml(title); if (decode && title.includes('%')) { try { const encoded = /%(?!21|3[ce]|5[bd]|7[b-d])[\da-f]{2}/iu.test(title); @@ -126,7 +124,7 @@ export class Title { this.encoded = encoded; } catch {} } - title = title.replace(/[_ ]+/gu, ' ').trim(); + title = decodeHtml(title).replace(/[_ ]+/gu, ' ').trim(); if (subpage) { this.#ns = 0; } else { From 5c04fbbcd61fea5e50f2539e96425b7f6b09dfa3 Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:10:47 +0800 Subject: [PATCH 2/9] feat(constants): stages for token types --- util/constants.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/util/constants.ts b/util/constants.ts index b7bc6e90..fb05bf37 100644 --- a/util/constants.ts +++ b/util/constants.ts @@ -7,6 +7,32 @@ export enum BuildMethod { Text, } +export const stages = { + redirect: 1, + comment: 1, + ext: 1, + include: 1, + noinclude: 1, + arg: 2, + template: 2, + 'magic-word': 2, + heading: 2, + html: 3, + table: 4, + hr: 5, + 'double-underscore': 5, + link: 6, + file: 6, + category: 6, + quote: 7, + 'ext-link': 8, + 'free-ext-link': 9, + 'magic-link': 9, + list: 10, + dd: 10, + converter: 11, +}; + /* NOT FOR BROWSER */ export const classes: Record = {}, From 356938da2f0a1bfd3d3efd6024972c9d90403657 Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:15:28 +0800 Subject: [PATCH 3/9] revert(constants): move `stages` to base.ts --- util/constants.ts | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/util/constants.ts b/util/constants.ts index fb05bf37..b7bc6e90 100644 --- a/util/constants.ts +++ b/util/constants.ts @@ -7,32 +7,6 @@ export enum BuildMethod { Text, } -export const stages = { - redirect: 1, - comment: 1, - ext: 1, - include: 1, - noinclude: 1, - arg: 2, - template: 2, - 'magic-word': 2, - heading: 2, - html: 3, - table: 4, - hr: 5, - 'double-underscore': 5, - link: 6, - file: 6, - category: 6, - quote: 7, - 'ext-link': 8, - 'free-ext-link': 9, - 'magic-link': 9, - list: 10, - dd: 10, - converter: 11, -}; - /* NOT FOR BROWSER */ export const classes: Record = {}, From 5c300943b3f68b9dbe354329c68e29f7df722b8e Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:22:47 +0800 Subject: [PATCH 4/9] feat(base): infer `maxStage` from token type(s) --- base.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/base.ts b/base.ts index d8a3a877..c8572f5f 100644 --- a/base.ts +++ b/base.ts @@ -93,6 +93,35 @@ export type TokenTypes = 'root' | 'param-line' | 'imagemap-link'; +export const stages = { + redirect: 1, + onlyinclude: 1, + noinclude: 1, + include: 1, + comment: 1, + ext: 1, + arg: 2, + 'magic-word': 2, + template: 2, + heading: 2, + html: 3, + table: 4, + hr: 5, + 'double-underscore': 5, + link: 6, + category: 6, + file: 6, + quote: 7, + 'ext-link': 8, + 'free-ext-link': 9, + 'magic-link': 9, + list: 10, + dd: 10, + converter: 11, +}; + +export type Stage = keyof typeof stages; + export const rules = [ 'bold-header', 'format-leakage', @@ -208,5 +237,5 @@ export interface Parser { * @param include 是否嵌入 * @param maxStage 最大解析层级 */ - parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): Token; + parse(wikitext: string, include?: boolean, maxStage?: number | Stage | Stage[], config?: Config): Token; } From a552d73d7503fcb94996eb51e04f474551d78603 Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:37:31 +0800 Subject: [PATCH 5/9] feat(Parser): infer `maxStage` from token type(s) --- index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 6a108bbc..1421f1a0 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,7 @@ /* eslint n/exports-style: 0 */ import * as fs from 'fs'; import * as path from 'path'; -import {rules} from './base'; +import {rules, stages} from './base'; import {Shadow} from './util/debug'; import { MAX_STAGE, @@ -12,7 +12,7 @@ import { classes, } from './util/constants'; import {tidy} from './util/string'; -import type {Config, LintError, TokenTypes, Parser as ParserBase} from './base'; +import type {Config, LintError, TokenTypes, Parser as ParserBase, Stage} from './base'; import type {Title} from './lib/title'; import type {Token} from './internal'; @@ -68,7 +68,7 @@ declare interface Parser extends ParserBase { selfLink?: boolean, // eslint-disable-line @typescript-eslint/unified-signatures ): Title; - parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): Token; + parse(wikitext: string, include?: boolean, maxStage?: number | Stage | Stage[], config?: Config): Token; /* NOT FOR BROWSER */ @@ -270,6 +270,10 @@ const Parser: Parser = { // eslint-disable-line @typescript-eslint/no-redeclare /** @implements */ parse(wikitext, include, maxStage = MAX_STAGE, config = Parser.getConfig()) { wikitext = tidy(wikitext); + if (typeof maxStage !== 'number') { + const types = Array.isArray(maxStage) ? maxStage : [maxStage]; + maxStage = Math.max(...types.map(t => stages[t] ?? MAX_STAGE)); + } const {Token}: typeof import('./src/index') = require('./src/index'); const root = Shadow.run(() => { const token = new Token(wikitext, config); From f05791012014d260a0cd4fb566e926cdc9825b3c Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:38:55 +0800 Subject: [PATCH 6/9] fix(base): remove prototype of `stages` --- base.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/base.ts b/base.ts index c8572f5f..82bdfba2 100644 --- a/base.ts +++ b/base.ts @@ -119,6 +119,7 @@ export const stages = { dd: 10, converter: 11, }; +Object.setPrototypeOf(stages, null); export type Stage = keyof typeof stages; From f45525c27ab5e73908ce89b71a74e6cf79cb8cee Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:48:28 +0800 Subject: [PATCH 7/9] revert(base): performance concern https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf --- base.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/base.ts b/base.ts index 82bdfba2..c8572f5f 100644 --- a/base.ts +++ b/base.ts @@ -119,7 +119,6 @@ export const stages = { dd: 10, converter: 11, }; -Object.setPrototypeOf(stages, null); export type Stage = keyof typeof stages; From 426029429e35d4955e67704375e29bd96699612f Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:03:31 +0800 Subject: [PATCH 8/9] revert(base): performance burden should be minimal --- base.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/base.ts b/base.ts index c8572f5f..82bdfba2 100644 --- a/base.ts +++ b/base.ts @@ -119,6 +119,7 @@ export const stages = { dd: 10, converter: 11, }; +Object.setPrototypeOf(stages, null); export type Stage = keyof typeof stages; From 2bd65fcf08d821bb217c9a4099877ce8a5069986 Mon Sep 17 00:00:00 2001 From: Bhsd <55071315+bhsd-harry@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:48:09 +0800 Subject: [PATCH 9/9] chore(tsconfig): update to ES2019 --- extensions/tsconfig.json | 2 +- index.ts | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions/tsconfig.json b/extensions/tsconfig.json index b899ecdd..65525036 100644 --- a/extensions/tsconfig.json +++ b/extensions/tsconfig.json @@ -4,7 +4,7 @@ "*.ts" ], "compilerOptions": { - "target": "es2018", + "target": "es2019", "outDir": "dist", "skipLibCheck": true } diff --git a/index.ts b/index.ts index 1421f1a0..4f75fe96 100644 --- a/index.ts +++ b/index.ts @@ -272,7 +272,7 @@ const Parser: Parser = { // eslint-disable-line @typescript-eslint/no-redeclare wikitext = tidy(wikitext); if (typeof maxStage !== 'number') { const types = Array.isArray(maxStage) ? maxStage : [maxStage]; - maxStage = Math.max(...types.map(t => stages[t] ?? MAX_STAGE)); + maxStage = Math.max(...types.map(t => stages[t] || MAX_STAGE)); } const {Token}: typeof import('./src/index') = require('./src/index'); const root = Shadow.run(() => { diff --git a/package.json b/package.json index 4d56281c..d61c9fd3 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "@codemirror/lint": "^6.8.0", "@types/node": "^20.11.6", "codejar-async": "^4.2.3", - "monaco-editor": "^0.51.0", + "monaco-editor": "^0.52.0", "v8r": "^3.0.0" }, "engines": {