From 00fc831936779bd72eecc896eed39ebc867d2ecc Mon Sep 17 00:00:00 2001 From: "supabase-vercel-tedd[bot]" <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:01:50 +0000 Subject: [PATCH] feat(kb): add last-updated dates to markdown exports --- apps/kb/scripts/generate-markdown.test.ts | 29 ++++++++++++ apps/kb/scripts/generate-markdown.ts | 55 +++++++++++++++++++---- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/apps/kb/scripts/generate-markdown.test.ts b/apps/kb/scripts/generate-markdown.test.ts index 607f368c605..64c7090d02b 100644 --- a/apps/kb/scripts/generate-markdown.test.ts +++ b/apps/kb/scripts/generate-markdown.test.ts @@ -4,6 +4,7 @@ import { absolutizeLinks, getInternalLinkBaseUrl, parseFrontmatter, + parseLastEdited, renderMarkdown, renderSitemapMarkdown, renderTopicMarkdown, @@ -174,6 +175,34 @@ describe('renderMarkdown', () => { 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('parseLastEdited', () => { + it('reads a YAML date string as that UTC day', () => { + const { data } = parseFrontmatter('---\nlastEdited: 2026-10-23\n---\nBody\n') + + expect(parseLastEdited(data.lastEdited)?.toISOString()).toBe('2026-10-23T00:00:00.000Z') + }) + + it('accepts a Date', () => { + const date = new Date('2026-10-23') + + expect(parseLastEdited(date)).toEqual(date) + }) + + it('returns undefined for a missing or invalid value', () => { + expect(parseLastEdited(undefined)).toBeUndefined() + expect(parseLastEdited(null)).toBeUndefined() + expect(parseLastEdited('not a date')).toBeUndefined() + }) }) describe('renderTopicMarkdown', () => { diff --git a/apps/kb/scripts/generate-markdown.ts b/apps/kb/scripts/generate-markdown.ts index 5022322cbdb..c8d31ddc8d7 100644 --- a/apps/kb/scripts/generate-markdown.ts +++ b/apps/kb/scripts/generate-markdown.ts @@ -13,7 +13,12 @@ import { unified } from 'unified' import YAML from 'yaml' import { BASE_PATH } from '../src/lib/constants.ts' +import { toIsoDay } from '../src/lib/format-date.ts' import { isInTopic, TOPICS, topicToSlug, type Topic } from '../src/lib/topics.ts' +import { + getEntryMetadataBySlug, + type TroubleshootingEntryMetadata, +} from '../src/lib/troubleshooting-db.ts' import { addBaseUrlPrefix, stripHeadingCustomIds } from './helpers.ts' const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url)) @@ -108,16 +113,28 @@ export function absolutizeLinks(body: string): string { } /** - * Renders the simplified export: the frontmatter title as an h1, the - * description as the paragraph beneath it, then the (link-absolutized) - * body. + * Reads the frontmatter `lastEdited` date. The `yaml` parser keeps + * `2026-10-23` as a string (Astro's loader turns it into a Date), so both + * forms are accepted. Undefined when the value is missing or doesn't parse. */ -export function renderMarkdown(data: Frontmatter, body: string): string { +export function parseLastEdited(value: unknown): Date | undefined { + if (!(value instanceof Date) && typeof value !== 'string') return undefined + const date = new Date(value) + return Number.isNaN(date.getTime()) ? undefined : date +} + +/** + * Renders the simplified export: the frontmatter title as an h1, the + * description as the paragraph beneath it, the last-updated date when there + * is one (the same date the page shows), then the (link-absolutized) body. + */ +export function renderMarkdown(data: Frontmatter, body: string, lastUpdated?: Date): string { const heading = data.title ? `# ${data.title}\n\n` : '' const lead = data.description ? `${data.description}\n\n` : '' + const updated = lastUpdated ? `Last updated: ${toIsoDay(lastUpdated)}\n\n` : '' // absolutizeLinks() already normalizes to exactly one trailing newline // (remark-stringify's doing, not ours) — no extra "\n" needed here. - return `${heading}${lead}${absolutizeLinks(body.trim())}` + return `${heading}${lead}${updated}${absolutizeLinks(body.trim())}` } /** @@ -189,6 +206,13 @@ function toMdUrl(relativePath: string): string { const byTitle = (a: Entry, b: Entry) => a.title.localeCompare(b.title) +// The troubleshooting slug is the path under its collection folder without the +// extension, the same id src/content.config.ts gives the entry. +function troubleshootingSlug(relativePath: string): string { + const [, ...rest] = relativePath.split(path.sep) + return toMdExtension(rest.join('/')).slice(0, -'.md'.length) +} + async function findContentFiles(dir: string): Promise { const entries = await readdir(dir, { withFileTypes: true }) const files = await Promise.all( @@ -210,6 +234,16 @@ async function writeFileEnsuringDir(outputPath: string, contents: string) { async function main() { const files = await findContentFiles(CONTENT_DIR) + // Troubleshooting dates live in the database, not the fetched frontmatter, + // same as on src/pages/troubleshooting/[...slug].astro. + const troubleshootingSlugs = files + .map((file) => path.relative(CONTENT_DIR, file)) + .filter((relativePath) => relativePath.split(path.sep)[0] === 'troubleshooting') + .map(troubleshootingSlug) + const troubleshootingMetadata = troubleshootingSlugs.length + ? await getEntryMetadataBySlug(troubleshootingSlugs) + : new Map() + const guidesByTopic = new Map(TOPICS.map((topic) => [topic.name, []])) const entriesByCollection: Record<'guides' | 'faqs' | 'troubleshooting', Entry[]> = { guides: [], @@ -223,12 +257,17 @@ async function main() { const { data, body } = parseFrontmatter(raw) const relativePath = path.relative(CONTENT_DIR, file) - const markdown = renderMarkdown(data, body) + // the first folder under src/content is the collection, e.g. guides, faqs or troubleshooting + const collection = relativePath.split(path.sep)[0] as keyof typeof entriesByCollection + + const lastUpdated = + (collection === 'troubleshooting' + ? troubleshootingMetadata.get(troubleshootingSlug(relativePath))?.dateUpdated + : undefined) ?? parseLastEdited(data.lastEdited) + const markdown = renderMarkdown(data, body, lastUpdated) const outputPath = path.join(OUTPUT_DIR, toMdExtension(relativePath)) await writeFileEnsuringDir(outputPath, markdown) - // the first folder under src/content is the collection, e.g. guides, faqs or troubleshooting - const collection = relativePath.split(path.sep)[0] as keyof typeof entriesByCollection const topics: string[] = Array.isArray(data.topics) ? data.topics : [] const entry: Entry = { title: data.title ?? relativePath, url: toMdUrl(relativePath) } entriesByCollection[collection]?.push(entry)