Fix error in empty markdown files with content (#6572)

* test: add test fixture

* test: add test case

* 🤏 fix

* chore: changeset

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Happydev 2023-03-17 16:38:10 +00:00 committed by GitHub
parent 66858f1f23
commit fa132e35c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Properly handle empty markdown files in content collections

View file

@ -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;
@ -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;

View file

@ -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;

View file

@ -0,0 +1,9 @@
{
"name": "@test/content-collections-empty-md-file",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,11 @@
import { z, defineCollection } from 'astro:content';
const blog = defineCollection({
schema: z.object({
title: z.string(),
}),
});
export const collections = {
blog,
};

View file

@ -0,0 +1,6 @@
---
import { getEntryBySlug } from 'astro:content';
const blogEntry = await getEntryBySlug('blog', 'empty');
---
{blogEntry.data.title}

View file

@ -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:*