Skip to content

LinkToken (EN)

bhsd edited this page Jul 11, 2024 · 16 revisions
Table of Contents

Other Languages

Introduction

Internal link, including interwiki link. LinkToken inherits all the properties and methods of the LinkBaseToken class which are not repeated here.

✅ Available in the Mini and Browser versions.

Properties

type

✅ Expand

type: 'link'

// type
var {firstChild} = Parser.parse('[[:category:a]]');
assert.strictEqual(firstChild.type, 'link');

innerText

Expand

type: string
Displayed link text.

// innerText (main)
var {firstChild, lastChild} = Parser.parse([[ : %3f ]][[b| c ]]');
assert.equal(firstChild, '[[ : %3f ]]');
assert.equal(lastChild, '[[b| c ]]');
assert.strictEqual(firstChild.innerText, ' ? ');
assert.strictEqual(lastChild.innerText, ' c ');
firstChild.innerText = 'd';
assert.equal(firstChild, '[[ : %3f |d]]');

selfLink

Expand

type: boolean
Whether the link is a self-link.

// selfLink (main)
var {firstChild, lastChild} = Parser.parse([[a#a]][[##]]');
assert.equal(firstChild, '[[a#a]]');
assert.equal(lastChild, '[[##]]');
assert(!firstChild.selfLink);
assert(lastChild.selfLink);
firstChild.selfLink = true;
assert.equal(firstChild, '[[#a]]');

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var {firstChild: {lastChild}} = Parser.parse('[//a [[b]]]');
assert.equal(lastChild, '[[b]]');
assert.deepStrictEqual(lastChild.lint(), [
	{
		rule: 'nested-link',
		severity: 'error',
		message: 'internal link in an external link',
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 10,
		endIndex: 10,
	},
]);

cloneNode

Expand

returns: this
Deep clone the node.

// cloneNode (main)
var {firstChild} = Parser.parse('[[a#b|c]]');
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);

setLangLink

Expand

param: string
param: string
Set inter-language link.

// setLangLink (main)
var firstChild;
// Interwiki prefixes depend on the specific settings of each MediaWiki site
Parser.config = 'moegirl';
({firstChild} = Parser.parse('[[a]]'));
firstChild.setLangLink('en', 'b#c');
assert.equal(firstChild, '[[en:b#c]]');

asSelfLink

Expand

param: string
Convert the link to a self-link.

// asSelfLink (main)
var {firstChild} = Parser.parse('[[a#b]]');
firstChild.asSelfLink();
assert.equal(firstChild, '[[#b]]');
firstChild.asSelfLink('[');
assert.equal(firstChild, '[[#%5B]]');

pipeTrick

Expand

Automatically generate the link text after the pipe.

// pipeTrick (main)
var {children: [a, b, c, d]} = Parser.parse(`[[:file: a  (a), a]]
[[:user: b  (b)]]
[[:talk: c , c]]
[[:help: d  (d) ]]`);
assert.equal(a, '[[:file: a  (a), a]]');
assert.equal(b, '[[:user: b  (b)]]');
assert.equal(c, '[[:talk: c , c]]');
assert.equal(d, '[[:help: d  (d) ]]');
a.pipeTrick();
assert.equal(a, '[[:file: a  (a), a| a ]]');
b.pipeTrick();
assert.equal(b, '[[:user: b  (b)| b ]]');
c.pipeTrick();
assert.equal(c, '[[:talk: c , c| c ]]');
d.pipeTrick();
assert.equal(d.toString(), '[[:help: d  (d) | d  (d) ]]');
Clone this wiki locally