Skip to content

wikiparse (EN)

Bhsd edited this page Feb 14, 2025 · 12 revisions
Table of Contents

Other Languages

Introduction

This is a global variable added by the browser extension, which can perform various operations such as highlighting static code or text boxes, reporting potential grammar errors, etc.

Properties

version

Expand

type: string
The version of WikiParser-Node.

codejar

Expand

Requires CodeJar extension.

type: Promise<(textbox: HTMLTextAreaElement, include?: boolean, linenums?: boolean) => CodeJar>
Highlight text box.

(await wikiparse.codejar)(document.getElementsByTagName('textarea')[0]);

Methods

setI18N

Expand

param: Record<string, string>
Set the language. The default language is English, and other preset languages include Simplified Chinese and Traditional Chinese.

// setI18N (browser)
(async () => {
	const wikitext = '{{';
	assert.strictEqual(
		(await wikiparse.lint(wikitext))[0].message,
		'lonely "{"',
	);
	const i18n = await (await fetch(
		'https://cdn.jsdelivr.net/npm/wikiparser-node/i18n/zh-hans.json',
	)).json();
	wikiparse.setI18N(i18n);
	assert.strictEqual(
		(await wikiparse.lint(wikitext))[0].message,
		'孤立的"{"',
	);
})();

setConfig

Expand

param: Config
Set the parsing configuration. Preset configurations include English Wikipedia (enwiki), Chinese Wikipedia (zhwiki), Moegirlpedia (moegirl) and LLWiki (llwiki). To customize the parsing configuration of a MediaWiki site, please refer to .schema.json for the relevant content.

const config = await (await fetch(
	'https://cdn.jsdelivr.net/npm/wikiparser-node/config/zhwiki.json',
)).json();
wikiparse.setConfig(config);

getConfig

Expand

returns: Promise<Config>
Get the parsing configuration.

const config = await wikiparse.getConfig();

lint

Expand

param: string Wikitext
param: boolean Whether to be transcluded, default to false
returns: Promise<LintError[]>
Report potential grammar errors.

// lint (browser)
(async () => {
	assert.deepStrictEqual(
		await wikiparse.lint('[[a]'),
		[
			{
				rule: 'lonely-bracket',
				message: 'lonely "["',
				severity: 'error',
				startIndex: 0,
				startLine: 0,
				startCol: 0,
				endIndex: 2,
				endLine: 0,
				endCol: 2,
			},
		],
	);
})();

json

Expand

param: string Wikitext
param: boolean Whether to be transcluded, default to false
returns: Promise<AST>
Produce an abstract syntax tree in JSON format.

// json (browser)
(async () => {
	assert.deepStrictEqual(
		await wikiparse.json('[[a]]'),
		{
			range: [0, 5],
			type: 'root',
			childNodes: [
				{
					range: [0, 5],
					type: 'link',
					name: 'A',
					childNodes: [
						{
							range: [2, 3],
							type: 'link-target',
							childNodes: [
								{
									range: [2, 3],
									data: 'a',
								},
							],
						},
					],
				},
			],
		},
	);
})();

lineNumbers

Expand

param: HTMLElement The HTML element for the static code
param: number The starting value of line numbers, optional
param: number The top padding, optional
returns: Promise<void>
Add line numbers to the code block.

wikiparse.lineNumbers(document.getElementsByTagName('pre')[0]);

highlight

Expand

Requires Highlight extension.

param: HTMLElement The HTML element for the static code
param: boolean Whether to be transcluded, default to false
param: boolean Whether to add line numbers, default to false
param: number The starting value of line numbers, optional
returns: Promise<void>
Highlight static code.

wikiparse.highlight(document.getElementsByTagName('pre')[0], false, true);

edit

Expand

(Deprecated) Requires Editor extension.

param: HTMLTextAreaElement Text box
param: boolean Whether to be transcluded, default to false
Highlight text box.

wikiparse.edit(document.getElementsByTagName('textarea')[0]);

Constructors

Linter

See Linter.

LanguageService

See LanguageService.

Clone this wiki locally