This commit is contained in:
Michael Zhang 2023-10-12 17:11:09 -05:00
parent 9cd6a6657b
commit cfbcf1db7f
5 changed files with 24 additions and 8 deletions

View file

@ -30,7 +30,7 @@ type RawContentEvent = { name: ChokidarEvent; entry: string };
type ContentEvent = { name: ChokidarEvent; entry: URL }; type ContentEvent = { name: ChokidarEvent; entry: URL };
type DataEntryMetadata = Record<string, never>; type DataEntryMetadata = Record<string, never>;
type ContentEntryMetadata = { slug: string }; type ContentEntryMetadata = { slug: string, path: string };
type CollectionEntryMap = { type CollectionEntryMap = {
[collection: string]: [collection: string]:
| { | {
@ -276,7 +276,7 @@ export async function createContentTypesGenerator({
if (!(entryKey in collectionEntryMap[collectionKey].entries)) { if (!(entryKey in collectionEntryMap[collectionKey].entries)) {
collectionEntryMap[collectionKey] = { collectionEntryMap[collectionKey] = {
type: 'content', type: 'content',
entries: { ...collectionInfo.entries, [entryKey]: { slug: addedSlug } }, entries: { ...collectionInfo.entries, [entryKey]: { slug: addedSlug, path: event.entry.toString() } },
}; };
} }
return { shouldGenerateTypes: true }; return { shouldGenerateTypes: true };
@ -453,7 +453,15 @@ async function writeContentFiles({
)}] }`; )}] }`;
const slugType = JSON.stringify(entryMetadata.slug); const slugType = JSON.stringify(entryMetadata.slug);
contentTypesStr += `${entryKey}: {\n id: ${entryKey};\n slug: ${slugType};\n body: string;\n collection: ${collectionKey};\n data: ${dataType}\n} & ${renderType};\n`; contentTypesStr += [
`${entryKey}: {`,
` id: ${entryKey};`,
` slug: ${slugType};`,
` path: ${JSON.stringify(entryMetadata.path)};`,
` body: string;`,
` collection: ${collectionKey};`,
` data: ${dataType}`,
`} & ${renderType};`].join("\n");
} }
contentTypesStr += `};\n`; contentTypesStr += `};\n`;
break; break;

View file

@ -123,7 +123,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
const vfile = new VFile({ value: pageContent, path: id }); const vfile = new VFile({ value: pageContent, path: id });
// Ensure `data.astro` is available to all remark plugins // Ensure `data.astro` is available to all remark plugins
setVfileFrontmatter(vfile, frontmatter); setVfileFrontmatter(vfile, frontmatter, { filePath: id });
try { try {
const compiled = await processor.process(vfile); const compiled = await processor.process(vfile);

View file

@ -1,5 +1,5 @@
import type { VFileData as Data, VFile } from 'vfile'; import type { VFileData as Data, VFile } from 'vfile';
import type { MarkdownAstroData } from './types.js'; import type { MarkdownAstroData, MarkdownProcessorRenderOptions } from './types.js';
function isValidAstroData(obj: unknown): obj is MarkdownAstroData { function isValidAstroData(obj: unknown): obj is MarkdownAstroData {
if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) { if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) {
@ -27,10 +27,15 @@ export function safelyGetAstroData(vfileData: Data): MarkdownAstroData | Invalid
return astro; return astro;
} }
export function setVfileFrontmatter(vfile: VFile, frontmatter: Record<string, any>) { export function setVfileFrontmatter(
vfile: VFile,
frontmatter: Record<string, any>,
renderOpts: MarkdownProcessorRenderOptions | undefined
) {
vfile.data ??= {}; vfile.data ??= {};
vfile.data.astro ??= {}; vfile.data.astro ??= {};
(vfile.data.astro as any).frontmatter = frontmatter; (vfile.data.astro as any).frontmatter = frontmatter;
(vfile.data.astro as any).filePath = renderOpts?.filePath;
} }
/** /**

View file

@ -124,8 +124,9 @@ export async function createMarkdownProcessor(
return { return {
async render(content, renderOpts) { async render(content, renderOpts) {
const vfile = new VFile({ value: content, path: renderOpts?.fileURL }); console.log('RENDER OPTS', renderOpts);
setVfileFrontmatter(vfile, renderOpts?.frontmatter ?? {}); const vfile = new VFile({ value: content, path: renderOpts?.filePath });
setVfileFrontmatter(vfile, renderOpts?.frontmatter ?? {}, renderOpts);
const result: MarkdownVFile = await parser.process(vfile).catch((err) => { const result: MarkdownVFile = await parser.process(vfile).catch((err) => {
// Ensure that the error message contains the input filename // Ensure that the error message contains the input filename
@ -170,6 +171,7 @@ export async function renderMarkdown(
const result = await processor.render(content, { const result = await processor.render(content, {
fileURL: opts.fileURL, fileURL: opts.fileURL,
filePath: opts.filePath,
frontmatter: opts.frontmatter, frontmatter: opts.frontmatter,
}); });

View file

@ -68,6 +68,7 @@ export interface MarkdownProcessor {
export interface MarkdownProcessorRenderOptions { export interface MarkdownProcessorRenderOptions {
/** @internal */ /** @internal */
fileURL?: URL; fileURL?: URL;
filePath?: string;
/** Used for frontmatter injection plugins */ /** Used for frontmatter injection plugins */
frontmatter?: Record<string, any>; frontmatter?: Record<string, any>;
} }