Skip to content

Commit

Permalink
Fix error in empty markdown files with content (#6572)
Browse files Browse the repository at this point in the history
* test: add test fixture

* test: add test case

* 🤏 fix

* chore: changeset

---------

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
MoustaphaDev and natemoo-re authored Mar 17, 2023
1 parent 66858f1 commit fa132e3
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-crabs-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Properly handle empty markdown files in content collections
5 changes: 3 additions & 2 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const contentConfigParser = z.object({
export type CollectionConfig = z.infer<typeof collectionConfigParser>;
export type ContentConfig = z.infer<typeof contentConfigParser>;

type EntryInternal = { rawData: string; filePath: string };
type EntryInternal = { rawData: string | undefined; filePath: string };

export type EntryInfo = {
id: string;
Expand Down Expand Up @@ -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;

Expand Down
15 changes: 15 additions & 0 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/content-collections-empty-md-file",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"astro": "workspace:*"
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z, defineCollection } from 'astro:content';

const blog = defineCollection({
schema: z.object({
title: z.string(),
}),
});

export const collections = {
blog,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
import { getEntryBySlug } from 'astro:content';
const blogEntry = await getEntryBySlug('blog', 'empty');
---

{blogEntry.data.title}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa132e3

Please sign in to comment.