From 205d1f07f13ae60f21954648d530895d479e42cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Seery?= Date: Sun, 24 Apr 2022 20:13:33 -0300 Subject: [PATCH] feat(markdown): Improved types (#3191) * feat(markdown): Improved types * More MarkdownMetadata --- .changeset/cool-experts-shake.md | 5 +++++ packages/astro/env.d.ts | 13 +++++++++++ packages/astro/src/@types/astro.ts | 35 +++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 .changeset/cool-experts-shake.md diff --git a/.changeset/cool-experts-shake.md b/.changeset/cool-experts-shake.md new file mode 100644 index 000000000..f0bbc7e78 --- /dev/null +++ b/.changeset/cool-experts-shake.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Added better types to importing Markdown diff --git a/packages/astro/env.d.ts b/packages/astro/env.d.ts index 54f9b1727..d9dfe741a 100644 --- a/packages/astro/env.d.ts +++ b/packages/astro/env.d.ts @@ -11,3 +11,16 @@ type Astro = import('astro').AstroGlobal; declare const Astro: Readonly; declare const Fragment: any; + +declare module '*.md' { + type MD = import('astro').MarkdownInstance>; + + export const frontmatter: MD['frontmatter']; + export const file: MD['file']; + export const url: MD['url']; + export const getHeaders: MD['getHeaders']; + export const Content: MD['Content']; + + const load: MD['default']; + export default load; +} diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 3047ae1b2..baf4a22e7 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -725,7 +725,13 @@ export interface MarkdownInstance> { file: string; url: string | undefined; Content: AstroComponentFactory; - getHeaders(): Promise<{ depth: number; slug: string; text: string }[]>; + getHeaders(): Promise; + default: () => Promise<{ + metadata: MarkdownMetadata; + frontmatter: MarkdownContent; + $$metadata: Metadata; + default: AstroComponentFactory; + }>; } export type GetHydrateCallback = () => Promise< @@ -767,18 +773,35 @@ export interface ManifestData { routes: RouteData[]; } +export interface MarkdownHeader { + depth: number; + slug: string; + text: string; +} + +export interface MarkdownMetadata { + headers: MarkdownHeader[]; + source: string; + html: string; +} + export interface MarkdownParserResponse { frontmatter: { [key: string]: any; }; - metadata: { - headers: any[]; - source: string; - html: string; - }; + metadata: MarkdownMetadata; code: string; } +/** + * The `content` prop given to a Layout + * https://docs.astro.build/guides/markdown-content/#markdown-layouts + */ +export interface MarkdownContent { + [key: string]: any; + astro: MarkdownMetadata; +} + /** * paginate() Options * Docs: https://docs.astro.build/guides/pagination/#calling-the-paginate-function