-
Notifications
You must be signed in to change notification settings - Fork 2
Parser (EN)
Table of Contents
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
✅ 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);
✅ 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注释');
Expand
version added: 1.10.0
type: string
The absolute path or relative path to the directory of templates used by Token.prototype.expand
. In a Windows file system, the colon (:
) in the page title needs to be replaced with a modifier letter colon (꞉
).
Expand
type: Map<string, string>
Used to define unidirectional language variant conversion.
// conversionTable (main)
Parser.conversionTable.set('頁', '页');
assert.strictEqual(Parser.normalizeTitle('首頁').title, '首页');
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', 'project : 首页#EN');
title = Parser.normalizeTitle('main page');
assert.strictEqual(title.title, 'Project:首页');
assert.strictEqual(title.toString(), 'Project:首页#EN');
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.
Expand
type: boolean
Whether to output warning messages, defaults to true
.
Expand
type: boolean
Whether to output debugging messages, defaults to false
.
✅ 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.equal(title, 'Template:Lang#參考資料');
title = Parser.normalizeTitle('File:<');
assert.deepStrictEqual({...title}, {
interwiki: '',
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.equal(title, 'zhwp:Template:Lang#參考資料');
✅ Expand
param: string
Wikitext
param: boolean
Whether to be transcluded
param: number | string | string[]
Maximum stage of parsing
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');
wikitext = '{{a}} [[b]] ';
assert.equal(Parser.parse(wikitext, false, 'template').lastChild, ' [[b]] ');
✅ Expand
returns: Config
Get the parsing configurations.
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 = 'zhwiki';
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}),
}),
);
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext