Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to content layer types and jsdocs #11643

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions packages/astro/src/content/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ import { AstroError, AstroErrorData } from '../core/errors/index.js';
const SAVE_DEBOUNCE_MS = 500;

export interface RenderedContent {
/** Rendered HTML string. If present then `render(entry)` will return a component that renders this HTML. */
html: string;
metadata?: {
imagePaths: Array<string>;
/** Any images that are present in this entry. Relative to the {@link DataEntry} filePath. */
imagePaths?: Array<string>;
/** Any headings that are present in this file. */
headings?: MarkdownHeading[];
/** Raw frontmatter, parsed parsed from the file. This may include data from remark plugins. */
frontmatter?: Record<string, any>;
/** Any other metadata that is present in this file. */
[key: string]: unknown;
};
}

export interface DataEntry<TData extends Record<string, unknown> = Record<string, unknown>> {
/** The ID of the entry. Unique per collection. */
id: string;
/** The parsed entry data */
data: TData;
/** The file path of the content, if applicable. Relative to the site root. */
filePath?: string;
/** The raw body of the content, if applicable. */
body?: string;
/** An optional content digest, to check if the content has changed. */
digest?: number | string;
/** The rendered content of the entry, if applicable. */
rendered?: RenderedContent;
}

Expand Down Expand Up @@ -305,24 +316,18 @@ export interface ScopedDataStore {
key: string
) => DataEntry<TData> | undefined;
entries: () => Array<[id: string, DataEntry]>;
/**
* Adds a new entry to the store. If an entry with the same ID already exists,
* it will be replaced.
* @param opts
* @param opts.id The ID of the entry. Must be unique per collection.
* @param opts.data The data to store.
* @param opts.body The raw body of the content, if applicable.
* @param opts.filePath The file path of the content, if applicable. Relative to the site root.
* @param opts.digest A content digest, to check if the content has changed.
* @param opts.rendered The rendered content, if applicable.
* @returns `true` if the entry was added or updated, `false` if the entry was not changed. This will be the case if the provided digest matches the one in the store.
*/
set: <TData extends Record<string, unknown>>(opts: {
/** The ID of the entry. Must be unique per collection. */
id: string;
/** The data to store. */
data: TData;
/** The raw body of the content, if applicable. */
body?: string;
/** The file path of the content, if applicable. Relative to the site root. */
filePath?: string;
/** A content digest, to check if the content has changed. */
digest?: number | string;
/** The rendered content, if applicable. */
rendered?: RenderedContent;
}) => boolean;
values: () => Array<DataEntry>;
Expand All @@ -331,18 +336,11 @@ export interface ScopedDataStore {
clear: () => void;
has: (key: string) => boolean;
/**
* Adds image etc assets to the store. These assets will be transformed
* by Vite, and the URLs will be available in the final build.
* @param assets An array of asset src values, relative to the importing file.
* @param fileName The full path of the file that is importing the assets.
* @internal Adds asset imports to the store. This is used to track image imports for the build. This API is subject to change.
*/
addAssetImports: (assets: Array<string>, fileName: string) => void;
/**
* Adds a single asset to the store. This asset will be transformed
* by Vite, and the URL will be available in the final build.
* @param assetImport
* @param fileName
* @returns
* @internal Adds an asset import to the store. This is used to track image imports for the build. This API is subject to change.
*/
addAssetImport: (assetImport: string, fileName: string) => void;
}
Expand Down Expand Up @@ -373,4 +371,5 @@ function dataStoreSingleton() {
};
}

/** @internal */
export const globalDataStore = dataStoreSingleton();
1 change: 0 additions & 1 deletion packages/astro/src/content/loaders/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ export interface Loader {
load: (context: LoaderContext) => Promise<void>;
/** Optionally, define the schema of the data. Will be overridden by user-defined schema */
schema?: ZodSchema | Promise<ZodSchema> | (() => ZodSchema | Promise<ZodSchema>);
render?: (entry: any) => any;
}
Loading