Skip to content

Commit

Permalink
Handle folders without config
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Oct 1, 2024
1 parent 5339675 commit 848e976
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
39 changes: 39 additions & 0 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down Expand Up @@ -543,6 +544,7 @@ async function loadContentConfig({
export async function autogenerateCollections({
config,
settings,
fs,
}: {
config?: ContentConfig;
settings: AstroSettings;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 };
}

Expand Down
11 changes: 0 additions & 11 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
font-family: 'Comic Sans MS', sans-serif;
}
Original file line number Diff line number Diff line change
@@ -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**
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
);
}

0 comments on commit 848e976

Please sign in to comment.