From cfbcf1db7f57ee3ae54a258476ffcca7c5855138 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Thu, 12 Oct 2023 17:11:09 -0500 Subject: [PATCH] wtf --- packages/astro/src/content/types-generator.ts | 14 +++++++++++--- packages/integrations/mdx/src/index.ts | 2 +- .../markdown/remark/src/frontmatter-injection.ts | 9 +++++++-- packages/markdown/remark/src/index.ts | 6 ++++-- packages/markdown/remark/src/types.ts | 1 + 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index b50c597fd..bd07f0476 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -30,7 +30,7 @@ type RawContentEvent = { name: ChokidarEvent; entry: string }; type ContentEvent = { name: ChokidarEvent; entry: URL }; type DataEntryMetadata = Record; -type ContentEntryMetadata = { slug: string }; +type ContentEntryMetadata = { slug: string, path: string }; type CollectionEntryMap = { [collection: string]: | { @@ -276,7 +276,7 @@ export async function createContentTypesGenerator({ if (!(entryKey in collectionEntryMap[collectionKey].entries)) { collectionEntryMap[collectionKey] = { type: 'content', - entries: { ...collectionInfo.entries, [entryKey]: { slug: addedSlug } }, + entries: { ...collectionInfo.entries, [entryKey]: { slug: addedSlug, path: event.entry.toString() } }, }; } return { shouldGenerateTypes: true }; @@ -453,7 +453,15 @@ async function writeContentFiles({ )}] }`; 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`; break; diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index fd330625e..192069a26 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -123,7 +123,7 @@ export default function mdx(partialMdxOptions: Partial = {}): AstroI const vfile = new VFile({ value: pageContent, path: id }); // Ensure `data.astro` is available to all remark plugins - setVfileFrontmatter(vfile, frontmatter); + setVfileFrontmatter(vfile, frontmatter, { filePath: id }); try { const compiled = await processor.process(vfile); diff --git a/packages/markdown/remark/src/frontmatter-injection.ts b/packages/markdown/remark/src/frontmatter-injection.ts index 4828873fd..6aa80df72 100644 --- a/packages/markdown/remark/src/frontmatter-injection.ts +++ b/packages/markdown/remark/src/frontmatter-injection.ts @@ -1,5 +1,5 @@ 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 { if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) { @@ -27,10 +27,15 @@ export function safelyGetAstroData(vfileData: Data): MarkdownAstroData | Invalid return astro; } -export function setVfileFrontmatter(vfile: VFile, frontmatter: Record) { +export function setVfileFrontmatter( + vfile: VFile, + frontmatter: Record, + renderOpts: MarkdownProcessorRenderOptions | undefined +) { vfile.data ??= {}; vfile.data.astro ??= {}; (vfile.data.astro as any).frontmatter = frontmatter; + (vfile.data.astro as any).filePath = renderOpts?.filePath; } /** diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index 89c9ca8bd..56e6b23d1 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -124,8 +124,9 @@ export async function createMarkdownProcessor( return { async render(content, renderOpts) { - const vfile = new VFile({ value: content, path: renderOpts?.fileURL }); - setVfileFrontmatter(vfile, renderOpts?.frontmatter ?? {}); + console.log('RENDER OPTS', renderOpts); + const vfile = new VFile({ value: content, path: renderOpts?.filePath }); + setVfileFrontmatter(vfile, renderOpts?.frontmatter ?? {}, renderOpts); const result: MarkdownVFile = await parser.process(vfile).catch((err) => { // Ensure that the error message contains the input filename @@ -170,6 +171,7 @@ export async function renderMarkdown( const result = await processor.render(content, { fileURL: opts.fileURL, + filePath: opts.filePath, frontmatter: opts.frontmatter, }); diff --git a/packages/markdown/remark/src/types.ts b/packages/markdown/remark/src/types.ts index bcab97041..eb7594a77 100644 --- a/packages/markdown/remark/src/types.ts +++ b/packages/markdown/remark/src/types.ts @@ -68,6 +68,7 @@ export interface MarkdownProcessor { export interface MarkdownProcessorRenderOptions { /** @internal */ fileURL?: URL; + filePath?: string; /** Used for frontmatter injection plugins */ frontmatter?: Record; }