mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-24 08:46:15 -04:00
feat(memo): added memo base
This commit is contained in:
parent
a40aae44ce
commit
a5953a3c42
11 changed files with 864 additions and 157 deletions
14
utils/md-toc.ts
Normal file
14
utils/md-toc.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue