-
Notifications
You must be signed in to change notification settings - Fork 2
LinkBaseToken (EN)
Table of Contents
The parent class of CategoryToken, FileToken, LinkToken and RedirectTargetToken. This class 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
var {firstChild: {link}} = Parser.parse('[[a]]');
assert.strictEqual(link.title, 'A');
// link (main)
var {firstChild} = Parser.parse('[[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 (print)
var {firstChild} = Parser.parse('[[a#%7B%7D]]');
assert.strictEqual(firstChild.fragment, '{}');
// fragment (main)
var {firstChild} = Parser.parse('[[a#%7B%7D]]');
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';
Parser.getConfig();
Parser.config.interwiki.push('zhwp', 'enwp');
({firstChild} = Parser.parse('[[zhwp:a#b]]'));
assert.strictEqual(firstChild.interwiki, 'zhwp');
firstChild.interwiki = 'enwp';
assert.equal(firstChild, '[[enwp:A#b]]');
✅ 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,
suggestions: [
{
desc: 'decode',
range: [2, 24],
text: 'category:a{{lj|a}}#b',
},
],
},
{
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: '',
desc: 'remove',
},
},
]);
🌐 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
Save the syntax tree as JSON.
// json
var {lastChild} = Parser.parse(' [[a#b]]');
assert.deepStrictEqual(lastChild.json(), {
range: [1, 8],
type: 'link',
name: 'A',
fragment: 'b',
childNodes: [
{
range: [3, 6],
type: 'link-target',
childNodes: [
{
range: [3, 6],
data: 'a#b',
},
],
},
],
});
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';
Parser.getConfig();
Parser.config.interwiki.push('mw');
({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>',
);
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext