diff --git a/app/layout.tsx b/app/layout.tsx index 94eab21..09c4be6 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,7 +1,6 @@ import type { Metadata } from 'next' import { Inter, Merriweather, JetBrains_Mono } from 'next/font/google' import { ThemeProvider } from '@wrksz/themes/next' -import { Providers } from './providers' import './globals.css' import 'katex/dist/katex.min.css' import { Header } from '@/components/layout/Header' diff --git a/app/not-found.tsx b/app/not-found.tsx index 6e159af..82ada3f 100644 --- a/app/not-found.tsx +++ b/app/not-found.tsx @@ -8,7 +8,7 @@ export default function NotFound() {

404

Page not found

-

The page you're looking for doesn't exist.

+

The page you{`'`}re looking for doesn{`'`}t exist.

Go home diff --git a/components/blog/TableOfContents.tsx b/components/blog/TableOfContents.tsx index 815c3bb..ed4c052 100644 --- a/components/blog/TableOfContents.tsx +++ b/components/blog/TableOfContents.tsx @@ -8,10 +8,8 @@ interface TOCItem { level: number; } -export function TableOfContents() { +function useHeadings(): TOCItem[] { const [headings, setHeadings] = useState([]); - const [activeId, setActiveId] = useState(""); - const observerRef = useRef(null); useEffect(() => { const elements = Array.from( @@ -24,8 +22,19 @@ export function TableOfContents() { level: el.tagName === "H2" ? 2 : 3, })); + // eslint-disable-next-line react-hooks/set-state-in-effect setHeadings(parsed); + }, []); + return headings; +} + +export function TableOfContents() { + const [activeId, setActiveId] = useState(""); + const observerRef = useRef(null); + const headings = useHeadings(); + + useEffect(() => { observerRef.current = new IntersectionObserver( (entries) => { entries.forEach((entry) => { @@ -37,6 +46,10 @@ export function TableOfContents() { { rootMargin: "-20% 0px -70% 0px", threshold: 0 } ); + const elements = Array.from( + document.querySelectorAll("article h2, article h3") + ) as HTMLElement[]; + elements.forEach((el) => observerRef.current?.observe(el)); return () => observerRef.current?.disconnect(); diff --git a/lib/posts.ts b/lib/posts.ts index 2ea150c..89b642b 100644 --- a/lib/posts.ts +++ b/lib/posts.ts @@ -3,7 +3,7 @@ import path from 'path' import matter from 'gray-matter' import { compileMDX } from 'next-mdx-remote/rsc' import { cache } from 'react' -import { useMDXComponents } from '@/mdx-components' +import { useMDXComponents as getMDXComponents } from '@/mdx-components' import remarkMath from 'remark-math' import remarkGfm from 'remark-gfm' import rehypeKatex from 'rehype-katex' @@ -58,7 +58,7 @@ export const getPost = async (slug: string): Promise => { const filePath = path.join(postsDirectory, file) const raw = await fs.readFile(filePath, 'utf8') const { data, content } = matter(raw) - const components = useMDXComponents({}) + const components = getMDXComponents({}) const { content: compiledContent } = await compileMDX({ source: content,