The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: active toc link with ReactMarkdown
Francesco Sansalvadorebdce77de2c03
diff --git a/apps/www/hooks/useActiveAnchors.tsx b/apps/www/hooks/useActiveAnchors.tsxindex 477ce04d825..c4919cb2852 100644--- a/apps/www/hooks/useActiveAnchors.tsx+++ b/apps/www/hooks/useActiveAnchors.tsx@@ -1,13 +1,16 @@import { useEffect, useRef, useState } from 'react'-const useActiveAnchors = (querySelector: string = 'h2') => {+const useActiveAnchors = (+ anchorsQuerySelector: string = 'h2',+ tocQuerySelector: string = '.prose-toc a',+ offset: number = 200+) => {const anchors = useRef<NodeListOf<HTMLHeadingElement> | null>(null)- const [activeSection, setActiveSection] = useState<any>('')+ const toc = useRef<NodeListOf<HTMLHeadingElement> | null>(null)const handleScroll = () => {const pageYOffset = window.pageYOffset- let newActiveAnchor = null- const offset = 150+ let newActiveAnchor: string = ''anchors.current?.forEach((anchor) => {if (pageYOffset >= anchor.offsetTop - offset) {@@ -15,11 +18,20 @@ const useActiveAnchors = (querySelector: string = 'h2') => {}})- setActiveSection(newActiveAnchor)+ toc.current?.forEach((link) => {+ link.classList.remove('translate-x-1')+ link.classList.remove('!text-brand-900')+ if (link.getAttribute('href')?.replace('#', '') === newActiveAnchor) {+ link.classList.add('translate-x-1')+ link.classList.add('!text-brand-900')+ }+ })}useEffect(() => {- anchors.current = document.querySelectorAll(querySelector)+ anchors.current = document.querySelectorAll(anchorsQuerySelector)+ toc.current = document.querySelectorAll(tocQuerySelector)+window.addEventListener('scroll', handleScroll)return () => {@@ -27,7 +39,7 @@ const useActiveAnchors = (querySelector: string = 'h2') => {}}, [])- return activeSection+ return null}export default useActiveAnchorsdiff --git a/apps/www/pages/blog/[slug].tsx b/apps/www/pages/blog/[slug].tsxindex 4f96db40972..51c3d363b58 100644--- a/apps/www/pages/blog/[slug].tsx+++ b/apps/www/pages/blog/[slug].tsx@@ -16,6 +16,7 @@ import useActiveAnchors from '~/hooks/useActiveAnchors'import mdxComponents from '~/lib/mdx/mdxComponents'import { mdxSerialize } from '~/lib/mdx/mdxSerialize'import { getAllPostSlugs, getPostdata, getSortedPosts } from '~/lib/posts'+import { ReactMarkdown } from 'react-markdown/lib/react-markdown'// table of contents extractorconst toc = require('markdown-toc')@@ -67,7 +68,7 @@ export async function getStaticProps({ params }: any) {function BlogPostPage(props: any) {const content = props.blog.contentconst authorArray = props.blog.author.split(',')- const activeSection = useActiveAnchors('h2')+ useActiveAnchors('h2, h3, h4')const author = []for (let i = 0; i < authorArray.length; i++) {@@ -104,6 +105,8 @@ function BlogPostPage(props: any) {)}+ // console.log('props.blog.toc', props.blog.toc)+const toc = props.blog.toc && (<div className="space-y-8 py-8 lg:py-0"><div>@@ -121,21 +124,7 @@ function BlogPostPage(props: any) {<div><p className="text-scale-1200 mb-4">On this page</p><div className="prose-toc">- <ul className="te">- {props.blog.toc.json.map((section: any) => (- <li key={section.slug} className="list-style-none">- <a- className={[- 'list-style-none translate-x-0 transition-all',- section.slug === activeSection && 'translate-x-1 !text-brand-900',- ].join(' ')}- href={`#${section.slug}`}- >- {section.content}- </a>- </li>- ))}- </ul>+ <ReactMarkdown>{props.blog.toc.content}</ReactMarkdown></div></div></div>