From 848e97647536483358e08ca7600f770715b0db89 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 1 Oct 2024 13:15:23 +0100 Subject: [PATCH] Handle folders without config --- packages/astro/src/content/loaders/glob.ts | 2 +- packages/astro/src/content/utils.ts | 39 +++++++++++++++++++ packages/astro/src/core/config/schema.ts | 11 ------ .../astro/test/content-collections.test.js | 34 ++++++++++++++++ .../content/.should-also-ignore/enterprise.md | 14 +++++++ .../src/content/_should-ignore/enterprise.md | 14 +++++++ .../src/content/without-config/columbia.md | 15 +++++++ .../src/content/without-config/endeavour.md | 14 +++++++ .../src/content/without-config/enterprise.md | 14 +++++++ .../promo/_launch-week-styles.css | 3 ++ .../without-config/promo/launch week.mdx | 14 +++++++ .../src/pages/collections.json.js | 3 +- 12 files changed, 164 insertions(+), 13 deletions(-) create mode 100644 packages/astro/test/fixtures/content-collections/src/content/.should-also-ignore/enterprise.md create mode 100644 packages/astro/test/fixtures/content-collections/src/content/_should-ignore/enterprise.md create mode 100644 packages/astro/test/fixtures/content-collections/src/content/without-config/columbia.md create mode 100644 packages/astro/test/fixtures/content-collections/src/content/without-config/endeavour.md create mode 100644 packages/astro/test/fixtures/content-collections/src/content/without-config/enterprise.md create mode 100644 packages/astro/test/fixtures/content-collections/src/content/without-config/promo/_launch-week-styles.css create mode 100644 packages/astro/test/fixtures/content-collections/src/content/without-config/promo/launch week.mdx diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index f2b40db0950e..9d8433035f86 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -90,7 +90,7 @@ export function glob(globOptions: GlobOptions): Loader { const untouchedEntries = new Set(store.keys()); const isLegacy = (globOptions as any)._legacy; - // If legacy mode is *not* enabled then we use emulate legacy collections instead + // If global legacy flag is *not* enabled then this loader is used to emulate legacy collections instead const emulateLegacyCollections = !config.legacy.legacyContentCollections; async function syncData(entry: string, base: URL, entryType?: ContentEntryType) { if (!entryType) { diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index abd259bc7ae5..ad67fd2af40c 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -25,6 +25,7 @@ import { } from './consts.js'; import { glob } from './loaders/glob.js'; import { createImage } from './runtime-assets.js'; +import { green } from 'kleur/colors'; /** * Amap from a collection + slug to the local file path. * This is used internally to resolve entry imports when using `getEntry()`. @@ -543,6 +544,7 @@ async function loadContentConfig({ export async function autogenerateCollections({ config, settings, + fs, }: { config?: ContentConfig; settings: AstroSettings; @@ -560,8 +562,10 @@ export async function autogenerateCollections({ const contentPattern = globWithUnderscoresIgnored('', contentExts); const dataPattern = globWithUnderscoresIgnored('', dataExts); + let usesContentLayer = false; for (const collectionName of Object.keys(collections)) { if (collections[collectionName]?.type === 'content_layer') { + usesContentLayer = true; // This is already a content layer, skip continue; } @@ -592,6 +596,41 @@ export async function autogenerateCollections({ }) as any, }; } + if (!usesContentLayer) { + // If the user hasn't defined any collections using the content layer, we'll try and help out by checking for + // any orphaned folders in the content directory and creating collections for them. + const orphanedCollections = []; + for (const entry of await fs.promises.readdir(contentDir, { withFileTypes: true })) { + if (['_', '.'].includes(entry.name.at(0) ?? '')) { + continue; + } + if (entry.isDirectory()) { + const collectionName = entry.name; + if (!collections[collectionName]) { + orphanedCollections.push(collectionName); + const base = new URL(`${collectionName}/`, contentDir); + collections[collectionName] = { + type: 'content_layer', + loader: glob({ + base, + pattern: contentPattern, + _legacy: true, + }) as any, + }; + } + } + } + if (orphanedCollections.length > 0) { + console.warn( + ` +Auto-generating collections for folders in the content directory that are not defined as collections. +This is deprecated, so you should define these collections yourself in "src/content/config.ts". +The following collections have been auto-generated: ${orphanedCollections + .map((name) => green(name)) + .join(', ')}\n`, + ); + } + } return { ...config, collections }; } diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 551f29387606..72c632e6a35d 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -701,17 +701,6 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) { }); } } - - if ( - configuration.experimental.contentCollectionCache && - !configuration.legacy.legacyContentCollections - ) { - ctx.addIssue({ - code: z.ZodIssueCode.custom, - message: - 'Content collections cache is not supported with the Content Layer API. Please remove the `experimental.contentCollectionsCache` option from your Astro config or enable `legacy.legacyContentCollections`', - }); - } }); return AstroConfigRelativeSchema; diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js index 6c452d8499c5..19a47f7e4cc6 100644 --- a/packages/astro/test/content-collections.test.js +++ b/packages/astro/test/content-collections.test.js @@ -21,6 +21,40 @@ describe('Content Collections', () => { json = devalue.parse(rawJson); }); + it('Returns `without config` collection', async () => { + assert.ok(json.hasOwnProperty('withoutConfig')); + assert.equal(Array.isArray(json.withoutConfig), true); + + const ids = json.withoutConfig.map((item) => item.id); + assert.deepEqual( + ids.sort(), + [ + 'columbia.md', + 'endeavour.md', + 'enterprise.md', + // Spaces allowed in IDs + 'promo/launch week.mdx', + ].sort(), + ); + }); + + it('Handles spaces in `without config` slugs', async () => { + assert.ok(json.hasOwnProperty('withoutConfig')); + assert.equal(Array.isArray(json.withoutConfig), true); + + const slugs = json.withoutConfig.map((item) => item.slug); + assert.deepEqual( + slugs.sort(), + [ + 'columbia', + 'endeavour', + 'enterprise', + // "launch week.mdx" is converted to "launch-week.mdx" + 'promo/launch-week', + ].sort(), + ); + }); + it('Returns `with schema` collection', async () => { assert.ok(json.hasOwnProperty('withSchemaConfig')); assert.equal(Array.isArray(json.withSchemaConfig), true); diff --git a/packages/astro/test/fixtures/content-collections/src/content/.should-also-ignore/enterprise.md b/packages/astro/test/fixtures/content-collections/src/content/.should-also-ignore/enterprise.md new file mode 100644 index 000000000000..3131e6d5df3a --- /dev/null +++ b/packages/astro/test/fixtures/content-collections/src/content/.should-also-ignore/enterprise.md @@ -0,0 +1,14 @@ +--- +title: 'Enterprise' +description: 'Learn about the Enterprise NASA space shuttle.' +publishedDate: 'Tue Jun 08 2021 00:00:00 GMT-0400 (Eastern Daylight Time)' +tags: [space, 70s] +--- + +**Source:** [Wikipedia](https://en.wikipedia.org/wiki/Space_Shuttle_Enterprise) + +Space Shuttle Enterprise (Orbiter Vehicle Designation: OV-101) was the first orbiter of the Space Shuttle system. Rolled out on September 17, 1976, it was built for NASA as part of the Space Shuttle program to perform atmospheric test flights after being launched from a modified Boeing 747. It was constructed without engines or a functional heat shield. As a result, it was not capable of spaceflight. + +Originally, Enterprise had been intended to be refitted for orbital flight to become the second space-rated orbiter in service. However, during the construction of Space Shuttle Columbia, details of the final design changed, making it simpler and less costly to build Challenger around a body frame that had been built as a test article. Similarly, Enterprise was considered for refit to replace Challenger after the latter was destroyed, but Endeavour was built from structural spares instead. + +Enterprise was restored and placed on display in 2003 at the Smithsonian's new Steven F. Udvar-Hazy Center in Virginia. Following the retirement of the Space Shuttle fleet, Discovery replaced Enterprise at the Udvar-Hazy Center, and Enterprise was transferred to the Intrepid Sea, Air & Space Museum in New York City, where it has been on display since July 2012. diff --git a/packages/astro/test/fixtures/content-collections/src/content/_should-ignore/enterprise.md b/packages/astro/test/fixtures/content-collections/src/content/_should-ignore/enterprise.md new file mode 100644 index 000000000000..3131e6d5df3a --- /dev/null +++ b/packages/astro/test/fixtures/content-collections/src/content/_should-ignore/enterprise.md @@ -0,0 +1,14 @@ +--- +title: 'Enterprise' +description: 'Learn about the Enterprise NASA space shuttle.' +publishedDate: 'Tue Jun 08 2021 00:00:00 GMT-0400 (Eastern Daylight Time)' +tags: [space, 70s] +--- + +**Source:** [Wikipedia](https://en.wikipedia.org/wiki/Space_Shuttle_Enterprise) + +Space Shuttle Enterprise (Orbiter Vehicle Designation: OV-101) was the first orbiter of the Space Shuttle system. Rolled out on September 17, 1976, it was built for NASA as part of the Space Shuttle program to perform atmospheric test flights after being launched from a modified Boeing 747. It was constructed without engines or a functional heat shield. As a result, it was not capable of spaceflight. + +Originally, Enterprise had been intended to be refitted for orbital flight to become the second space-rated orbiter in service. However, during the construction of Space Shuttle Columbia, details of the final design changed, making it simpler and less costly to build Challenger around a body frame that had been built as a test article. Similarly, Enterprise was considered for refit to replace Challenger after the latter was destroyed, but Endeavour was built from structural spares instead. + +Enterprise was restored and placed on display in 2003 at the Smithsonian's new Steven F. Udvar-Hazy Center in Virginia. Following the retirement of the Space Shuttle fleet, Discovery replaced Enterprise at the Udvar-Hazy Center, and Enterprise was transferred to the Intrepid Sea, Air & Space Museum in New York City, where it has been on display since July 2012. diff --git a/packages/astro/test/fixtures/content-collections/src/content/without-config/columbia.md b/packages/astro/test/fixtures/content-collections/src/content/without-config/columbia.md new file mode 100644 index 000000000000..4971108e36ed --- /dev/null +++ b/packages/astro/test/fixtures/content-collections/src/content/without-config/columbia.md @@ -0,0 +1,15 @@ +--- +title: Columbia +description: 'Learn about the Columbia NASA space shuttle.' +publishedDate: 'Sat May 21 2022 00:00:00 GMT-0400 (Eastern Daylight Time)' +tags: [space, 90s] +--- + +**Source:** [Wikipedia](https://en.wikipedia.org/wiki/Space_Shuttle_Endeavour) + +Space Shuttle Endeavour (Orbiter Vehicle Designation: OV-105) is a retired orbiter from NASA's Space Shuttle program and the fifth and final operational Shuttle built. It embarked on its first mission, STS-49, in May 1992 and its 25th and final mission, STS-134, in May 2011. STS-134 was expected to be the final mission of the Space Shuttle program, but with the authorization of STS-135, Atlantis became the last shuttle to fly. + +The United States Congress approved the construction of Endeavour in 1987 to replace the Space Shuttle Challenger, which was destroyed in 1986. + +NASA chose, on cost grounds, to build much of Endeavour from spare parts rather than refitting the Space Shuttle Enterprise, and used structural spares built during the construction of Discovery and Atlantis in its assembly. +Space Shuttle Endeavour (Orbiter Vehicle Designation: OV-105) is a retired orbiter from NASA's Space Shuttle program and the fifth and final operational Shuttle built. It embarked on its first mission, STS-49, in May 1992 and its 25th and final mission, STS-134, in May 2011. STS-134 was expected to be the final mission of the Space Shuttle program, but with the authorization of STS-135, Atlantis became the last shuttle to fly. diff --git a/packages/astro/test/fixtures/content-collections/src/content/without-config/endeavour.md b/packages/astro/test/fixtures/content-collections/src/content/without-config/endeavour.md new file mode 100644 index 000000000000..51d6e8c42178 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections/src/content/without-config/endeavour.md @@ -0,0 +1,14 @@ +--- +title: Endeavour +description: 'Learn about the Endeavour NASA space shuttle.' +publishedDate: 'Sun Jul 11 2021 00:00:00 GMT-0400 (Eastern Daylight Time)' +tags: [space, 90s] +--- + +**Source:** [Wikipedia](https://en.wikipedia.org/wiki/Space_Shuttle_Endeavour) + +Space Shuttle Endeavour (Orbiter Vehicle Designation: OV-105) is a retired orbiter from NASA's Space Shuttle program and the fifth and final operational Shuttle built. It embarked on its first mission, STS-49, in May 1992 and its 25th and final mission, STS-134, in May 2011. STS-134 was expected to be the final mission of the Space Shuttle program, but with the authorization of STS-135, Atlantis became the last shuttle to fly. + +The United States Congress approved the construction of Endeavour in 1987 to replace the Space Shuttle Challenger, which was destroyed in 1986. + +NASA chose, on cost grounds, to build much of Endeavour from spare parts rather than refitting the Space Shuttle Enterprise, and used structural spares built during the construction of Discovery and Atlantis in its assembly. diff --git a/packages/astro/test/fixtures/content-collections/src/content/without-config/enterprise.md b/packages/astro/test/fixtures/content-collections/src/content/without-config/enterprise.md new file mode 100644 index 000000000000..3131e6d5df3a --- /dev/null +++ b/packages/astro/test/fixtures/content-collections/src/content/without-config/enterprise.md @@ -0,0 +1,14 @@ +--- +title: 'Enterprise' +description: 'Learn about the Enterprise NASA space shuttle.' +publishedDate: 'Tue Jun 08 2021 00:00:00 GMT-0400 (Eastern Daylight Time)' +tags: [space, 70s] +--- + +**Source:** [Wikipedia](https://en.wikipedia.org/wiki/Space_Shuttle_Enterprise) + +Space Shuttle Enterprise (Orbiter Vehicle Designation: OV-101) was the first orbiter of the Space Shuttle system. Rolled out on September 17, 1976, it was built for NASA as part of the Space Shuttle program to perform atmospheric test flights after being launched from a modified Boeing 747. It was constructed without engines or a functional heat shield. As a result, it was not capable of spaceflight. + +Originally, Enterprise had been intended to be refitted for orbital flight to become the second space-rated orbiter in service. However, during the construction of Space Shuttle Columbia, details of the final design changed, making it simpler and less costly to build Challenger around a body frame that had been built as a test article. Similarly, Enterprise was considered for refit to replace Challenger after the latter was destroyed, but Endeavour was built from structural spares instead. + +Enterprise was restored and placed on display in 2003 at the Smithsonian's new Steven F. Udvar-Hazy Center in Virginia. Following the retirement of the Space Shuttle fleet, Discovery replaced Enterprise at the Udvar-Hazy Center, and Enterprise was transferred to the Intrepid Sea, Air & Space Museum in New York City, where it has been on display since July 2012. diff --git a/packages/astro/test/fixtures/content-collections/src/content/without-config/promo/_launch-week-styles.css b/packages/astro/test/fixtures/content-collections/src/content/without-config/promo/_launch-week-styles.css new file mode 100644 index 000000000000..cce2effe20c1 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections/src/content/without-config/promo/_launch-week-styles.css @@ -0,0 +1,3 @@ +body { + font-family: 'Comic Sans MS', sans-serif; +} diff --git a/packages/astro/test/fixtures/content-collections/src/content/without-config/promo/launch week.mdx b/packages/astro/test/fixtures/content-collections/src/content/without-config/promo/launch week.mdx new file mode 100644 index 000000000000..22ed07c43f0c --- /dev/null +++ b/packages/astro/test/fixtures/content-collections/src/content/without-config/promo/launch week.mdx @@ -0,0 +1,14 @@ +--- +title: 'Launch week!' +description: 'Join us for the exciting launch of SPACE BLOG' +publishedDate: 'Sat May 21 2022 00:00:00 GMT-0400 (Eastern Daylight Time)' +tags: ['announcement'] +--- + +import './_launch-week-styles.css'; + +Join us for the space blog launch! + +- THIS THURSDAY +- Houston, TX +- Dress code: **interstellar casual** ✨ diff --git a/packages/astro/test/fixtures/content-collections/src/pages/collections.json.js b/packages/astro/test/fixtures/content-collections/src/pages/collections.json.js index 2855b24ceb8d..0694cfaeca99 100644 --- a/packages/astro/test/fixtures/content-collections/src/pages/collections.json.js +++ b/packages/astro/test/fixtures/content-collections/src/pages/collections.json.js @@ -8,8 +8,9 @@ export async function GET() { const withUnionSchema = stripAllRenderFn(await getCollection('with-union-schema')); const withSymlinkedContent = stripAllRenderFn(await getCollection('with-symlinked-content')); const withSymlinkedData = stripAllRenderFn(await getCollection('with-symlinked-data')); + const withoutConfig = stripAllRenderFn(await getCollection('without-config')); return new Response( - devalue.stringify({ withSchemaConfig, withSlugConfig, withUnionSchema, withSymlinkedContent, withSymlinkedData }), + devalue.stringify({ withSchemaConfig, withSlugConfig, withUnionSchema, withSymlinkedContent, withSymlinkedData, withoutConfig }), ); }