Skip to content

Examples (tag) (EN)

Bhsd edited this page Apr 17, 2026 · 4 revisions

Other Languages

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
import type {HtmlToken} from 'wikiparser-node';
const content = `Foo

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

=== <strong>Baz</strong> ===`,
	root = Parser.parse(content),
	boldings = root.querySelectorAll<HtmlToken>(
		'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
import type {ExtToken} from 'wikiparser-node';
const content = `<syntaxhighlight lang="moin">
Foo
</syntaxhighlight>`,
	root = Parser.parse(content),
	exts = root.querySelectorAll<ExtToken>('ext#syntaxhighlight[lang=moin]');
for (const ext of exts) {
	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 invalid self-closing tags
import type {HtmlToken} from 'wikiparser-node';
const content = '<div>Foo<div/>',
	root = Parser.parse(content);
for (const html of root.querySelectorAll<HtmlToken>('html[selfClosing]')) {
	try {
		// May throw if it cannot be automatically fixed
		html.fix();
	} catch {}
}
assert.equal(
	root,
	'<div>Foo</div>',
);

Clone this wiki locally