Examples (list) (EN) - bhsd-harry/wikiparser-node GitHub Wiki
Expand
You can increase the indentation of a definition list in the content of a page. This example demonstrates the use of ListToken.
// Increasing indentation of list items (main)
// The content of a page containing a definition list
import type {ListToken} from "wikiparser-node";
const content = `: Foo
:: Bar`,
root = Parser.parse(content);
for (const list of root.querySelectorAll<ListToken>("list")) {
if (!list.ul && !list.ol && !list.dt) {
list.indent++;
}
}
assert.equal(
root,
`::Foo
:::Bar`,
);Expand
You can convert definition terms to section headers in the content of a page. This example demonstrates the use of Token.prototype.buildLists and ListToken.
// Converting definition terms to section headers (main)
// The content of a page containing definition terms
import type {ListToken, HeadingToken, DdToken} from "wikiparser-node";
const content = `== Foo ==
; Bar
Baz`,
root = Parser.parse(content);
root.buildLists();
const sections = root.sections()!;
for (const dt of root.querySelectorAll<ListToken>("root > list")) {
const {nextSibling} = dt;
if (
dt.ul || dt.ol || dt.dd
|| nextSibling?.type !== "list-range"
|| !nextSibling.text().trim()
// Not a lonely definition term
|| nextSibling.nextSibling?.is<DdToken>("dd")
|| nextSibling.nextSibling?.text() === "\n"
&& nextSibling.nextSibling.nextSibling?.type === "list"
) {
continue;
}
const level = sections.findLast(
({childNodes}) => childNodes.includes(dt),
)?.querySelector<HeadingToken>("heading")?.level;
if (level) {
const title = nextSibling.text().trim(),
equals = "=".repeat(level + 1);
nextSibling.remove();
dt.replaceWith(`${equals} ${title} ${equals}`);
}
}
assert.equal(
root,
`== Foo ==
=== Bar ===
Baz`,
);