Update table of contents

This commit is contained in:
Defectink
2022-04-01 15:15:38 +08:00
parent d68b4523ef
commit 11fcb9677f
7 changed files with 71 additions and 39 deletions

View File

@ -10,3 +10,30 @@ export const sortByDate = (
return 0;
}
};
/**
* Read post head to mark TOC
* @param source
* @returns
*/
export const getHeadings = (source: string) => {
const regex = /<h2 id="(.*?)">(.*?)<\/h2>/g;
const linkRegx = /id="(.*?)"/;
if (source.match(regex)) {
return source.match(regex)?.map((heading) => {
const headingText = heading
.replace(/<h2 id="(.*?)">/, '')
.replace(/<\/h2>/, '');
const link = '#' + heading.match(linkRegx)?.[1];
return {
text: headingText,
link,
};
});
}
return [];
};