---
import { render, type CollectionEntry } from 'astro:content';
import DocsLayout from '../layouts/DocsLayout.astro';
import { docSlug, titleText } from '../consts';
import { docStaticPaths } from '../lib/raw-markdown';

export const getStaticPaths = docStaticPaths;

type Props = { doc: CollectionEntry<'docs'> };
const { doc } = Astro.props as Props;
const { Content, headings: mdHeadings } = await render(doc);

const slug = docSlug(doc.id);
const rel = (doc as unknown as { filePath?: string }).filePath
  ?.split('/src/content/docs/')
  .pop();
const sourcePath = rel ?? `${doc.id}.md`;

// Title may contain *asterisks* marking coral accent segments
// (see src/consts.ts titleMarkup); plain metadata strips them.
const rawTitle: string =
  typeof doc.data.title === 'string' && doc.data.title.length > 0 ? doc.data.title : slug;
const title = rawTitle;
const metaTitle = titleText(rawTitle);
const description =
  typeof doc.data.description === 'string' ? doc.data.description : undefined;
const introMeta = doc.data.meta;
---

<DocsLayout
  title={metaTitle}
  titleMarkup={title}
  description={description}
  slug={slug}
  headings={mdHeadings}
  sourcePath={sourcePath}
  introMeta={introMeta}
>
  <Content />
</DocsLayout>
