Skip to content

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

Chore/custom html elements cleanup (#26836)

Francesco Sansalvadore7f42a81dce1d
diff --git a/apps/docs/components/CustomHTMLElements/CustomHTMLElements.utils.ts b/apps/docs/components/CustomHTMLElements/CustomHTMLElements.utils.ts
deleted file mode 100644
index 337f48b2bbf..00000000000
--- a/apps/docs/components/CustomHTMLElements/CustomHTMLElements.utils.ts
+++ /dev/null
@@ -1,101 +0,0 @@
-// Check if heading has custom anchor first, before forming the anchor based on the title
-export const getAnchor = (text: any): string | undefined => {
- if (typeof text === 'object') {
- if (Array.isArray(text)) {
- const customAnchor = text.find((x) => typeof x === 'string' && hasCustomAnchor(x))
- if (customAnchor !== undefined) {
- return parseCustomAnchor(customAnchor)
- }
-
- const formattedText = text
- .map((x) => {
- if (typeof x !== 'string') {
- return x.props.children
- }
-
- return x.trim()
- })
- .map((x) => {
- if (typeof x !== 'string') {
- return x
- }
-
- return slugify(x)
- })
-
- return formattedText.join('-').toLowerCase()
- } else {
- const anchor = text.props.children
- if (typeof anchor === 'string') {
- return slugify(anchor)
- }
- return anchor
- }
- } else if (typeof text === 'string') {
- if (hasCustomAnchor(text)) {
- return parseCustomAnchor(text)
- }
- return slugify(text)
- } else {
- return undefined
- }
-}
-
-const hasCustomAnchor = (value: string): boolean => value.includes('[#') && value.includes(']')
-
-const parseCustomAnchor = (value: string): string =>
- value.slice(value.indexOf('[#') + 2, value.indexOf(']'))
-
-const slugify = (value: string): string =>
- value
- .toLowerCase()
- .trim()
- .replace(/[^a-z0-9- ]/g, '')
- .replace(/[ ]/g, '-')
-
-export const removeAnchor = (text: any) => {
- if (typeof text === 'object' && Array.isArray(text)) {
- return text.filter((x) => !(typeof x === 'string' && hasCustomAnchor(x)))
- } else if (typeof text === 'string') {
- if (text.indexOf('[#') > 0) return text.slice(0, text.indexOf('[#'))
- else return text
- }
- return text
-}
-
-export const highlightSelectedTocItem = (id: string) => {
- const tocMenuItems = document.querySelectorAll('.toc-menu a')
-
- // find any currently active items and remove them
- const currentActiveItem = document.querySelector('.toc-menu .toc__menu-item--active')
- currentActiveItem?.classList.remove('toc__menu-item--active')
-
- // Add active class to the current item
- tocMenuItems.forEach((item) => {
- // @ts-ignore
- if (item.href.split('#')[1] === id) {
- item.classList.add('toc__menu-item--active')
- }
- })
-}
-
-// find any currently active items and remove them on route change
-export const unHighlightSelectedTocItems = () => {
- const currentActiveItem = document.querySelector('.toc-menu .toc__menu-item--active')
- currentActiveItem?.classList.remove('toc__menu-item--active')
-}
-
-export const highlightSelectedNavItem = (id: string) => {
- const navMenuItems = document.querySelectorAll<HTMLAnchorElement>('.function-link-item a')
-
- // find any currently active items and remove them
- const currentActiveItems = document.querySelectorAll('.function-link-list .text-brand')
- currentActiveItems.forEach((item) => item.classList.remove('text-brand'))
-
- // Add active class to the current item
- navMenuItems.forEach((item) => {
- if (item.href.split('/').at(-1) === id) {
- item.classList.add('text-brand')
- }
- })
-}
diff --git a/apps/docs/components/CustomHTMLElements/Heading.tsx b/apps/docs/components/CustomHTMLElements/Heading.tsx
deleted file mode 100644
index bb3eed1a3d9..00000000000
--- a/apps/docs/components/CustomHTMLElements/Heading.tsx
+++ /dev/null
@@ -1,60 +0,0 @@
-import {
- getAnchor,
- removeAnchor,
- highlightSelectedTocItem,
- unHighlightSelectedTocItems,
-} from './CustomHTMLElements.utils'
-import { useInView } from 'react-intersection-observer'
-
-/**
- * [Joshen] The trick with rootMargin
- * We are shrinking the top of the root element by 20 percent, which is currently our entire page,
- * and the bottom by 35 percent. Therefore, when a header is at the top 20 percent and bottom 35 percent
- * of our page, it will not be counted as visible.
- */
-
-interface Props {
- tag?: string
- parseAnchors?: boolean
- customAnchor?: string
-}
-
-/**
- * This TOC is used in .mdx files and in .tsx files.
- * In mdx files, we need to parse the content and format them to match the
- * expected tocList format (text, link,level).
- *
- * In tsx files, we can generate this tocList directly. For these files, we don't
- * need to parse the <a> and generate anchors. Custom anchors are used in tsx files.
- * (see: /pages/guides/cli/config.tsx)
- */
-const Heading: React.FC<React.PropsWithChildren<Props>> = ({ tag, customAnchor, children }) => {
- const HeadingTag = `${tag}` as any
- const anchor = customAnchor ? customAnchor : getAnchor(children)
- const link = `#${anchor}`
-
- const { ref } = useInView({
- threshold: 1,
- rootMargin: '-20% 0% -35% 0px',
- onChange: (inView, entry) => {
- if (window.scrollY === 0) unHighlightSelectedTocItems()
- if (inView) highlightSelectedTocItem(entry.target.id)
- },
- })
-
- return (
- <HeadingTag id={anchor} ref={ref} className="group scroll-mt-24">
- {removeAnchor(children)}
- {anchor && (
- <a
- href={link}
- aria-hidden="true"
- className="ml-2 opacity-0 group-hover:opacity-100 transition"
- >
- <span aria-hidden="true">#</span>
- </a>
- )}
- </HeadingTag>
- )
-}
-export default Heading
diff --git a/apps/docs/components/CustomHTMLElements/InlineCode.tsx b/apps/docs/components/CustomHTMLElements/InlineCode.tsx
deleted file mode 100644
index 5ee5811c2f4..00000000000
--- a/apps/docs/components/CustomHTMLElements/InlineCode.tsx
+++ /dev/null
@@ -1,13 +0,0 @@
-import { FC } from 'react'
-
-const InlineCodeTag = ({ children }: any) => {
- // If children isn't a string, just return it as is, just in case
- if (typeof children !== 'string') return children
-
- // check the length of the string inside the <code> tag
- // if it's fewer than 70 characters, add a white-space: pre so it doesn't wrap
- const className = children.length < 70 ? 'short-inline-codeblock' : ''
-
- return <code className={className}> HELLO WORLD {children}</code>
-}
-export default InlineCodeTag
diff --git a/apps/docs/components/CustomHTMLElements/index.tsx b/apps/docs/components/CustomHTMLElements/index.tsx
deleted file mode 100644
index 56ded7aefd1..00000000000
--- a/apps/docs/components/CustomHTMLElements/index.tsx
+++ /dev/null
@@ -1,3 +0,0 @@
-import Heading from './Heading'
-
-export { Heading }
diff --git a/apps/docs/components/GuidesTableOfContents.tsx b/apps/docs/components/GuidesTableOfContents.tsx
index 3e4aefbc7ba..1125f974076 100644
--- a/apps/docs/components/GuidesTableOfContents.tsx
+++ b/apps/docs/components/GuidesTableOfContents.tsx
@@ -6,7 +6,7 @@ import { proxy, useSnapshot } from 'valtio'
import {
highlightSelectedTocItem,
removeAnchor,
-} from '~/components/CustomHTMLElements/CustomHTMLElements.utils'
+} from 'ui/src/components/CustomHTMLElements/CustomHTMLElements.utils'
import { Feedback } from '~/components/Feedback'
import useHash from '~/hooks/useHash'
diff --git a/apps/docs/components/index.tsx b/apps/docs/components/index.tsx
index d8ec7a4a0f6..76ed777d98d 100644
--- a/apps/docs/components/index.tsx
+++ b/apps/docs/components/index.tsx
@@ -14,7 +14,7 @@ import { TabPanel, Tabs } from '~/components/Tabs'
import { CH } from '@code-hike/mdx/components'
import StepHikeCompact from '~/components/StepHikeCompact'
import ButtonCard from './ButtonCard'
-import { Heading } from './CustomHTMLElements'
+import { Heading } from 'ui'
// Reference guide specific
// [Charis] I think we can factor these out so they aren't in the bundle for absolutely everything
diff --git a/apps/docs/layouts/ref/RefSubLayout.tsx b/apps/docs/layouts/ref/RefSubLayout.tsx
index a14c56139be..69a91fc1db3 100644
--- a/apps/docs/layouts/ref/RefSubLayout.tsx
+++ b/apps/docs/layouts/ref/RefSubLayout.tsx
@@ -1,6 +1,6 @@
import { useInView } from 'react-intersection-observer'
import { FC, PropsWithChildren } from 'react'
-import { highlightSelectedNavItem } from '~/components/CustomHTMLElements/CustomHTMLElements.utils'
+import { highlightSelectedNavItem } from 'ui/src/components/CustomHTMLElements/CustomHTMLElements.utils'
import { useRouter } from 'next/router'
import { useNavigationMenuContext } from '~/components/Navigation/NavigationMenu/NavigationMenu.Context'
import { menuState } from '~/hooks/useMenuState'
diff --git a/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx b/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx
index 3ae40579e67..b31b8260e68 100644
--- a/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx
+++ b/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx
@@ -1,6 +1,6 @@
import { useInView } from 'react-intersection-observer'
import { FC, PropsWithChildren } from 'react'
-import { highlightSelectedNavItem } from '~/components/CustomHTMLElements/CustomHTMLElements.utils'
+import { highlightSelectedNavItem } from 'ui/src/components/CustomHTMLElements/CustomHTMLElements.utils'
import { useRouter } from 'next/router'
import { useNavigationMenuContext } from '~/components/Navigation/NavigationMenu/NavigationMenu.Context'
import { menuState } from '~/hooks/useMenuState'
diff --git a/apps/docs/package.json b/apps/docs/package.json
index 3af05c7c50b..2ce3f0375ed 100644
--- a/apps/docs/package.json
+++ b/apps/docs/package.json
@@ -10,7 +10,7 @@
"start": "next start",
"lint": "next lint",
"typecheck": "tsc --noEmit",
- "test": "jest",
+ "test": "jest --passWithNoTests",
"build:sitemap": "node ./internals/generate-sitemap.mjs",
"embeddings": "tsx scripts/search/generate-embeddings.ts",
"embeddings:refresh": "npm run embeddings -- --refresh",
diff --git a/apps/docs/pages/guides/cli/config.tsx b/apps/docs/pages/guides/cli/config.tsx
index 51c40bd31f9..89b16e55973 100644
--- a/apps/docs/pages/guides/cli/config.tsx
+++ b/apps/docs/pages/guides/cli/config.tsx
@@ -3,7 +3,7 @@ import ReactMarkdown from 'react-markdown'
import { CodeBlock, cn } from 'ui'
-import { Heading } from '~/components/CustomHTMLElements'
+import { Heading } from 'ui/src/components/CustomHTMLElements'
import GuidesTableOfContents from '~/components/GuidesTableOfContents'
import { MenuId } from '~/components/Navigation/NavigationMenu/NavigationMenu'
import { MainSkeleton } from '~/layouts/MainSkeleton'
diff --git a/apps/docs/pages/guides/database/database-advisors.tsx b/apps/docs/pages/guides/database/database-advisors.tsx
index 70d678eacb1..a699383dce3 100644
--- a/apps/docs/pages/guides/database/database-advisors.tsx
+++ b/apps/docs/pages/guides/database/database-advisors.tsx
@@ -10,7 +10,7 @@ import rehypeSlug from 'rehype-slug'
import codeHikeTheme from 'config/code-hike.theme.json' assert { type: 'json' }
import components from '~/components'
-import { Heading } from '~/components/CustomHTMLElements'
+import { Heading } from 'ui'
import { TabPanel, Tabs } from '~/components/Tabs'
import { MenuId } from '~/components/Navigation/NavigationMenu/NavigationMenu'
import Layout from '~/layouts/DefaultGuideLayout'
diff --git a/packages/ui/src/components/CustomHTMLElements/CustomHTMLElements.utils.test.ts b/packages/ui/src/components/CustomHTMLElements/CustomHTMLElements.utils.test.ts
deleted file mode 100644
index 2d2d1285229..00000000000
--- a/packages/ui/src/components/CustomHTMLElements/CustomHTMLElements.utils.test.ts
+++ /dev/null
@@ -1,96 +0,0 @@
-import { getAnchor, removeAnchor } from './CustomHTMLElements.utils'
-
-describe('CustomHTMLElementsUtils', () => {
- describe('getAnchor', () => {
- describe('when value is an object', () => {
- it('returns slugified version of props.children', () => {
- const value = {
- props: {
- children: 'Full Text Search',
- },
- }
-
- const result = getAnchor(value)
-
- expect(result).toStrictEqual('full-text-search')
- })
- })
-
- describe('when value is an array', () => {
- describe('when custom anchor exists', () => {
- it('returns inner slug', () => {
- const value = ['Proximity: <->', '[#proximity]']
-
- const result = getAnchor(value)
-
- expect(result).toStrictEqual('proximity')
- })
-
- it('trims whitespace', () => {
- const value = ['Proximity: <->', ' [#proximity] ']
-
- const result = getAnchor(value)
-
- expect(result).toStrictEqual('proximity')
- })
- })
-
- it('returns concatenated slug of elements', () => {
- const value = ['Full', 'Text', 'Search']
-
- const result = getAnchor(value)
-
- expect(result).toStrictEqual('full-text-search')
- })
-
- it('trims whitespace', () => {
- const value = [' Full ', ' Text ', ' Search ']
-
- const result = getAnchor(value)
-
- expect(result).toStrictEqual('full-text-search')
- })
-
- it('removes special characters', () => {
- const value = ['function()']
-
- const result = getAnchor(value)
-
- expect(result).toStrictEqual('function')
- })
- })
-
- describe('when value is a string', () => {
- it('returns slugified version of string', () => {
- const value = 'My (Very) Awesome Heading'
-
- const result = getAnchor(value)
-
- expect(result).toStrictEqual('my-very-awesome-heading')
- })
- })
- })
-
- describe('removeAnchor', () => {
- describe('when value is an array', () => {
- it('filters out custom anchor elements', () => {
- const value = ['My (Very) Awesome Heading', '[#my-custom-heading]']
-
- const result = removeAnchor(value)
-
- expect(result).toStrictEqual(['My (Very) Awesome Heading'])
- })
- })
-
- describe('when value is a string', () => {
- it('strips out custom anchor string', () => {
- const value = 'My (Very) Awesome Heading [#my-custom-heading]'
-
- const result = removeAnchor(value)
-
- // Original implementation didn't trim the resulting string - not sure if it really matters
- expect(result).toStrictEqual('My (Very) Awesome Heading ')
- })
- })
- })
-})
diff --git a/apps/docs/components/CustomHTMLElements/CustomHTMLElements.utils.test.ts b/packages/ui/src/components/CustomHTMLElements/CustomHTMLElements.utils.test.tsx
similarity index 100%
rename from apps/docs/components/CustomHTMLElements/CustomHTMLElements.utils.test.ts
rename to packages/ui/src/components/CustomHTMLElements/CustomHTMLElements.utils.test.tsx