mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 16:51:37 +00:00
Update table of contents
This commit is contained in:
@ -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 [];
|
||||
};
|
||||
|
Reference in New Issue
Block a user