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

Table of Contents

Other Languages

Introduction

Header of a section. 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.

Properties

level

✅ Expand

type: number
The level of the header, read-only.

// level
var {firstChild} = Parser.parse("==a==");
assert.equal(firstChild, "==a==");
assert.strictEqual(firstChild.level, 2);

innerText

Expand

type: string
The text content of the header.

// innerText (main)
var {firstChild} = Parser.parse("==a==");
assert.equal(firstChild, "==a==");
assert.strictEqual(firstChild.innerText, "a");
firstChild.innerText = "b";
assert.equal(firstChild, "==b==");

id

Expand

version added: 1.12.4

type: string
The id attribute of the header, read-only.

// id (main)
var {firstChild} = Parser.parse("==a==");
assert.equal(firstChild, "==a==");
assert.strictEqual(firstChild.id, "a");

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var [h2, h1, h3] = Parser.parse(`<p
=a''==
>
= b =
===== c'' ===`).querySelectorAll("heading");
assert.equal(h2, "=a''==");
assert.equal(h1, "= b =");
assert.equal(h3, "===== c'' ===");
assert.deepStrictEqual(h2.lint(), [
	{
		rule: "h1",
		severity: "warning",
		message: "<h1>",
		startLine: 1,
		startCol: 1,
		startIndex: 4,
		endLine: 1,
		endCol: 5,
		endIndex: 8,
	},
	{
		rule: "unbalanced-header",
		severity: "error",
		message: 'unbalanced "=" in a section header',
		startLine: 1,
		startCol: 1,
		startIndex: 4,
		endLine: 1,
		endCol: 5,
		endIndex: 8,
		suggestions: [
			{
				desc: "h1",
				range: [7, 8],
				text: "",
			},
			{
				desc: "h2",
				range: [4, 4],
				text: "=",
			},
		],
	},
	{
		rule: "parsing-order",
		severity: "error",
		message: "section header in HTML tag attributes",
		startLine: 1,
		startCol: 0,
		startIndex: 3,
		endLine: 1,
		endCol: 6,
		endIndex: 9,
	},
	{
		rule: "format-leakage",
		severity: "warning",
		message: "unbalanced italic apostrophes in a section header",
		startLine: 1,
		startCol: 2,
		startIndex: 5,
		endLine: 1,
		endCol: 4,
		endIndex: 7,
		suggestions: [
			{
				desc: "close",
				range: [8, 8],
				text: "''",
			},
		],
	},
]);
assert.deepStrictEqual(h1.lint(), [
	{
		rule: "h1",
		severity: "warning",
		message: "<h1>",
		startLine: 3,
		startCol: 1,
		startIndex: 13,
		endLine: 3,
		endCol: 4,
		endIndex: 16,
		suggestions: [
			{
				desc: "h2",
				range: [13, 16],
				text: "= b =",
			},
		],
	},
]);
assert.deepStrictEqual(h3.lint(), [
	{
		rule: "unbalanced-header",
		severity: "error",
		message: 'unbalanced "=" in a section header',
		startLine: 4,
		startCol: 3,
		startIndex: 21,
		endLine: 4,
		endCol: 10,
		endIndex: 28,
		suggestions: [
			{
				desc: "h3",
				range: [21, 23],
				text: "",
			},
			{
				desc: "h5",
				range: [28, 28],
				text: "==",
			},
		],
	},
	{
		rule: "format-leakage",
		severity: "warning",
		message: "unbalanced italic apostrophes in a section header",
		startLine: 4,
		startCol: 7,
		startIndex: 25,
		endLine: 4,
		endCol: 9,
		endIndex: 27,
		fix: {
			desc: "remove",
			range: [25, 27],
			text: "",
		},
	},
]);

json

🌐 Expand

Save the syntax tree as JSON.

// json (print)
var {lastChild} = Parser.parse("\n=a=");
assert.deepStrictEqual(lastChild.json(), {
	range: [1, 4],
	type: "heading",
	level: 1,
	childNodes: [
		{
			range: [2, 3],
			type: "heading-title",
			childNodes: [
				{
					range: [2, 3],
					data: "a",
				},
			],
		},
		{
			range: [4, 4],
			type: "heading-trail",
			childNodes: [
				{
					range: [4, 4],
					data: "",
				},
			],
		},
	],
});

cloneNode

Expand

returns: this
Deep clone the node.

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

section

Expand

version added: 1.30.0

returns: AstRange
Get the section led by this heading.

// section (main)
var {firstChild, lastElementChild} = Parser.parse("==a==\nA\n==b==\nB");
assert.equal(firstChild, "==a==");
assert.equal(lastElementChild, "==b==");
assert.equal(firstChild.section(), "==a==\nA\n");
assert.equal(lastElementChild.section(), "==b==\nB");

setLevel

Expand

param: number Level of the header
Set the level of the header.

// setLevel (main)
var {firstChild} = Parser.parse("==a==");
assert.equal(firstChild, "==a==");
firstChild.setLevel(3);
assert.equal(firstChild, "===a===");

toHtml

Expand

version added: 1.10.0

returns: string
Convert to HTML.

// toHtml (main)
var {firstChild} = Parser.parse("= '' a  b '' =");
assert.strictEqual(
	firstChild.toHtml(),
	'<div class="mw-heading mw-heading1"><h1 id="a_b"><i> a  b </i></h1></div>',
);
⚠️ **GitHub.com Fallback** ⚠️