Skip to content

Commit

Permalink
Merge branch 'noprint' into lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Oct 2, 2024
2 parents 7573340 + ca0b403 commit 9020251
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
32 changes: 31 additions & 1 deletion base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ 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,
};
Object.setPrototypeOf(stages, null);

export type Stage = keyof typeof stages;

export const rules = [
'bold-header',
'format-leakage',
Expand Down Expand Up @@ -187,5 +217,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;
}
10 changes: 7 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* 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,
BuildMethod,
} 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';

Expand Down Expand Up @@ -44,7 +44,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;
}

/**
Expand Down Expand Up @@ -133,6 +133,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);
Expand Down
3 changes: 1 addition & 2 deletions lib/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ 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);
title = rawurldecode(title);
this.encoded = encoded;
} catch {}
}
title = title.replace(/[_ ]+/gu, ' ').trim();
title = decodeHtml(title).replace(/[_ ]+/gu, ' ').trim();
if (subpage) {
this.#ns = 0;
} else {
Expand Down

0 comments on commit 9020251

Please sign in to comment.