import { withDocsBasePath } from '~/internals/internal-links' import type { ContentListingGroup } from '~/lib/content-listings.schema' import { filterContentListingItems, getContentListingById, isExternalContentListingHref, } from '~/lib/content-listings.utils' import { getInternalLinkBaseUrl } from '../internal-links' function listingMarkdownHref(href: string, linkBaseUrl: string) { if (href.startsWith('#')) return href if (isExternalContentListingHref(href)) return href return `${linkBaseUrl}${withDocsBasePath(href)}` } const HEADING_MARKDOWN: Record<'h2' | 'h3' | 'h4', string> = { h2: '##', h3: '###', h4: '####', } export function serializeContentListingGroupToMarkdown( group: ContentListingGroup, linkBaseUrl: string ): string { const items = filterContentListingItems(group.items) if (!items.length) return '' const lines: string[] = [] if (group.heading) { const level = group.headingLevel ?? 'h2' lines.push(`${HEADING_MARKDOWN[level]} ${group.heading}`) lines.push('') } if (group.description) { lines.push(group.description) lines.push('') } items.forEach((item, index) => { const marker = group.type === 'progression' ? `${index + 1}.` : '-' const detail = [item.subtitle, item.description].filter(Boolean).join('. ') const label = detail ? `**${item.title}:** ${detail}` : `**${item.title}**` if (!item.href) { lines.push(`${marker} ${label}`) return } const href = listingMarkdownHref(item.href, linkBaseUrl) lines.push( detail ? `${marker} **[${item.title}](${href}):** ${detail}` : `${marker} **[${item.title}](${href})**` ) }) return lines.join('\n') } /** * Markdown export handler for ``. Looks up the * group by id in the same data registry the React component uses. */ export const ContentListings = ({ props }: { props: Record }): string => { const id = typeof props.id === 'string' ? props.id : '' if (!id) return '' const group = getContentListingById(id) if (!group) return '' return serializeContentListingGroupToMarkdown(group, getInternalLinkBaseUrl()) }