Skip to content

Examples (tag)

Bhsd edited this page Apr 15, 2026 · 5 revisions

Removing bolding from section headers

Expand

You can remove bolding from section headers in the content of a page. This example demonstrates the use of AstNode.prototype.remove.

// Removing bolding from section headers (main)
// The content of a page
var content = `Foo

== <b>Bar</b> ==

=== <strong>Baz</strong> ===`,
	root = Parser.parse(content),
	boldings = root.querySelectorAll('heading-title > html:is(#b, #strong)');
for (const html of boldings) {
	html.remove();
}
assert.equal(
	root,
	`Foo

== Bar ==

=== Baz ===`,
);

Replacing the syntaxhighlight language

Expand

You can replace the lang attribute of <syntaxhighlight> tags from moin to wikitext in the content of a page. This example demonstrates the use of ExtToken.

// Replacing the syntaxhighlight language (main)
// The content of a page containing `<syntaxhighlight>` tags
var content = `<syntaxhighlight lang="moin">
Foo
</syntaxhighlight>`,
	root = Parser.parse(content);
for (const ext of root.querySelectorAll('ext#syntaxhighlight[lang=moin]')) {
	ext.setAttr('lang', 'wikitext');
}
assert.equal(
	root,
	`<syntaxhighlight lang="wikitext">
Foo
</syntaxhighlight>`,
);

Fixing invalid self-closing tags

Expand

You can easily fix invalid self-closing tags in the content of a page using HtmlToken.prototype.fix.

// Replacing the syntaxhighlight language (main)
// The content of a page containing `<syntaxhighlight>` tags
var content = '<div>Foo<div/>',
	root = Parser.parse(content);
for (const html of root.querySelectorAll('html[selfClosing]')) {
	try {
		// May throw if it cannot be automatically fixed
		html.fix();
	} catch {}
}
assert.equal(
	root,
	'<div>Foo</div>',
);

Clone this wiki locally