fix: handle possible cache miss
This commit is contained in:
parent
6217b7a1a1
commit
c876d3fee3
1 changed files with 17 additions and 22 deletions
|
@ -12,18 +12,19 @@ export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
||||||
name: '@astrojs/markdoc',
|
name: '@astrojs/markdoc',
|
||||||
hooks: {
|
hooks: {
|
||||||
'astro:config:setup': async ({ updateConfig, config, addContentEntryType }: any) => {
|
'astro:config:setup': async ({ updateConfig, config, addContentEntryType }: any) => {
|
||||||
|
function getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||||
|
entryBodyByFileIdCache.set(fileUrl.pathname, parsed.content);
|
||||||
|
return {
|
||||||
|
data: parsed.data,
|
||||||
|
body: parsed.content,
|
||||||
|
slug: parsed.data.slug,
|
||||||
|
rawData: parsed.matter,
|
||||||
|
};
|
||||||
|
}
|
||||||
const contentEntryType = {
|
const contentEntryType = {
|
||||||
extensions: ['.mdoc'],
|
extensions: ['.mdoc'],
|
||||||
getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
getEntryInfo,
|
||||||
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
|
||||||
entryBodyByFileIdCache.set(fileUrl.pathname, parsed.content);
|
|
||||||
return {
|
|
||||||
data: parsed.data,
|
|
||||||
body: parsed.content,
|
|
||||||
slug: parsed.data.slug,
|
|
||||||
rawData: parsed.matter,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
contentModuleTypes: await fs.promises.readFile(
|
contentModuleTypes: await fs.promises.readFile(
|
||||||
new URL('../template/content-module-types.d.ts', import.meta.url),
|
new URL('../template/content-module-types.d.ts', import.meta.url),
|
||||||
'utf-8'
|
'utf-8'
|
||||||
|
@ -39,17 +40,10 @@ export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
||||||
if (!id.endsWith('.mdoc')) return;
|
if (!id.endsWith('.mdoc')) return;
|
||||||
|
|
||||||
validateRenderProperties(markdocConfig, config);
|
validateRenderProperties(markdocConfig, config);
|
||||||
const body = entryBodyByFileIdCache.get(id);
|
const body =
|
||||||
if (!body) {
|
entryBodyByFileIdCache.get(id) ??
|
||||||
// Cache entry should exist if `getCollection()` was called
|
// It's possible for Vite to attempt to transform a file before `getEntryInfo()` has run
|
||||||
// (see `getEntryInfo()` above)
|
getEntryInfo({ fileUrl: new URL(id, 'file://'), contents: code }).body;
|
||||||
// If not, the user probably tried to import directly.
|
|
||||||
throw new Error(
|
|
||||||
`Unable to render ${JSON.stringify(
|
|
||||||
id.replace(config.root.pathname, '')
|
|
||||||
)}. If you tried to import this file directly, please use a Content Collection query instead. See https://docs.astro.build/en/guides/content-collections/#rendering-content-to-html for more information.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const ast = Markdoc.parse(body);
|
const ast = Markdoc.parse(body);
|
||||||
const content = Markdoc.transform(ast, markdocConfig);
|
const content = Markdoc.transform(ast, markdocConfig);
|
||||||
|
|
||||||
|
@ -101,7 +95,8 @@ function validateRenderProperty({
|
||||||
throw new MarkdocError({
|
throw new MarkdocError({
|
||||||
message: `Invalid ${type} configuration: ${JSON.stringify(
|
message: `Invalid ${type} configuration: ${JSON.stringify(
|
||||||
name
|
name
|
||||||
)}. The "render" property must reference a capitalized component name. If you want to render to an HTML element, see our docs on rendering Markdoc manually [TODO docs link].`,
|
)}. The "render" property must reference a capitalized component name.`,
|
||||||
|
hint: 'If you want to render to an HTML element, see our docs on rendering Markdoc manually [TODO docs link].',
|
||||||
location: astroConfigPath
|
location: astroConfigPath
|
||||||
? {
|
? {
|
||||||
file: astroConfigPath,
|
file: astroConfigPath,
|
||||||
|
|
Loading…
Reference in a new issue