Parser (EN) - bhsd-harry/wikiparser-node GitHub Wiki

Table of Contents

Other Languages

Introduction

The main entry point of the program is an object called Parser. Typically, the first step in all operations is to generate a complete syntax tree via the Parser.parse method, then query and modify the syntax tree to output the wikitext.

✅ Available in the Mini and Browser versions.

const Parser = require('wikiparser-node'); // CommonJS

or

import Parser from 'wikiparser-node'; // ES module

or

import Parser = require('wikiparser-node'); // TypeScript

Properties

config

✅ Expand

type: Config | string
The absolute path or relative path to the parsing configurations, or a complete configuration object. The default 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.

// config
var config;
// Use the default configuration of Chinese Wikipedia
Parser.config = 'zhwiki';
config = Parser.getConfig();
// Equivalent to the above using relative paths
Parser.config = './config/zhwiki';
assert.deepStrictEqual(Parser.getConfig(), config);

i18n

✅ Expand

type: string
The absolute path or relative path to the language file used to specify the linting message. The default language is English, and other preset languages include Simplified Chinese and Traditional Chinese.

// i18n
var message;
Parser.i18n = 'zh-hans';
[{message}] = Parser.parse('<!--').lint();
assert.strictEqual(message, '未闭合的HTML注释');
Parser.i18n = './i18n/zh-hans'; // Equivalent to the above using relative paths
[{message}] = Parser.parse('<!--').lint();
assert.strictEqual(message, '未闭合的HTML注释');

rules

✅ Expand

version added: 1.5.1

type: LintError.Rule[]
All linting rules.

templateDir

Expand

version added: 1.10.0

type: string
The absolute path or relative path to the directory of templates used by Token.prototype.expand.

conversionTable

Expand

type: Map<string, string>
Used to define unidirectional language variant conversion.

// conversionTable (main)
Parser.conversionTable.set('頁', '页');
assert.strictEqual(Parser.normalizeTitle('首頁').title, '首页');

redirects

Expand

type: Map<string, string>
Used to define redirects. Note that the page name must be capitalized and spaces must be replaced with underscores.

// redirects (main)
var title;
Parser.redirects.set('Main_page', '首页#EN');
title = Parser.normalizeTitle('main page');
assert.strictEqual(title.title, '首页');
assert.strictEqual(title.toString(), '首页#EN');

viewOnly

Expand

version added: 1.9.0

type: boolean
Whether to parse the content without changing, defaults to false. When set to true, the parser's performance will be improved.

warning

Expand

type: boolean
Whether to output warning messages, defaults to true.

debugging

Expand

type: boolean
Whether to output debugging messages, defaults to false.

Methods

normalizeTitle

✅ Expand

param: string Title (with or without namespace prefix)
param: number Namespace, default to 0
returns: Title
Normalize the page title. Note that when using the default parsing configurations, no interwiki information will be included.

// normalizeTitle
var title = Parser.normalizeTitle('lang#參考資料', 10);
assert.deepStrictEqual({...title}, {
	ns: 10,
	interwiki: '',
	fragment: '參考資料',
	valid: true,
});
title = Parser.normalizeTitle('File:<');
assert.deepStrictEqual({...title}, {
	ns: 6,
	interwiki: '',
	fragment: undefined,
	valid: false,
});
// normalizeTitle (main)
var title;
// Interwiki prefixes depend on the specific settings of each MediaWiki site
Parser.config = 'moegirl';
title = Parser.normalizeTitle('zhwp:模板:lang#參考資料');
assert.deepStrictEqual({...title}, {
	ns: 10,
	interwiki: 'zhwp',
	fragment: '參考資料',
	valid: true,
});
assert.equal(title, 'zhwp:Template:Lang#參考資料');

parse

✅ Expand

param: string Wikitext
param: boolean Whether to be transcluded
returns: Token
Parse wikitext.

// parse
var wikitext = '<includeonly>i</includeonly><noinclude>n</noinclude>';
assert.strictEqual(Parser.parse(wikitext).text(), 'n');
assert.strictEqual(Parser.parse(wikitext, true).text(), 'i');

getConfig

✅ Expand

returns: Config
Get the parsing configurations.

isInterwiki

Expand

param: string Interwiki link
returns: RegExpExecArray | null
Determine whether it is an interwiki link. Note that when using the default parsing configurations, no interwiki information will be included.

// isInterwiki (main)
// Interwiki prefixes depend on the specific settings of each MediaWiki site
Parser.config = 'moegirl';
assert.deepStrictEqual(
	Parser.isInterwiki('mw :Main Page'),
	Object.assign(['mw :', 'mw'], {
		index: 0,
		input: 'mw :Main Page',
		groups: undefined,
		indices: Object.assign([[0, 4], [0, 2]], {groups: undefined}),
	}),
);
⚠️ **GitHub.com Fallback** ⚠️