aubade
Version:
markdown, orchestrated.
42 lines (41 loc) • 1.51 kB
JavaScript
import { engrave } from '../artisan/index.js';
import { parse as manifest } from '../manifest/index.js';
export function assemble(source) {
const match = /---\r?\n([\s\S]+?)\r?\n---/.exec(source);
const body = match ? source.slice(match.index + match[0].length) : source;
const document = engrave(body.trim());
return {
doc: document,
manifest: match ? manifest(match[1].trim()) : {},
meta: {
head: match ? match[0] : '',
body: body.trim() + '\n',
get table() {
const toc = [];
for (const token of document.tokens) {
if (token.type !== 'block:heading')
continue;
toc.push({
id: token.attr.id,
title: token.meta.text,
level: token.meta.level,
});
}
return toc;
},
get words() {
return count(document.tokens);
function count(tokens) {
let total = 0;
for (const token of tokens) {
if ('children' in token)
total += count(token.children);
else if ('text' in token)
total += token.text.split(/\s+/).length;
}
return total;
}
},
},
};
}