From dfc6d085e09e2dce7f7562fec8e6992fec4c4b34 Mon Sep 17 00:00:00 2001
From: Emil Gulamov <125820963+mearashadowfax@users.noreply.github.com>
Date: Tue, 2 Apr 2024 20:21:57 +0400
Subject: [PATCH] Remove generateToc file and refactor TOC in insights page
The generateToc.ts file has been deleted and the way Table of Contents is generated in the insights page has been refactored. The refactoring changes include graphical enhancements, making the UI more dynamic, and implementing GSAP's ScrollTrigger for the headings. Increased readability and easier code maintenance are also anticipated from this.
---
src/pages/insights/[...slug].astro | 132 +++++++++++++++++++++++++----
src/utils/generateToc.ts | 51 -----------
2 files changed, 117 insertions(+), 66 deletions(-)
delete mode 100644 src/utils/generateToc.ts
diff --git a/src/pages/insights/[...slug].astro b/src/pages/insights/[...slug].astro
index 8b2e945..95cb1a0 100644
--- a/src/pages/insights/[...slug].astro
+++ b/src/pages/insights/[...slug].astro
@@ -36,11 +36,23 @@ const pageTitle: string = `${post.data.title} | ${SITE.title}`;
format={"avif"}
/>
-
-
-
+
+
+
Table of Contents:
-
+
+
@@ -63,33 +75,37 @@ const pageTitle: string = `${post.data.title} | ${SITE.title}`;
diff --git a/src/utils/generateToc.ts b/src/utils/generateToc.ts
deleted file mode 100644
index d77d3d7..0000000
--- a/src/utils/generateToc.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-// https://github.com/withastro/docs/blob/882e0b0a9d16d1c822cb8c230a62a4bfcd308605/src/util/generateToc.ts
-import type { MarkdownHeading } from 'astro';
-export interface TocItem extends MarkdownHeading {
- children: TocItem[];
-}
-
-function diveChildren(item: TocItem, depth: number): TocItem[] {
- if (depth === 1) {
- return item.children;
- } else {
- // e.g., 2
- return diveChildren(item.children[item.children.length - 1], depth - 1);
- }
-}
-
-export default function generateToc(headings: MarkdownHeading[], title = 'Overview') {
- const overview = { depth: 2, slug: 'overview', text: title };
- headings = [overview, ...headings.filter(({ depth }) => depth > 1 && depth < 4)];
- const toc: Array = [];
-
- for (const heading of headings) {
- if (toc.length === 0) {
- toc.push({
- ...heading,
- children: [],
- });
- } else {
- const lastItemInToc = toc[toc.length - 1];
- if (heading.depth < lastItemInToc.depth) {
- throw new Error(`Orphan heading found: ${heading.text}.`);
- }
- if (heading.depth === lastItemInToc.depth) {
- // same depth
- toc.push({
- ...heading,
- children: [],
- });
- } else {
- // higher depth
- // push into children, or children' children alike
- const gap = heading.depth - lastItemInToc.depth;
- const target = diveChildren(lastItemInToc, gap);
- target.push({
- ...heading,
- children: [],
- });
- }
- }
- }
- return toc;
-}
\ No newline at end of file