Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: finish highlighting active toc label in blog post

Francesco Sansalvadore3562f9697df2
diff --git a/apps/www/components/Blog/BlogListItem.tsx b/apps/www/components/Blog/BlogListItem.tsx
index c08b5006db7..423c6b077be 100644
--- a/apps/www/components/Blog/BlogListItem.tsx
+++ b/apps/www/components/Blog/BlogListItem.tsx
@@ -1,5 +1,6 @@
import authors from 'lib/authors.json'
import Image from 'next/image'
+import Link from 'next/link'
import React from 'react'
import Author from '~/types/author'
import PostTypes from '~/types/post'
@@ -24,8 +25,8 @@ const BlogListItem = ({ post }: Props) => {
return (
<div>
- <a href={`${post.path}`}>
- <div className="group inline-block min-w-full">
+ <Link href={post.path}>
+ <a className="group inline-block min-w-full">
<div className="flex flex-col space-y-6">
<div className="flex flex-col space-y-3">
<div
@@ -69,8 +70,8 @@ const BlogListItem = ({ post }: Props) => {
})}
</div>
</div>
- </div>
- </a>
+ </a>
+ </Link>
</div>
)
}
diff --git a/apps/www/hooks/useActiveAnchors.tsx b/apps/www/hooks/useActiveAnchors.tsx
index 00a48824387..d02509c147c 100644
--- a/apps/www/hooks/useActiveAnchors.tsx
+++ b/apps/www/hooks/useActiveAnchors.tsx
@@ -1,10 +1,13 @@
+import { useRouter } from 'next/router'
import { useEffect, useRef } from 'react'
+import { isBrowser, stripEmojis } from '~/lib/helpers'
const useActiveAnchors = (
anchorsQuerySelector: string = 'h2',
tocQuerySelector: string = '.prose-toc a',
offset: number = 200
) => {
+ const router = useRouter()
const anchors = useRef<NodeListOf<HTMLHeadingElement> | null>(null)
const toc = useRef<NodeListOf<HTMLHeadingElement> | null>(null)
@@ -20,10 +23,12 @@ const useActiveAnchors = (
toc.current?.forEach((link) => {
link.classList.remove('toc-animate')
- // TODO: escape emojis
- // The problem is that MDXRemote strips out emojis on slugs
- // but ReactMarkdown doesn't,instead it encodes them
- const sanitizedHref = (link.getAttribute('href') ?? '').replace('#', '')
+
+ // Need to decodeURI the href to get emojis, then strip them off and remove "--""
+ // to make toc hrefs and content headings ids match
+ const sanitizedHref = stripEmojis(
+ decodeURI(link.getAttribute('href') ?? '').replace('#', '')
+ ).replace('--', '-')
const isMatch = sanitizedHref === newActiveAnchor
if (isMatch) {
@@ -33,6 +38,7 @@ const useActiveAnchors = (
}
useEffect(() => {
+ if (!isBrowser) return
anchors.current = document.querySelectorAll(anchorsQuerySelector)
toc.current = document.querySelectorAll(tocQuerySelector)
diff --git a/apps/www/lib/helpers.tsx b/apps/www/lib/helpers.tsx
index fbf8fe599dd..eb8495ca624 100644
--- a/apps/www/lib/helpers.tsx
+++ b/apps/www/lib/helpers.tsx
@@ -31,3 +31,12 @@ export const toPixels = (value: string | number) => {
}
export const isBrowser = typeof window !== 'undefined'
+
+export const stripEmojis = (str: string) =>
+ str
+ .replace(
+ /([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g,
+ ''
+ )
+ .replace(/\s+/g, ' ')
+ .trim()
diff --git a/apps/www/pages/blog/[slug].tsx b/apps/www/pages/blog/[slug].tsx
index 58eea27ee2b..27a7b28b2a2 100644
--- a/apps/www/pages/blog/[slug].tsx
+++ b/apps/www/pages/blog/[slug].tsx
@@ -12,7 +12,7 @@ import DefaultLayout from '~/components/Layouts/Default'
import BlogLinks from '~/components/LaunchWeek/7/BlogLinks'
import { generateReadingTime } from '~/lib/helpers'
import ShareArticleActions from '~/components/Blog/ShareArticleActions'
-// import useActiveAnchors from '~/hooks/useActiveAnchors'
+import useActiveAnchors from '~/hooks/useActiveAnchors'
import mdxComponents from '~/lib/mdx/mdxComponents'
import { mdxSerialize } from '~/lib/mdx/mdxSerialize'
import { getAllPostSlugs, getPostdata, getSortedPosts } from '~/lib/posts'
@@ -56,9 +56,9 @@ export async function getStaticProps({ params }: any) {
relatedPosts,
blog: {
slug: `${params.slug}`,
- content: mdxSource,
source: content,
...data,
+ content: mdxSource,
toc: toc(content, { maxdepth: data.toc_depth ? data.toc_depth : 2 }),
},
},
@@ -68,7 +68,7 @@ export async function getStaticProps({ params }: any) {
function BlogPostPage(props: any) {
const content = props.blog.content
const authorArray = props.blog.author.split(',')
- // useActiveAnchors('h2, h3, h4')
+ useActiveAnchors('h2, h3, h4', '.prose-toc a')
const isLaunchWeek7 = props.blog.launchweek === 7
const author = []
diff --git a/apps/www/public/rss.xml b/apps/www/public/rss.xml
index c8bcd42038e..ca327cabe0f 100644
--- a/apps/www/public/rss.xml
+++ b/apps/www/public/rss.xml
@@ -5,12 +5,19 @@
<link>https://supabase.com</link>
<description>Latest news from Supabase</description>
<language>en</language>
- <lastBuildDate>Mon, 10 Apr 2023 22:10:00 +0000</lastBuildDate>
+ <lastBuildDate>Tue, 11 Apr 2023 22:10:00 +0000</lastBuildDate>
<atom:link href="https://supabase.com/rss.xml" rel="self" type="application/rss+xml"/>
<item>
- <guid>https://supabase.com/blog/edge-runtime-self-hosted-deno</guid>
+ <guid>https://supabase.com/blog/storage-v3-resumable-uploads</guid>
+ <title>Supabase Storage v3: Resumable Uploads with support for 50GB files</title>
+ <link>https://supabase.com/blog/storage-v3-resumable-uploads</link>
+ <description>undefined</description>
+ <pubDate>Tue, 11 Apr 2023 22:10:00 +0000</pubDate>
+</item>
+<item>
+ <guid>https://supabase.com/blog/edge-runtime-self-hosted-deno-functions</guid>
<title>Supabase Edge Runtime: Self-hosted Deno Functions</title>
- <link>https://supabase.com/blog/edge-runtime-self-hosted-deno</link>
+ <link>https://supabase.com/blog/edge-runtime-self-hosted-deno-functions</link>
<description>undefined</description>
<pubDate>Mon, 10 Apr 2023 22:10:00 +0000</pubDate>
</item>
diff --git a/apps/www/styles/index.css b/apps/www/styles/index.css
index 15de303b62c..916a53c1eb2 100644
--- a/apps/www/styles/index.css
+++ b/apps/www/styles/index.css
@@ -85,8 +85,7 @@ h6:not(.overwrite) {
}
.toc-animate {
- transform: translate3d(3px, 0, 0) !important;
- color: hsla(153, 60%, 53%, 1) !important;
+ @apply !translate-x-1 !text-brand-900;
}
.prose-toc a {