Skip to content

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

4.0 KB
Raw Blame
import { defineCollection } from 'astro:content'
import { glob } from 'astro/loaders'
import { z } from 'astro/zod'

import { TOPIC_NAMES } from './lib/topics'

// Format date as "September 26, 2026"
const formatDate = (date: Date): string => {
  // Adjust for timezone offset to preserve the intended date
  const adjusted = new Date(date.getTime() + date.getTimezoneOffset() * 60000)
  return new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
  }).format(adjusted)
}

// Astro's default id is a lowercased slug of the file path, but the markdown
// export (scripts/generate-markdown.ts) keeps the filename's case and Vercel
// serves static files case-sensitively. Keeping the case here makes the page
// URL and the `.md` export path line up.
const generateId = ({ entry }: { entry: string }) => entry.replace(/\.mdx?$/, '')

// Every entry here is rendered through GuideLayout by
// Shared by every article-like collection (guides, troubleshooting) so their
// schemas can't drift on these fields — both render through ArticleLayout via
// a matching catch-all page and need the same base fields. `description` and
// `topics` are NOT shared: each collection validates those differently below.
const articleFields = {
  title: z.string(),
  // Surfaces the guide in the homepage's "Featured Guides" section.
  pinned: z.boolean().default(false),
  github_url: z.string().optional(),
}

// Every entry here is rendered through ArticleLayout by
// src/pages/guides/[...slug].astro — dropping a new file in
// src/content/guides doesn't need any per-file layout wiring.
const guides = defineCollection({
  loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/guides', generateId }),
  schema: z.object({
    ...articleFields,
    description: z.string(),
    // z.enum (not z.string) so a guide referencing a topic outside TOPICS
    // fails content validation instead of silently rendering an orphaned tag.
    topics: z.array(z.enum(TOPIC_NAMES)),
    // Surfaces the guide in the homepage's "Featured Guides" section.
    pinned: z.boolean().default(false),
    github_url: z.string().optional(),
    date: z
      .date()
      .optional()
      .transform((date) => (date ? formatDate(date) : undefined)),
  }),
})

// Fetched from the private supabase/troubleshooting repo by
// scripts/federated-content/fetch-federated-content.ts (a prebuild step) —
// entirely generated, never hand-authored, see .gitignore. Rendered through
// ArticleLayout by src/pages/troubleshooting/[...slug].astro.
const troubleshooting = defineCollection({
  loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/troubleshooting', generateId }),
  schema: z.object({
    ...articleFields,
    // The source has no description field, and ArticleLayout already
    // renders this conditionally — optional rather than synthesized.
    description: z.string().optional(),
    // Passed through verbatim from the source repo's own frontmatter, not
    // validated against TOPIC_NAMES — this collection's topic vocabulary is
    // the source repo's, not kb's, so entries won't all match a kb Topic
    // (ArticleLayout and the topic pages degrade gracefully for those).
    topics: z.array(z.string()).default([]),
  }),
})

// Question-and-answer entries in src/content/faqs. `title` holds the question,
// and the body holds the answer. Entries are not rendered by a page yet — this
// collection only defines the schema so content can be authored against it.
const faqs = defineCollection({
  loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/faqs', generateId }),
  // Every entry is tagged with the FAQs topic in addition to any topics its
  // frontmatter lists, so it appears on /topics/faqs and its product topic pages.
  schema: z
    .object({
      title: z.string(),
      topics: z.array(z.string()).default([]),
    })
    .transform((data) => ({ ...data, topics: [...new Set(['FAQs', ...data.topics])] })),
})

export const collections = { guides, troubleshooting, faqs }