Files
supabase/apps/docs/internals/helpers.ts
Jeremias Menichelli e0e6a01905 chore: Remove custom ids from md headings (#52230)
## Problem

Markdown for agents hold the custom IDs, which leak into search
embeddings.

<img width="701" height="231" alt="Screenshot 2026-10-23 at 15 07 17"
src="https://github.com/user-attachments/assets/1e79e282-b954-4bf5-850c-8f0074e5b094"
/>


## Solution

Update markdown generation scripts to remove them.

<img width="399" height="297" alt="Screenshot 2026-10-23 at 15 07 11"
src="https://github.com/user-attachments/assets/af5183b8-8585-450b-a624-9127e4babfe8"
/>


<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions

1. Open docs preview link.
1. Go to a page that has a custom id in their headings
1. Open the corresponding markdown file by adding .md extension to the
URL
1. Markdown version of the page should have deleted custom IDs in
headings.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-10-26 11:04:54 +01:00

35 lines
1.1 KiB
TypeScript

import type { Root } from 'mdast'
import { visit } from 'unist-util-visit'
/**
* Drops the custom anchor from headings like `## My heading [#my-anchor]`. The
* anchor only matters to the rendered page; left in, it leaks into the exported
* markdown and from there into search embeddings.
*/
export function stripHeadingCustomIds(tree: Root): void {
visit(tree, 'heading', (node) => {
const last = node.children.at(-1)
if (last?.type !== 'text') return
const text = last.value.trimEnd()
const anchorStart = text.lastIndexOf('[#')
if (anchorStart === -1 || !text.endsWith(']')) return
last.value = text.slice(0, anchorStart).trimEnd()
})
}
export function flattenSections(sections: any[]): any[] {
let a: any[] = []
for (let i = 0; i < sections.length; i++) {
if (sections[i].id) {
// only push a section that has an id
// these are reserved for sidebar subtitles
a.push(sections[i])
}
if (sections[i].items) {
// if there are subitems, loop through
a = a.concat(flattenSections(sections[i].items))
}
}
return a
}