Skip to content

Examples (list)

Bhsd edited this page Apr 15, 2026 · 6 revisions

Increasing indentation of list items

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
var content = `: Foo
:: Bar`,
	root = Parser.parse(content);
for (const list of root.querySelectorAll('list')) {
	if (!list.ul && !list.ol && !list.dt) {
		list.indent++;
	}
}
assert.equal(
	root,
	`::Foo
:::Bar`,
);

Converting definition terms to section headers

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
var content = `== Foo ==
; Bar
Baz`,
	root = Parser.parse(content);
root.buildLists();
var sections = root.sections();
for (const dt of root.querySelectorAll('root > list')) {
	const {nextSibling} = dt;
	if (
		dt.ul || dt.ol || dt.dl
		|| nextSibling?.type !== 'list-range'
		|| !nextSibling.text().trim()
		// Not a lonely definition term
		|| nextSibling.nextSibling?.type === 'dd'
		|| nextSibling.nextSibling?.text() === '\n'
		&& nextSibling.nextSibling.nextSibling?.type === 'list'
	) {
		continue;
	}
	const level = sections.findLast(
		({childNodes}) => childNodes.includes(dt),
	)?.querySelector('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`,
);

Clone this wiki locally