summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Happydev <81974850+MoustaphaDev@users.noreply.github.com> 2023-03-17 16:38:10 +0000
committerGravatar GitHub <noreply@github.com> 2023-03-17 11:38:10 -0500
commitfa132e35c23f2cfe368fd0a7239584a2bc5c4f12 (patch)
tree86c4226e13e16b67d0c078229b559d0b5a22d69f
parent66858f1f238a0edf6ded2b0f693bc738785d5aa3 (diff)
downloadastro-fa132e35c23f2cfe368fd0a7239584a2bc5c4f12.tar.gz
astro-fa132e35c23f2cfe368fd0a7239584a2bc5c4f12.tar.zst
astro-fa132e35c23f2cfe368fd0a7239584a2bc5c4f12.zip
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>
-rw-r--r--.changeset/tidy-crabs-warn.md5
-rw-r--r--packages/astro/src/content/utils.ts5
-rw-r--r--packages/astro/test/content-collections.test.js15
-rw-r--r--packages/astro/test/fixtures/content-collections-empty-md-file/package.json9
-rw-r--r--packages/astro/test/fixtures/content-collections-empty-md-file/src/content/blog/empty.md0
-rw-r--r--packages/astro/test/fixtures/content-collections-empty-md-file/src/content/config.ts11
-rw-r--r--packages/astro/test/fixtures/content-collections-empty-md-file/src/pages/index.astro6
-rw-r--r--pnpm-lock.yaml6
8 files changed, 55 insertions, 2 deletions
diff --git a/.changeset/tidy-crabs-warn.md b/.changeset/tidy-crabs-warn.md
new file mode 100644
index 000000000..43976ba0a
--- /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 f6b420acd..e80b19872 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<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;
diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js
index 643f5858f..10c65b0dd 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 000000000..5e0d91a74
--- /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 000000000..e69de29bb
--- /dev/null
+++ b/packages/astro/test/fixtures/content-collections-empty-md-file/src/content/blog/empty.md
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 000000000..991994433
--- /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 000000000..134d3cffa
--- /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 380705f81..cce9c363d 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:*