it-tools/utils/md-toc.ts
2021-08-02 21:35:12 +02:00

14 lines
454 B
TypeScript

const slugify = (e: string) => encodeURIComponent(String(e).trim().toLowerCase().replace(/\s+/g, '-'))
function toc(content: string) {
return content.split('\n').filter(s => s.startsWith('#')).map((s) => {
const [_, hashtags, title] = s.match(/^(#+) (.*)/) ?? [undefined, 0, '']
const level = Math.max((hashtags as string).length - 2, 0)
return `${' '.repeat(level)}- [${title}](#${slugify(title as string)})`
})
}
export {
toc
}