Examples (tag) - bhsd-harry/wikiparser-node GitHub Wiki
展开
你可以移除页面中章节标题的加粗标签。此示例演示了 AstNode.prototype.remove 的用法。
// Removing bolding from section headers (main)
// 页面内容
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 ===`,
);展开
你可以将页面中 <syntaxhighlight> 标签的 lang 属性从 moin 替换为 wikitext。此示例演示了 ExtToken 的用法。
// Replacing the syntaxhighlight language (main)
// 包含 `<syntaxhighlight>` 标签的页面内容
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>`,
);展开
你可以使用 HtmlToken.prototype.fix 轻松修复页面中的无效自封闭标签。
// Replacing the syntaxhighlight language (main)
// 包含无效自封闭标签的页面内容
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 {
// 如果无法自动修复可能会抛出错误
html.fix();
} catch {}
}
assert.equal(
root,
"<div>Foo</div>",
);