diff --git a/.changeset/tidy-crabs-warn.md b/.changeset/tidy-crabs-warn.md new file mode 100644 index 000000000000..43976ba0a2dc --- /dev/null +++ b/.changeset/tidy-crabs-warn.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Properly handle empty markdown files in content collections \ No newline at end of file diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index f6b420acd774..e80b19872f38 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -32,7 +32,7 @@ export const contentConfigParser = z.object({ export type CollectionConfig = z.infer; export type ContentConfig = z.infer; -type EntryInternal = { rawData: string; filePath: string }; +type EntryInternal = { rawData: string | undefined; filePath: string }; export type EntryInfo = { id: string; @@ -222,7 +222,8 @@ function hasUnderscoreBelowContentDirectoryPath( return false; } -function getFrontmatterErrorLine(rawFrontmatter: string, frontmatterKey: string) { +function getFrontmatterErrorLine(rawFrontmatter: string | undefined, frontmatterKey: string) { + if (!rawFrontmatter) return 0; const indexOfFrontmatterKey = rawFrontmatter.indexOf(`\n${frontmatterKey}`); if (indexOfFrontmatterKey === -1) return 0; diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js index 643f5858ffd9..10c65b0dd20f 100644 --- a/packages/astro/test/content-collections.test.js +++ b/packages/astro/test/content-collections.test.js @@ -215,6 +215,21 @@ describe('Content Collections', () => { }); }); + describe('With empty markdown file', () => { + it('Throws the right error', async () => { + const fixture = await loadFixture({ + root: './fixtures/content-collections-empty-md-file/', + }); + let error; + try { + await fixture.build(); + } catch (e) { + error = e.message; + } + expect(error).to.include('**title**: Required'); + }); + }); + describe('SSR integration', () => { let app; diff --git a/packages/astro/test/fixtures/content-collections-empty-md-file/package.json b/packages/astro/test/fixtures/content-collections-empty-md-file/package.json new file mode 100644 index 000000000000..5e0d91a74df9 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-md-file/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/content-collections-empty-md-file", + "version": "0.0.0", + "private": true, + "type": "module", + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/content-collections-empty-md-file/src/content/blog/empty.md b/packages/astro/test/fixtures/content-collections-empty-md-file/src/content/blog/empty.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/astro/test/fixtures/content-collections-empty-md-file/src/content/config.ts b/packages/astro/test/fixtures/content-collections-empty-md-file/src/content/config.ts new file mode 100644 index 000000000000..991994433870 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-md-file/src/content/config.ts @@ -0,0 +1,11 @@ +import { z, defineCollection } from 'astro:content'; + +const blog = defineCollection({ + schema: z.object({ + title: z.string(), + }), +}); + +export const collections = { + blog, +}; diff --git a/packages/astro/test/fixtures/content-collections-empty-md-file/src/pages/index.astro b/packages/astro/test/fixtures/content-collections-empty-md-file/src/pages/index.astro new file mode 100644 index 000000000000..134d3cffa419 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-md-file/src/pages/index.astro @@ -0,0 +1,6 @@ +--- +import { getEntryBySlug } from 'astro:content'; +const blogEntry = await getEntryBySlug('blog', 'empty'); +--- + +{blogEntry.data.title} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 380705f8142c..cce9c363d957 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1835,6 +1835,12 @@ importers: '@astrojs/mdx': link:../../../../integrations/mdx astro: link:../../.. + packages/astro/test/fixtures/content-collections-empty-md-file: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/content-collections-with-config-mjs: specifiers: astro: workspace:*