LinkBaseToken (EN) - bhsd-harry/wikiparser-node GitHub Wiki
Table of Contents
The parent class of CategoryToken, FileToken, LinkToken and RedirectTargetToken. LinkBaseToken inherits all the properties and methods of the Token class which are not repeated here.
✅ Available in the Mini and Browser versions.
🌐 Available in the Browser version.
✅ Expand
type: string
Normalized target page name, read-only.
// name
var {firstChild} = Parser.parse('[[category:a b]]');
assert.strictEqual(firstChild.name, 'Category:A_b');
Expand
type: Title
Full link target.
// link (main)
var {firstChild} = Parser.parse('[[a]]');
assert.equal(firstChild.link, 'A');
firstChild.link = 'file:b';
assert.equal(firstChild, '[[:file:b]]'); // auto add ':' at the beginning
assert.strictEqual(firstChild.name, 'File:B');
({firstChild} = Parser.parse('[[category:c]]'));
firstChild.link = 'category:d';
assert.equal(firstChild, '[[category:d]]');
assert.strictEqual(firstChild.name, 'Category:D');
Expand
type: string
// fragment (main)
var {firstChild} = Parser.parse('[[a#%7B%7D]]');
assert.strictEqual(firstChild.fragment, '{}');
firstChild.fragment = undefined;
assert.equal(firstChild, '[[A]]'); // auto normalize the target page name
firstChild.fragment = 'b';
assert.equal(firstChild, '[[A#b]]');
Expand
type: string
Interwiki prefix.
// interwiki (main)
var firstChild;
// Interwiki prefixes depend on the specific settings of each MediaWiki site
Parser.config = 'moegirl';
({firstChild} = Parser.parse('[[zhwp:a]]'));
assert.strictEqual(firstChild.interwiki, 'zhwp');
firstChild.interwiki = 'enwp';
assert.equal(firstChild, '[[enwp:A]]');
✅ Expand
returns: LintError[]
Report potential grammar errors.
// lint
var {firstChild} = Parser.parse('[[category:%61{{lj|a}}#b|c|]]');
assert.deepStrictEqual(firstChild.lint(), [
{
rule: 'unknown-page',
severity: 'warning',
message: 'template in an internal link target',
startLine: 0,
startCol: 2,
startIndex: 2,
endLine: 0,
endCol: 24,
endIndex: 24,
},
{
rule: 'url-encoding',
severity: 'error',
message: 'unnecessary URL encoding in an internal link',
startLine: 0,
startCol: 2,
startIndex: 2,
endLine: 0,
endCol: 24,
endIndex: 24,
},
{
rule: 'pipe-like',
severity: 'warning',
message: 'additional "|" in the link text',
startLine: 0,
startCol: 25,
startIndex: 25,
endLine: 0,
endCol: 27,
endIndex: 27,
suggestions: [
{
desc: 'escape',
range: [25, 27],
text: 'c|',
},
],
},
{
rule: 'no-ignored',
severity: 'error',
message: 'useless fragment',
startLine: 0,
startCol: 2,
startIndex: 2,
endLine: 0,
endCol: 24,
endIndex: 24,
fix: {
range: [22, 24],
text: '',
},
},
]);
🌐 Expand
returns: string
Output in HTML format.
// print
var {firstChild} = Parser.parse('[[a#b|b]]');
assert.equal(
firstChild.print(),
`<span class="wpb-link">[[<span class="wpb-link-target">a#b</span>|<span class="wpb-link-text">b</span>]]</span>`,
);
Expand
param: string
Set the target page name.
// setTarget (main)
var {firstChild} = Parser.parse('[[a]]');
firstChild.setTarget('b');
assert.equal(firstChild, '[[b]]');
firstChild.setTarget('category:c');
assert.equal(firstChild, '[[:category:c]]'); // auto add ':' at the beginning
Expand
param: string
Set fragment.
// setFragment (main)
var {firstChild} = Parser.parse('[[a#a]]');
firstChild.setFragment();
// this method will normalize the target page name
assert.equal(firstChild, '[[A]]');
firstChild.setFragment('b');
assert.equal(firstChild, '[[A#b]]');
Expand
param: string
Set the displayed link text.
// setLinkText (main)
var {firstChild} = Parser.parse('[[a|a]]');
firstChild.setLinkText();
assert.equal(firstChild, '[[a]]');
firstChild.setLinkText('{{b}}');
assert.equal(firstChild, '[[a|{{b}}]]');
assert(firstChild.querySelector('template#Template:B'));
firstChild.setLinkText('c');
assert.equal(firstChild, '[[a|c]]');
Expand
version added: 1.10.0
returns: string
Convert to HTML.
// toHtml (main)
var firstChild, lastChild;
Parser.config = 'enwiki';
({firstChild, lastChild} = Parser.parse([[mw:"?| c ]][[ #< ]]'));
assert.strictEqual(
firstChild.toHtml(),
'<a class="extiw" href="/wiki/mw%3A%22%3F" title="mw:"?"> c </a>',
);
assert.strictEqual(
lastChild.toHtml(),
'<a href="#%3C">#< </a>',
);