MagicLinkToken (EN) - bhsd-harry/wikiparser-node GitHub Wiki

Table of Contents

Other Languages

Introduction

Free external link. This class inherits all the properties and methods of the Token class which are not repeated here.

✅ 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. Read-only in the Mini and Browser versions.

// 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: "encode",
				range: [8, 9],
				text: "%E3%80%82",
			},
		],
	},
]);

getUrl

✅ Expand

returns: URL
Get the URL object.

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

cloneNode

Expand

returns: this
Deep clone the node.

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

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.ok(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 class="external" rel="nofollow" href="https://pubmed.ncbi.nlm.nih.gov/1">PMID 1</a>`,
);
({firstChild} = Parser.parse("RFC 1"));
assert.strictEqual(
	firstChild.toHtml(),
	`<a class="external" rel="nofollow" href="https://datatracker.ietf.org/doc/html/rfc1">RFC 1</a>`,
);
⚠️ **GitHub.com Fallback** ⚠️