console/packages/web/docs/src/components/use-frontmatter.ts
Piotr Monwid-Olechnowicz 4a865c03b2
Hive Blog (#6625)
Co-authored-by: Dotan Simha <dotansimha@users.noreply.github.com>
2025-03-27 16:12:42 +01:00

26 lines
733 B
TypeScript

'use client';
import { useConfig } from '@theguild/components';
/**
* Dima said there's no possible way to access frontmatter imperatively
* from a server component in Nextra, so we have a hook for client components.
*/
export function useFrontmatter<
// this is unsafe, but we're in a blog, and the frontmatter type is
// controlled by us. if it crashes, we can just fix the frontmatter in markdown
TFrontmatter,
>() {
const normalizePagesResult = useConfig().normalizePagesResult;
const frontmatter = normalizePagesResult.activeMetadata as TFrontmatter;
const name = normalizePagesResult.activePath.at(-1)?.name;
if (!name) {
throw new Error('unexpected');
}
return {
frontmatter,
name,
};
}