Skip to content

Examples (tag)

Bhsd edited this page Apr 15, 2026 · 5 revisions

Other Languages

移除章节标题的加粗标签

展开

你可以移除页面中章节标题的加粗标签。此示例演示了 AstNode.prototype.remove 的用法。

// Removing bolding from section headers (main)
// 页面内容
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 ===`,
);

替换 syntaxhighlight 的语言属性

展开

你可以将页面中 <syntaxhighlight> 标签的 lang 属性从 moin 替换为 wikitext。此示例演示了 ExtToken 的用法。

// Replacing the syntaxhighlight language (main)
// 包含 `<syntaxhighlight>` 标签的页面内容
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>`,
);

修复无效的自封闭标签

展开

你可以使用 HtmlToken.prototype.fix 轻松修复页面中的无效自封闭标签。

// Replacing the syntaxhighlight language (main)
// 包含无效自封闭标签的页面内容
var content = '<div>Foo<div/>',
	root = Parser.parse(content);
for (const html of root.querySelectorAll('html[selfClosing]')) {
	try {
		// 如果无法自动修复可能会抛出错误
		html.fix();
	} catch {}
}
assert.equal(
	root,
	'<div>Foo</div>',
);

Clone this wiki locally