Parser (EN) - bhsd-harry/wikiparser-node GitHub Wiki
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.
🌐 Available in the Browser version.
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: Map<string, string>
Used to define unidirectional language variant conversion.
// conversionTable (main)
Parser.conversionTable.set('頁', '页');
assert.strictEqual(Parser.normalizeTitle('首頁').title, '首页');
Expand
type: boolean
Whether to output debugging messages, defaults to false
.
✅ 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
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.equal(title, 'Project:首页#EN');
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
version added: 1.10.0
type: Map<string, string>
Instead of Parser.templateDir
, the templates can also be manually added to this map. The key is the title of the template, and the value is the wikitext of the template.
// templates (main)
Parser.templates.set('template:a', '1');
assert.equal(Parser.parse('{{a}}').expand(), '1');
🌐 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
version added: 1.16.1
param: object
The document object
returns: LanguageService
Create a language service task. Note that calling this method will automatically set viewOnly
to true
.
Expand
version added: 1.18.4
param: string
The site nickname
param: string
The script path
returns: Promise<Config>
Fetch the parsing configurations for the specified MediaWiki site with Extension:CodeMirror installed.
✅ 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)
Parser.getConfig();
Parser.config.interwiki = ['mw'];
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}),
}),
);
✅ 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.strictEqual(title.title, 'Template:Lang');
assert.strictEqual(title.fragment, '參考資料');
title = Parser.normalizeTitle('File:<');
assert.ok(!title.valid);
// normalizeTitle (main)
var title;
Parser.getConfig();
Parser.config.interwiki = ['zhwp'];
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. Note that the list elements (ul
, ol
, dl
) will not be fully parsed, which needs Token.prototype.buildLists
to be called.
// 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]] ');
// When there is an unknown stage, the text will be parsed to the last stage
assert.equal(Parser.parse(wikitext, false, ['ext', 'unknown']).lastChild, ' ');
// parse (main)
var wikitext = '*a';
assert.strictEqual(Parser.parse(wikitext).lastChild.type, 'text');
assert.strictEqual(
Parser.parse(wikitext, false, 'list-range').lastChild.type,
'list-range',
);