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:
parent
66858f1f23
commit
fa132e35c2
8 changed files with 55 additions and 2 deletions
5
.changeset/tidy-crabs-warn.md
Normal file
5
.changeset/tidy-crabs-warn.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Properly handle empty markdown files in content collections
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
9
packages/astro/test/fixtures/content-collections-empty-md-file/package.json
vendored
Normal file
9
packages/astro/test/fixtures/content-collections-empty-md-file/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "@test/content-collections-empty-md-file",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
0
packages/astro/test/fixtures/content-collections-empty-md-file/src/content/blog/empty.md
vendored
Normal file
0
packages/astro/test/fixtures/content-collections-empty-md-file/src/content/blog/empty.md
vendored
Normal file
11
packages/astro/test/fixtures/content-collections-empty-md-file/src/content/config.ts
vendored
Normal file
11
packages/astro/test/fixtures/content-collections-empty-md-file/src/content/config.ts
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { z, defineCollection } from 'astro:content';
|
||||
|
||||
const blog = defineCollection({
|
||||
schema: z.object({
|
||||
title: z.string(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const collections = {
|
||||
blog,
|
||||
};
|
6
packages/astro/test/fixtures/content-collections-empty-md-file/src/pages/index.astro
vendored
Normal file
6
packages/astro/test/fixtures/content-collections-empty-md-file/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
import { getEntryBySlug } from 'astro:content';
|
||||
const blogEntry = await getEntryBySlug('blog', 'empty');
|
||||
---
|
||||
|
||||
{blogEntry.data.title}
|
|
@ -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:*
|
||||
|
|
Loading…
Reference in a new issue