Skip to content

MagicLinkToken (EN)

bhsd edited this page Jul 4, 2024 · 15 revisions
Table of Contents

Other Languages

Introduction

Free external link.

All of the following properties and methods are not available in the Mini and Browser versions.

Properties

innerText

✅ Expand

version added: 1.10.0

type: string
Link text, read-only.

// innerText
var {firstChild} = Parser.parse('ftp://a');
assert.strictEqual(firstChild.innerText, 'ftp://a');

link

✅ Expand

type: string
The external link.

// link
var {firstChild} = Parser.parse('ftp://a');
assert.strictEqual(firstChild.link, 'ftp://a');
// link (main)
var {firstChild} = Parser.parse('ftp://a');
firstChild.link = 'https://b';
assert.equal(firstChild, 'https://b');

protocol

Expand

type: string
Protocol of the external link.

// protocol (main)
var {firstChild} = Parser.parse('ftp://a');
assert.strictEqual(firstChild.protocol, 'ftp://');
firstChild.protocol = 'https://';
assert.equal(firstChild, 'https://a');

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var {firstChild} = Parser.parse('http://a。b');
assert.equal(firstChild, 'http://a。b');
assert.deepStrictEqual(firstChild.lint(), [
	{
		rule: 'unterminated-url',
		severity: 'warning',
		message: 'full-width punctuation in URL',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 10,
		endIndex: 10,
		suggestions: [
			{
				desc: 'whitespace',
				range: [8, 8],
				text: ' ',
			},
			{
				desc: 'escape',
				range: [8, 9],
				text: '%E3%80%82',
			},
		],
	},
]);

cloneNode

Expand

returns: this
Deep clone the node.

// cloneNode (main)
var {firstChild} = Parser.parse('http://a');
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);

getUrl

Expand

returns: URL
Get the URL object.

// getUrl (main)
var {firstChild} = Parser.parse('http://a');
assert.deepStrictEqual(firstChild.getUrl(), new URL('http://a/'));

setTarget

Expand

param: string | URL URL, contains the protocol
Set the target of the external link.

// setTarget (main)
var {firstChild} = Parser.parse('http://a');
firstChild.setTarget('https://b');
assert.equal(firstChild, 'https://b');

isParamValue

Expand

returns: boolean
Whether it is a template or magic word parameter.

// isParamValue (main)
var link = Parser.parse('{{a|http://a}}').querySelector('free-ext-link');
assert.equal(link, 'http://a');
assert(link.isParamValue());

escape

Expand

version added: 1.1.4

Escape =.

// escape (main)
var {firstChild} = Parser.parse('http://a/b?c=');
assert.equal(firstChild, 'http://a/b?c=');
firstChild.escape();
assert.equal(firstChild, 'http://a/b?c{{=}}');

toHtml

Expand

version added: 1.10.0

returns: string
Convert to HTML.

// toHtml (main)
var {firstChild} = Parser.parse('ftp://a');
assert.strictEqual(
	firstChild.toHtml(),
	'<a rel="nofollow" class="external free" href="ftp://a/">ftp://a</a>',
);
({firstChild} = Parser.parse('ISBN 1-2-3-4-5-6-7-8-9-0'));
assert.strictEqual(
	firstChild.toHtml(),
	`<a href="/wiki/Special%3ABookSources%2F1234567890">ISBN 1-2-3-4-5-6-7-8-9-0</a>`,
);
({firstChild} = Parser.parse('PMID 1'));
assert.strictEqual(
	firstChild.toHtml(),
	`<a rel="nofollow" class="external" href="https://pubmed.ncbi.nlm.nih.gov/1">PMID 1</a>`,
);
({firstChild} = Parser.parse('RFC 1'));
assert.strictEqual(
	firstChild.toHtml(),
	`<a rel="nofollow" class="external" href="https://tools.ietf.org/html/rfc1">RFC 1</a>`,
);
Clone this wiki locally