Files
supabase/apps/kb/scripts/generate-markdown.test.ts
supabase-vercel-tedd[bot] a63ccbad12 feat(kb): add last-updated dates to markdown exports (#54538)
## Problem

The Knowledge Base HTML pages show an "Updated" date, but their `.md`
exports had no date. Readers and tools that use the markdown versions
couldn't tell how recent a guide, FAQ, or troubleshooting article was.

## Solution

Every Knowledge Base `.md` export now has a `Last updated: YYYY-MM-DD`
line right under its title and description. It uses the date the HTML
page shows. Guides and FAQs take it from the frontmatter `lastEdited`.
Troubleshooting articles take it from the
`troubleshooting_entries.date_updated` lookup the troubleshooting page
already does, and fall back to frontmatter `lastEdited`. A page with no
date gets no line. A failed database lookup logs a warning, and the
export still builds. The export and the troubleshooting lookup share one
date parser in `format-date.ts`.

## How to verify

1. Open a guide export on the preview, for example
`/kb/guides/best-astro-authentication-providers.md`. Below the
description it reads `Last updated: ` with the date from the HTML page's
"Updated" label.
2. Open an FAQ `.md` export from `/kb/sitemap.md` and check that it has
the same line.
3. Open a troubleshooting `.md` export from `/kb/sitemap.md`. If the
HTML page shows an "Updated" date, the export shows the same day.
4. Run `pnpm --filter kb exec vitest run
scripts/generate-markdown.test.ts src/lib/format-date.test.ts`. It
covers the date line and the date parsing.

## Checks

Ran `vitest run scripts/generate-markdown.test.ts
src/lib/format-date.test.ts` in apps/kb (29 passed), `pnpm turbo run
lint --filter=kb`, `tsc --noEmit -p apps/kb` (no errors in the changed
files), and `oxfmt --check` on the changed files. Ran `tsx
scripts/generate-markdown.ts` locally: every guide and FAQ export got
the line, and the sitemap links are unchanged. A sample troubleshooting
file with no database credentials fell back to its frontmatter date. The
database lookup for troubleshooting wasn't run, because the sandbox has
no credentials.

## Checklist

- [x] I have read the [Contributing
Guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] I have added tests for the change
- [ ] I have checked that the change works in the preview deployment

## Feedback for Tedd

To ask Tedd for a change to this PR, reply in Tedd's session on
[DOCS-1838](https://linear.app/supabase/issue/DOCS-1838/add-last-modified-dates-to-md-versions-of-pages).
Tedd doesn't read comments on this PR.

---------

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
2026-12-04 08:10:40 -06:00

234 lines
8.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import {
absolutizeLinks,
getInternalLinkBaseUrl,
parseFrontmatter,
renderMarkdown,
renderSitemapMarkdown,
renderTopicMarkdown,
} from './generate-markdown.ts'
describe('parseFrontmatter', () => {
it('parses title/description out of the YAML frontmatter and returns the rest as body', () => {
const raw = `---\ntitle: 'Sample guide'\ndescription: 'A short summary.'\n---\n\nBody text here.\n`
const { data, body } = parseFrontmatter(raw)
expect(data.title).toBe('Sample guide')
expect(data.description).toBe('A short summary.')
// The blank line separating the closing `---` from the body is kept as-is
// here — renderMarkdown() is what trims it before composing the output.
expect(body).toBe('\nBody text here.\n')
})
it('parses values that would trip up a naive regex (colons and quotes in the text)', () => {
const raw = `---\ntitle: "Note: this has a colon"\ndescription: "Quoted 'inner' text: still one value"\n---\nBody\n`
const { data } = parseFrontmatter(raw)
expect(data.title).toBe('Note: this has a colon')
expect(data.description).toBe("Quoted 'inner' text: still one value")
})
it('returns an empty data object and the raw text untouched when there is no frontmatter', () => {
const raw = 'Just a plain paragraph, no frontmatter.\n'
const { data, body } = parseFrontmatter(raw)
expect(data).toEqual({})
expect(body).toBe(raw)
})
})
describe('getInternalLinkBaseUrl', () => {
const ORIGINAL_ENV = process.env
beforeEach(() => {
process.env = { ...ORIGINAL_ENV }
delete process.env.VERCEL_ENV
delete process.env.VERCEL_URL
})
afterEach(() => {
process.env = ORIGINAL_ENV
})
it('returns the production origin when VERCEL_ENV=production', () => {
process.env.VERCEL_ENV = 'production'
expect(getInternalLinkBaseUrl()).toBe('https://supabase.com')
})
it('returns the deployment URL when VERCEL_ENV=preview', () => {
process.env.VERCEL_ENV = 'preview'
process.env.VERCEL_URL = 'kb-git-fork-supabase.vercel.app'
expect(getInternalLinkBaseUrl()).toBe('https://kb-git-fork-supabase.vercel.app')
})
it('returns empty when preview is set but VERCEL_URL is missing', () => {
process.env.VERCEL_ENV = 'preview'
expect(getInternalLinkBaseUrl()).toBe('')
})
it('returns empty when VERCEL_ENV is not set (local dev/CI)', () => {
expect(getInternalLinkBaseUrl()).toBe('')
})
})
describe('absolutizeLinks', () => {
const ORIGINAL_ENV = process.env
beforeEach(() => {
process.env = { ...ORIGINAL_ENV, VERCEL_ENV: 'production' }
})
afterEach(() => {
process.env = ORIGINAL_ENV
})
it('leaves already-absolute links untouched', () => {
expect(absolutizeLinks('See [the docs](https://example.com/guide) for more.')).toBe(
'See [the docs](https://example.com/guide) for more.\n'
)
})
it('rewrites a root-relative link into a full URL under the kb base path', () => {
expect(absolutizeLinks('See [another guide](/guides/other-guide) for more.')).toBe(
'See [another guide](https://supabase.com/kb/guides/other-guide) for more.\n'
)
})
it('does not double up the base path when a link already includes it', () => {
expect(absolutizeLinks('[another guide](/kb/guides/other-guide)')).toBe(
'[another guide](https://supabase.com/kb/guides/other-guide)\n'
)
})
it('leaves the link relative (base path only, no origin) outside of Vercel', () => {
process.env.VERCEL_ENV = undefined
expect(absolutizeLinks('[another guide](/guides/other-guide)')).toBe(
'[another guide](/kb/guides/other-guide)\n'
)
})
it('does not rewrite image URLs', () => {
expect(absolutizeLinks('![alt](/img.png)')).toBe('![alt](/img.png)\n')
})
it('skips link-like text inside fenced code blocks', () => {
expect(absolutizeLinks('```\n[x](/x)\n```\n\n[y](/y)')).toBe(
'```\n[x](/x)\n```\n\n[y](https://supabase.com/kb/y)\n'
)
})
it('leaves a root-relative URL inside a fenced code block untouched, even outside link syntax', () => {
const body = '```bash\ncurl -X GET /guides/foo\n```'
expect(absolutizeLinks(body)).toBe('```bash\ncurl -X GET /guides/foo\n```\n')
})
it('leaves a root-relative URL inside a fenced markdown code block untouched', () => {
const body = '```md\n[Guides](/guides/foo)\n```'
expect(absolutizeLinks(body)).toBe('```md\n[Guides](/guides/foo)\n```\n')
})
it('leaves a root-relative URL inside an inline code span untouched', () => {
expect(absolutizeLinks('Run `GET /guides/foo` to fetch it.')).toBe(
'Run `GET /guides/foo` to fetch it.\n'
)
})
it('round-trips GFM tables and strikethrough without mangling them', () => {
const body = '| a | b |\n| - | - |\n| 1 | 2 |\n\n~~gone~~'
expect(absolutizeLinks(body)).toBe('| a | b |\n| - | - |\n| 1 | 2 |\n\n~~gone~~\n')
})
it('strips a custom anchor from the end of a heading', () => {
expect(absolutizeLinks('## How to create a client [#create-client]')).toBe(
'## How to create a client\n'
)
})
it('strips a custom anchor that follows inline code in a heading', () => {
expect(absolutizeLinks('## Using `createClient` [#create-client]')).toBe(
'## Using `createClient`\n'
)
})
it('leaves bracketed text that is not a trailing heading anchor untouched', () => {
const body = '## Arrays [#1] explained\n\nSee [#create-client] below.'
expect(absolutizeLinks(body)).toBe(
'## Arrays \\[#1] explained\n\nSee \\[#create-client] below.\n'
)
})
})
describe('renderMarkdown', () => {
it('turns the title into an h1 and the description into the paragraph beneath it', () => {
const data = { title: 'Sample guide', description: 'A short summary.' }
expect(renderMarkdown(data, 'Body text.')).toBe(
'# Sample guide\n\nA short summary.\n\nBody text.\n'
)
})
it('skips the heading/lead lines gracefully when title or description are missing', () => {
expect(renderMarkdown({}, 'Body text.')).toBe('Body text.\n')
})
it('puts the last-updated date beneath the title and description', () => {
const data = { title: 'Sample guide', description: 'A short summary.' }
expect(renderMarkdown(data, 'Body text.', new Date('2026-10-23'))).toBe(
'# Sample guide\n\nA short summary.\n\nLast updated: 2026-10-23\n\nBody text.\n'
)
})
})
describe('renderTopicMarkdown', () => {
const topic = { name: 'Tutorial', description: 'Step-by-step walkthroughs.' }
it('renders the topic as an h1/description followed by a bullet list of its guides', () => {
const guides = [
{ title: 'Sample guide', url: 'https://supabase.com/kb/guides/sample-guide.md' },
]
expect(renderTopicMarkdown(topic, guides)).toBe(
'# Tutorial\n\nStep-by-step walkthroughs.\n\n- [Sample guide](https://supabase.com/kb/guides/sample-guide.md)\n'
)
})
it('falls back to "No guides for this topic" when the list is empty', () => {
expect(renderTopicMarkdown(topic, [])).toBe(
'# Tutorial\n\nStep-by-step walkthroughs.\n\nNo guides for this topic\n'
)
})
})
describe('renderSitemapMarkdown', () => {
it('renders the site title and description, then linked lists for topics, guides, FAQs and troubleshooting', () => {
const markdown = renderSitemapMarkdown({
topics: [{ title: 'Auth', url: 'https://supabase.com/kb/topics/auth.md' }],
guides: [{ title: 'Sample guide', url: 'https://supabase.com/kb/guides/sample-guide.md' }],
faqs: [{ title: 'Sample FAQ', url: 'https://supabase.com/kb/faqs/auth/sample-faq.md' }],
troubleshooting: [
{ title: 'Permission denied', url: 'https://supabase.com/kb/troubleshooting/denied.md' },
],
})
expect(markdown).toBe(
[
'# Supabase Knowledge Base',
'In-depth guides, tutorials, troubleshooting, and explainers for best practices for Supabase',
'## Topics',
'- [Auth](https://supabase.com/kb/topics/auth.md)',
'## Guides',
'- [Sample guide](https://supabase.com/kb/guides/sample-guide.md)',
'## FAQs',
'- [Sample FAQ](https://supabase.com/kb/faqs/auth/sample-faq.md)',
'## Troubleshooting',
'- [Permission denied](https://supabase.com/kb/troubleshooting/denied.md)',
].join('\n\n') + '\n'
)
})
})