wikiparser-template
v1.36.0
Published
A light-weight template parser for MediaWiki markup
Readme
WikiParser-Template
WikiParser-Template is a standalone Wikitext template parser for the Node.js and browser environments. It is a highly simplified version of WikiParser-Node, focusing solely on parsing templates and their parameters.
Installation
You can install this CommonJS package via npm:
npm i wikiparser-templateUsage
import Parser from 'wikiparser-template';API
The full WikiParser-Node API is documented in the Wiki, but WikiParser-Template only supports a small subset of it. The most commonly used methods are (in TypeScript syntax):
import Parser from 'wikiparser-template';
import type {Token, TranscludeToken, ParameterToken} from 'wikiparser-template';
Parser.config = {
ext: [ // You need to specify available extension tags
'pre',
'nowiki',
'gallery',
'indicator',
],
};
// Parse the wikitext and return the root node of the AST
const root: Token = Parser.parse(myWikitext);
// Get the first template node
const template: TranscludeToken | undefined = root.querySelector<TranscludeToken>('template');
// Get all template nodes
const templates: TranscludeToken[] = root.querySelectorAll<TranscludeToken>('template');
// Get the first template node by its name
const myTemplate: TranscludeToken = root.querySelector<TranscludeToken>('template#Template:My_Template')!;
// Template name
const templateName = myTemplate.name; // 'Template:My_Template'
// Get all template nodes by their name
const myTemplates: TranscludeToken[] = root.querySelectorAll<TranscludeToken>('template#Template:My_Template_1, template#Template:My_Template_2');
// Get the parameter `1` of the template
const param_1: ParameterToken = myTemplate.getArg(1)!;
// Get the parameter `a` of the template
const param_a: ParameterToken = myTemplate.getArg('a')!;
// Parameter name
const name_1 = param_1.name; // '1'
const name_a = param_a.name; // 'a'
// Whether the parameter is anonymous
const isAnon_1 = param_1.anon; // true
const isAnon_a = param_a.anon; // false
// Get the value of the parameter `1`
let value_1: string = param_1.getValue();
// Get the value of the parameter `1` from the template node
value_1 = myTemplate.getValue(1)!;
// Append new anonymouse parameters to the template
myTemplate.append('anonymous parameter', 'another anonymous parameter');
// Set the value of the parameter `1`
param_1.setValue('new value');
// Set the value of the parameter `1` from the template node
myTemplate.setValue(1, 'new value');
// Insert new anonymous parameters after the parameter `1`
param_1.after('anonymous parameter after 1');
// Insert new named parameters before the parameter `a`
param_a.before('another named parameter before a');
// Remove the parameter `1`
param_1.remove();List of Supported Properties and Methods
Title
AstNode
- AstNode.childNodes
- AstNode.firstChild
- AstNode.lastChild
- AstNode.nextSibling
- AstNode.parentNode
- AstNode.previousSibling
- AstNode.after
- AstNode.before
- AstNode.remove
AstText
AstElement
- AstElement.length
- AstElement.append
- AstElement.insertAt
- AstElement.querySelector
- AstElement.querySelectorAll
- AstElement.removeAt
- AstElement.replaceChildren
- AstElement.setText
- AstElement.text
Token
NowikiBaseToken
CommentToken
TagPairToken
HeadingToken
TranscludeToken
- TranscludeToken.modifier
- TranscludeToken.name
- TranscludeToken.getAllArgs
- TranscludeToken.getAnonArgs
- TranscludeToken.getArg
- TranscludeToken.getArgs
- TranscludeToken.getValue
- TranscludeToken.isTemplate
- TranscludeToken.setModifier
- TranscludeToken.setValue
