Skip to content

Examples (internal link)

Bhsd edited this page Apr 15, 2026 · 4 revisions

Other Languages

设置分类的排序关键字

展开

你可以在页面中设置分类的排序关键字。此示例演示了 CategoryToken 的用法。

// Setting the sort key of a category (main)
// 包含 `Category:Foo` 的页面内容
var content = `Foo

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

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

移除图片空白的 alt 属性

展开

你可以在页面中移除图片空白的 alt 属性,以提升无障碍性。此示例演示了 ImageParameterToken 的用法。

// Removing blank alt attributes from images (main)
// 包含图片的页面内容
var content = `[[File:Foo.jpg|alt=]]

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

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

Clone this wiki locally