initial commit

This commit is contained in:
PrinOrange
2023-12-25 17:21:39 +08:00
commit 0bd1089d74
94 changed files with 18648 additions and 0 deletions

23
lib/toc.ts Normal file
View File

@@ -0,0 +1,23 @@
import { TTOCItem } from "@/types/toc.type";
import { JSDOM } from "jsdom";
/**
* Generate the Table Of Content List by html code.
* It supports h1-h6 level headings at most.
* @param htmlCode
* @returns
*/
export const getTOCTree = (htmlCode: string) => {
const doc_dom = new JSDOM(htmlCode);
const all_headers = doc_dom.window.document.querySelectorAll("h1,h2,h3,h4,h5,h6");
const result: TTOCItem[] = [];
for (let i = 0; i < all_headers.length; i++) {
const level = parseInt(all_headers[i].tagName.replace("H", ""));
result.push({
level: level,
anchorId: all_headers[i].id,
title: all_headers[i].textContent!,
});
}
return result;
};