Skip to content

Commit

Permalink
refactor: replace duplicate parseSlug
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Apr 27, 2023
1 parent 45404cf commit 7f77913
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
44 changes: 20 additions & 24 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import { CONTENT_TYPES_FILE, VIRTUAL_MODULE_ID } from './consts.js';
import {
getContentPaths,
getEntryInfo,
getEntrySlug,
getEntryType,
loadContentConfig,
NoCollectionError,
parseFrontmatter,
type ContentConfig,
type ContentObservable,
type ContentPaths,
type EntryInfo,
getContentEntryConfigByExtMap,
getEntrySlug,
} from './utils.js';

type ChokidarEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
Expand Down Expand Up @@ -187,14 +186,23 @@ export async function createContentTypesGenerator({
return { shouldGenerateTypes: false };
}

const { id, collection } = entryInfo;
const { id, collection, slug: generatedSlug } = entryInfo;
const contentEntryType = contentEntryConfigByExt.get(path.extname(event.entry.pathname));
if (!contentEntryType) return { shouldGenerateTypes: false };

const collectionKey = JSON.stringify(collection);
const entryKey = JSON.stringify(id);

switch (event.name) {
case 'add':
const addedSlug = await parseSlug({ fs, event, entryInfo });
const addedSlug = await getEntrySlug({
generatedSlug,
id,
collection,
fileUrl: event.entry,
contentEntryType,
fs,
});
if (!(collectionKey in contentTypes)) {
addCollection(contentTypes, collectionKey);
}
Expand All @@ -210,7 +218,14 @@ export async function createContentTypesGenerator({
case 'change':
// User may modify `slug` in their frontmatter.
// Only regen types if this change is detected.
const changedSlug = await parseSlug({ fs, event, entryInfo });
const changedSlug = await getEntrySlug({
generatedSlug,
id,
collection,
fileUrl: event.entry,
contentEntryType,
fs,
});
if (contentTypes[collectionKey]?.[entryKey]?.slug !== changedSlug) {
setEntry(contentTypes, collectionKey, entryKey, changedSlug);
return { shouldGenerateTypes: true };
Expand Down Expand Up @@ -309,25 +324,6 @@ function removeCollection(contentMap: ContentTypes, collectionKey: string) {
delete contentMap[collectionKey];
}

async function parseSlug({
fs,
event,
entryInfo: { id, collection, slug: generatedSlug },
}: {
fs: typeof fsMod;
event: ContentEvent;
entryInfo: EntryInfo;
}) {
// `slug` may be present in entry frontmatter.
// This should be respected by the generated `slug` type!
// Parse frontmatter and retrieve `slug` value for this.
// Note: will raise any YAML exceptions and `slug` parse errors (i.e. `slug` is a boolean)
// on dev server startup or production build init.
const rawContents = await fs.promises.readFile(event.entry, 'utf-8');
const { data: frontmatter } = parseFrontmatter(rawContents, fileURLToPath(event.entry));
return getEntrySlug({ id, collection, generatedSlug, frontmatterSlug: frontmatter.slug });
}

function setEntry(
contentTypes: ContentTypes,
collectionKey: string,
Expand Down
41 changes: 35 additions & 6 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const msg = {
`${collection} does not have a config. We suggest adding one for type safety!`,
};

export function getEntrySlug({
export function parseEntrySlug({
id,
collection,
generatedSlug,
Expand Down Expand Up @@ -422,23 +422,52 @@ export async function getStringifiedLookupMap({
contentGlob.map(async (filePath) => {
const info = getEntryInfo({ contentDir, entry: filePath });
if (info instanceof NoCollectionError) return;
const contentEntryConfig = contentEntryConfigByExt.get(extname(filePath));
if (!contentEntryConfig) return;
const contentEntryType = contentEntryConfigByExt.get(extname(filePath));
if (!contentEntryType) return;

const { id, collection, slug: generatedSlug } = info;
filePathByLookupId[collection] ??= {};
const { slug: frontmatterSlug } = await contentEntryConfig.getEntryInfo({
const slug = await getEntrySlug({
id,
collection,
generatedSlug,
fs,
fileUrl: pathToFileURL(filePath),
contents: await fs.promises.readFile(filePath, 'utf-8'),
contentEntryType,
});
const slug = getEntrySlug({ id, collection, generatedSlug, frontmatterSlug });
filePathByLookupId[collection][slug] = rootRelativePath(root, filePath);
})
);

return JSON.stringify(filePathByLookupId);
}

/**
* Check for slug in content entry frontmatter and validate the type,
* falling back to the `generatedSlug` if none is found.
*/
export async function getEntrySlug({
id,
collection,
generatedSlug,
contentEntryType,
fileUrl,
fs,
}: {
fs: typeof fsMod;
id: string;
collection: string;
generatedSlug: string;
fileUrl: URL;
contentEntryType: Pick<ContentEntryType, 'getEntryInfo'>;
}) {
const { slug: frontmatterSlug } = await contentEntryType.getEntryInfo({
fileUrl,
contents: await fs.promises.readFile(fileUrl, 'utf-8'),
});
return parseEntrySlug({ generatedSlug, frontmatterSlug, id, collection });
}

export function getExtGlob(exts: string[]) {
return exts.length === 1
? // Wrapping {...} breaks when there is only one extension
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getContentPaths,
getEntryData,
getEntryInfo,
getEntrySlug,
parseEntrySlug,
getEntryType,
globalContentConfigObserver,
NoCollectionError,
Expand Down Expand Up @@ -221,7 +221,7 @@ export function astroContentImportPlugin({
const _internal = { filePath: fileId, rawData: rawData };
// TODO: move slug calculation to the start of the build
// to generate a performant lookup map for `getEntryBySlug`
const slug = getEntrySlug({
const slug = parseEntrySlug({
id,
collection,
generatedSlug,
Expand Down

0 comments on commit 7f77913

Please sign in to comment.