From 6e1005c78a5760449e88813c15b80830f333a286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Demirel?= Date: Thu, 26 Mar 2026 22:06:03 +0300 Subject: [PATCH] refactor: toc style improved --- src/components/TableOfContents.astro | 161 +++++++++++++++++++++++++++ src/pages/articles/[slug].astro | 27 +---- src/pages/en/articles/[slug].astro | 27 +---- 3 files changed, 167 insertions(+), 48 deletions(-) create mode 100644 src/components/TableOfContents.astro diff --git a/src/components/TableOfContents.astro b/src/components/TableOfContents.astro new file mode 100644 index 0000000..19be069 --- /dev/null +++ b/src/components/TableOfContents.astro @@ -0,0 +1,161 @@ +--- +interface Props { + headings: Array<{ depth: number; slug: string; text: string }> + label: string +} + +const { headings, label } = Astro.props + +const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3) +--- + +{ + tocHeadings.length > 0 && ( + + ) +} + + diff --git a/src/pages/articles/[slug].astro b/src/pages/articles/[slug].astro index 800a88d..e6d72b3 100644 --- a/src/pages/articles/[slug].astro +++ b/src/pages/articles/[slug].astro @@ -3,6 +3,7 @@ import DocsLayout from '../../layouts/DocsLayout.astro' import ShareButtons from '../../components/ShareButtons.astro' import GitHubUserCard from '../../components/GitHubUserCard.astro' import ReadingProgress from '../../components/ReadingProgress.astro' +import TableOfContents from '../../components/TableOfContents.astro' import { getCollection, render } from 'astro:content' import { SITE } from '../../config.mjs' import { getLangFromUrl, t } from '../../i18n/index.ts' @@ -34,7 +35,7 @@ const ogImageUrl = `/og/${article.data.lang}/${article.data.slug}.png` const publishedDate = new Date(article.data.date) const isoDate = publishedDate.toISOString() -const sections = headings.filter((h) => h.depth === 2).map((h) => h.text) +const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3) const jsonLdArticle = { '@context': 'https://schema.org', @@ -152,28 +153,6 @@ const onThisPageLabel = t(labels, 'article.onThisPage') - + diff --git a/src/pages/en/articles/[slug].astro b/src/pages/en/articles/[slug].astro index ada17c4..37fbb26 100644 --- a/src/pages/en/articles/[slug].astro +++ b/src/pages/en/articles/[slug].astro @@ -3,6 +3,7 @@ import DocsLayout from '../../../layouts/DocsLayout.astro' import ShareButtons from '../../../components/ShareButtons.astro' import GitHubUserCard from '../../../components/GitHubUserCard.astro' import ReadingProgress from '../../../components/ReadingProgress.astro' +import TableOfContents from '../../../components/TableOfContents.astro' import { getCollection, render } from 'astro:content' import { SITE } from '../../../config.mjs' import { t } from '../../../i18n/index.ts' @@ -35,7 +36,7 @@ const ogImageUrl = `/og/${article.data.slug}.png` const publishedDate = new Date(article.data.date) const isoDate = publishedDate.toISOString() -const sections = headings.filter((h) => h.depth === 2).map((h) => h.text) +const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3) const jsonLdArticle = { '@context': 'https://schema.org', @@ -151,28 +152,6 @@ const onThisPageLabel = t(labels, 'article.onThisPage') - +