Skip to content

Examples (internal link) (EN)

Bhsd edited this page Apr 17, 2026 · 3 revisions

Other Languages

Setting the sort key of a category

Expand

You can set the sort key of a category in the content of a page. This example demonstrates the use of CategoryToken.

// Setting the sort key of a category (main)
// The content of a page containing `Category:Foo`
import type {CategoryToken} from 'wikiparser-node';
const content = `Foo

[[Category:Foo]]`,
	root = Parser.parse(content),
	category = root.querySelector<CategoryToken>('category#Category:Foo');
if (category) {
	category.setSortkey('*');
}
assert.equal(
	root,
	`Foo

[[Category:Foo|*]]`,
);

Removing blank alt attributes from images

Expand

You can removing blank alt attributes from images in the content of a page to improve accessibility. This example demonstrates the use of ImageParameterToken.

// Removing blank alt attributes from images (main)
// The content of a page containing images
import type {ImageParameterToken} from 'wikiparser-node';
const content = `[[File:Foo.jpg|alt=]]

<gallery>
Bar.jpg|alt=
</gallery>`,
	root = Parser.parse(content),
	parameters = root.querySelectorAll<ImageParameterToken>(
		'image-parameter#alt',
	);
for (const parameter of parameters) {
	if (parameter.value === '') {
		parameter.remove();
	}
}
assert.equal(
	root,
	`[[File:Foo.jpg]]

<gallery>
Bar.jpg
</gallery>`,
);

Clone this wiki locally