From 9e3ae0732044e2df03b3adf30cc76b36c06a90e2 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Thu, 22 May 2025 10:30:01 +0200 Subject: [PATCH] Fix TOC linking on blog index page Signed-off-by: Julius Volz --- src/app/blog/page.tsx | 20 ++++++++++++++++---- src/blog-helpers.ts | 11 ++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/app/blog/page.tsx b/src/app/blog/page.tsx index d7927212..6c0c914e 100644 --- a/src/app/blog/page.tsx +++ b/src/app/blog/page.tsx @@ -24,6 +24,10 @@ export async function generateMetadata() { }; } +function headingSlug({ year, month, day, slug }) { + return `${year}-${month}-${day}-${slug}`.replace(/[^A-Za-z0-9\-_]/g, "-"); +} + export default function BlogPage() { const allPosts = getAllPosts(); @@ -36,10 +40,13 @@ export default function BlogPage() { new Date(b.frontmatter.created_at).valueOf() - new Date(a.frontmatter.created_at).valueOf() ) - .map(({ frontmatter, excerpt, path }) => ( - + .map(({ frontmatter, excerpt, path, params }) => ( + // "overflow: unset" is needed, since otherwise "overflow: hidden" + // on the Card breaks the scroll-margin-top of the Title / h1, and + // the title ends up under the sticky header. + - + <Title order={1} mt={0} mb="xs" id={headingSlug(params)}> {frontmatter.title} @@ -63,7 +70,12 @@ export default function BlogPage() { ))} - + ); } diff --git a/src/blog-helpers.ts b/src/blog-helpers.ts index e40829f7..697ddb74 100644 --- a/src/blog-helpers.ts +++ b/src/blog-helpers.ts @@ -67,11 +67,16 @@ export const getPost = (params: { export const getAllPosts = () => { const fileNames = getAllPostFileNames(); return fileNames.map((fileName) => { - const filePath = getPostFilePath(postFileNameToParams(fileName)); - const content = fs.readFileSync(filePath, "utf8"); + const params = postFileNameToParams(fileName); + const content = getPostFileContent(params); const { data, excerpt } = matter(content, { excerpt_separator: "", }); - return { frontmatter: data, excerpt, path: postFileNameToPath(fileName) }; + return { + frontmatter: data, + excerpt, + path: postFileNameToPath(fileName), + params, + }; }); };