Examples (tag) (EN) - bhsd-harry/wikiparser-node GitHub Wiki
Expand
You can remove bolding from section headers in the content of a page. This example demonstrates the use of AstNode.prototype.remove.
// Removing bolding from section headers (main)
// The content of a page
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 ===`,
);Expand
You can replace the lang attribute of <syntaxhighlight> tags from moin to wikitext in the content of a page. This example demonstrates the use of ExtToken.
// Replacing the syntaxhighlight language (main)
// The content of a page containing `<syntaxhighlight>` tags
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>`,
);Expand
You can easily fix invalid self-closing tags in the content of a page using HtmlToken.prototype.fix.
// Replacing the syntaxhighlight language (main)
// The content of a page containing invalid self-closing tags
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 {
// May throw if it cannot be automatically fixed
html.fix();
} catch {}
}
assert.equal(
root,
"<div>Foo</div>",
);