From 01adddb800bfc03d557cb0cb5e58d1e6a0cc1834 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Wed, 22 Feb 2023 16:20:26 +0000 Subject: [PATCH 1/5] test: add test fixture --- .../package.json | 9 +++++++++ .../src/content/blog/introduction.md | 5 +++++ .../src/pages/index.astro | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json create mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md create mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/pages/index.astro diff --git a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json new file mode 100644 index 000000000000..bf013ff70def --- /dev/null +++ b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/content-collections-underscore-above-content-dir", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/mdx": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md new file mode 100644 index 000000000000..70a92aeab0df --- /dev/null +++ b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md @@ -0,0 +1,5 @@ +--- +title: 'Introduction' +--- + +# Hi there diff --git a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/pages/index.astro b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/pages/index.astro new file mode 100644 index 000000000000..84a1735138ab --- /dev/null +++ b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/pages/index.astro @@ -0,0 +1,18 @@ +--- +import {getEntryBySlug} from "astro:content"; + +const blogEntry = await getEntryBySlug("blog", "introduction"); +const {Content} = blogEntry.render() +--- + + + + + It's content time! + + +
+ +
+ + From 60c79731e07c132c2d41159f4d9f2952be62d5d6 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Wed, 22 Feb 2023 16:20:58 +0000 Subject: [PATCH 2/5] test: add cc config file --- .../src/content/config.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/config.ts diff --git a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/config.ts b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/config.ts new file mode 100644 index 000000000000..991994433870 --- /dev/null +++ b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/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, +}; From 3cf32efac58b0944a673d5e150285657230e0d1b Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Wed, 22 Feb 2023 18:24:24 +0000 Subject: [PATCH 3/5] migrate to unit test instead --- .../package.json | 9 -- .../src/content/blog/introduction.md | 5 - .../src/content/config.ts | 11 -- .../src/pages/index.astro | 18 --- .../get-entry-type.test.js | 119 ++++++++++-------- 5 files changed, 69 insertions(+), 93 deletions(-) delete mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json delete mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md delete mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/config.ts delete mode 100644 packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/pages/index.astro diff --git a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json deleted file mode 100644 index bf013ff70def..000000000000 --- a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "@test/content-collections-underscore-above-content-dir", - "version": "0.0.0", - "private": true, - "dependencies": { - "astro": "workspace:*", - "@astrojs/mdx": "workspace:*" - } -} diff --git a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md deleted file mode 100644 index 70a92aeab0df..000000000000 --- a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/blog/introduction.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -title: 'Introduction' ---- - -# Hi there diff --git a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/config.ts b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/config.ts deleted file mode 100644 index 991994433870..000000000000 --- a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/content/config.ts +++ /dev/null @@ -1,11 +0,0 @@ -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-underscore-above-content-dir/src/pages/index.astro b/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/pages/index.astro deleted file mode 100644 index 84a1735138ab..000000000000 --- a/packages/astro/test/fixtures/_content-collections-underscore-above-content-dir/src/pages/index.astro +++ /dev/null @@ -1,18 +0,0 @@ ---- -import {getEntryBySlug} from "astro:content"; - -const blogEntry = await getEntryBySlug("blog", "introduction"); -const {Content} = blogEntry.render() ---- - - - - - It's content time! - - -
- -
- - diff --git a/packages/astro/test/units/content-collections/get-entry-type.test.js b/packages/astro/test/units/content-collections/get-entry-type.test.js index b9293d22d5e2..3e549c2a2ff3 100644 --- a/packages/astro/test/units/content-collections/get-entry-type.test.js +++ b/packages/astro/test/units/content-collections/get-entry-type.test.js @@ -2,64 +2,83 @@ import { getEntryType } from '../../../dist/content/utils.js'; import { expect } from 'chai'; import { fileURLToPath } from 'node:url'; -describe('Content Collections - getEntryType', () => { - const contentDir = new URL('src/content/', import.meta.url); - const contentPaths = { - config: { - url: new URL('src/content/config.ts', import.meta.url), - exists: true, +const fixtures = [ + { + title: 'Without any underscore above the content directory tree', + contentPaths: { + config: { + url: new URL('src/content/config.ts', import.meta.url), + exists: true, + }, + contentDir: new URL('src/content/', import.meta.url), + }, + }, + { + title: 'With underscore levels above the content directory tree', + contentPaths: { + config: { + url: new URL('_src/content/config.ts', import.meta.url), + exists: true, + }, + contentDir: new URL('_src/content/', import.meta.url), }, - }; + }, +]; - it('Returns "content" for Markdown files', () => { - for (const entryPath of ['blog/first-post.md', 'blog/first-post.mdx']) { - const entry = fileURLToPath(new URL(entryPath, contentDir)); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('content'); - } - }); +describe('Content Collections - getEntryType', () => { + fixtures.forEach(({ title, contentPaths }) => { + describe(title, () => { + it('Returns "content" for Markdown files', () => { + for (const entryPath of ['blog/first-post.md', 'blog/first-post.mdx']) { + const entry = fileURLToPath(new URL(entryPath, contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('content'); + } + }); - it('Returns "content" for Markdown files in nested directories', () => { - for (const entryPath of ['blog/2021/01/01/index.md', 'blog/2021/01/01/index.mdx']) { - const entry = fileURLToPath(new URL(entryPath, contentDir)); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('content'); - } - }); + it('Returns "content" for Markdown files in nested directories', () => { + for (const entryPath of ['blog/2021/01/01/index.md', 'blog/2021/01/01/index.mdx']) { + const entry = fileURLToPath(new URL(entryPath, contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('content'); + } + }); - it('Returns "config" for config files', () => { - const entry = fileURLToPath(contentPaths.config.url); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('config'); - }); + it('Returns "config" for config files', () => { + const entry = fileURLToPath(contentPaths.config.url); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('config'); + }); - it('Returns "unsupported" for non-Markdown files', () => { - const entry = fileURLToPath(new URL('blog/robots.txt', contentDir)); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('unsupported'); - }); + it('Returns "unsupported" for non-Markdown files', () => { + const entry = fileURLToPath(new URL('blog/robots.txt', contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('unsupported'); + }); - it('Returns "ignored" for .DS_Store', () => { - const entry = fileURLToPath(new URL('blog/.DS_Store', contentDir)); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('ignored'); - }); + it('Returns "ignored" for .DS_Store', () => { + const entry = fileURLToPath(new URL('blog/.DS_Store', contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('ignored'); + }); - it('Returns "ignored" for unsupported files using an underscore', () => { - const entry = fileURLToPath(new URL('blog/_draft-robots.txt', contentDir)); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('ignored'); - }); + it('Returns "ignored" for unsupported files using an underscore', () => { + const entry = fileURLToPath(new URL('blog/_draft-robots.txt', contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('ignored'); + }); - it('Returns "ignored" when using underscore on file name', () => { - const entry = fileURLToPath(new URL('blog/_first-post.md', contentDir)); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('ignored'); - }); + it('Returns "ignored" when using underscore on file name', () => { + const entry = fileURLToPath(new URL('blog/_first-post.md', contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('ignored'); + }); - it('Returns "ignored" when using underscore on directory name', () => { - const entry = fileURLToPath(new URL('blog/_draft/first-post.md', contentDir)); - const type = getEntryType(entry, contentPaths); - expect(type).to.equal('ignored'); + it('Returns "ignored" when using underscore on directory name', () => { + const entry = fileURLToPath(new URL('blog/_draft/first-post.md', contentPaths.contentDir)); + const type = getEntryType(entry, contentPaths); + expect(type).to.equal('ignored'); + }); + }); }); }); From 445d9277e891bf971451399b451ef52eedb6f32d Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Wed, 22 Feb 2023 18:29:16 +0000 Subject: [PATCH 4/5] fix: only include child folders in ignore list --- packages/astro/src/content/utils.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index 70771f2dcd72..afe15eeb0077 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -160,12 +160,12 @@ export function getEntryInfo({ export function getEntryType( entryPath: string, - paths: Pick + paths: Pick ): 'content' | 'config' | 'ignored' | 'unsupported' { const { ext, base } = path.parse(entryPath); const fileUrl = pathToFileURL(entryPath); - if (hasUnderscoreInPath(fileUrl) || isOnIgnoreList(base)) { + if (hasUnderscoreBelowContentDirectoryPath(fileUrl, paths.contentDir) || isOnIgnoreList(base)) { return 'ignored'; } else if ((contentFileExts as readonly string[]).includes(ext)) { return 'content'; @@ -180,8 +180,11 @@ function isOnIgnoreList(fileName: string) { return ['.DS_Store'].includes(fileName); } -function hasUnderscoreInPath(fileUrl: URL): boolean { - const parts = fileUrl.pathname.split('/'); +function hasUnderscoreBelowContentDirectoryPath( + fileUrl: URL, + contentDir: ContentPaths['contentDir'] +): boolean { + const parts = fileUrl.pathname.replace(contentDir.pathname, '').split('/'); for (const part of parts) { if (part.startsWith('_')) return true; } From 8ad55e10b70476db248348d5827a0586611d9fec Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Wed, 22 Feb 2023 23:00:23 +0000 Subject: [PATCH 5/5] chore: changeset --- .changeset/tiny-geckos-reply.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tiny-geckos-reply.md diff --git a/.changeset/tiny-geckos-reply.md b/.changeset/tiny-geckos-reply.md new file mode 100644 index 000000000000..c9b7917d0c47 --- /dev/null +++ b/.changeset/tiny-geckos-reply.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Ensure prefixed underscore ignores only child paths of the content directory.