Examples (internal link) (EN) - bhsd-harry/wikiparser-node GitHub Wiki

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>`,
);
⚠️ **GitHub.com Fallback** ⚠️