fix: potentially unreliable cache
This commit is contained in:
parent
989dcd44fd
commit
d6a03bfc99
1 changed files with 2 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
||||||
import type { AstroIntegration, AstroConfig } from 'astro';
|
import type { AstroIntegration, AstroConfig } from 'astro';
|
||||||
import type { InlineConfig } from 'vite';
|
import { InlineConfig, normalizePath } from 'vite';
|
||||||
import type { Config } from '@markdoc/markdoc';
|
import type { Config } from '@markdoc/markdoc';
|
||||||
import Markdoc from '@markdoc/markdoc';
|
import Markdoc from '@markdoc/markdoc';
|
||||||
import { getAstroConfigPath, MarkdocError, parseFrontmatter } from './utils.js';
|
import { getAstroConfigPath, MarkdocError, parseFrontmatter } from './utils.js';
|
||||||
|
@ -7,14 +7,12 @@ import { fileURLToPath } from 'node:url';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
|
|
||||||
export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
||||||
const entryBodyByFileIdCache = new Map<string, string>();
|
|
||||||
return {
|
return {
|
||||||
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 }) {
|
function getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||||
entryBodyByFileIdCache.set(fileUrl.pathname, parsed.content);
|
|
||||||
return {
|
return {
|
||||||
data: parsed.data,
|
data: parsed.data,
|
||||||
body: parsed.content,
|
body: parsed.content,
|
||||||
|
@ -41,9 +39,7 @@ export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
||||||
|
|
||||||
validateRenderProperties(markdocConfig, config);
|
validateRenderProperties(markdocConfig, config);
|
||||||
const body =
|
const body =
|
||||||
entryBodyByFileIdCache.get(id) ??
|
getEntryInfo({ fileUrl: new URL(normalizePath(id), 'file://'), contents: code }).body;
|
||||||
// It's possible for Vite to attempt to transform a file before `getEntryInfo()` has run
|
|
||||||
getEntryInfo({ fileUrl: new URL(id, 'file://'), contents: code }).body;
|
|
||||||
const ast = Markdoc.parse(body);
|
const ast = Markdoc.parse(body);
|
||||||
const content = Markdoc.transform(ast, markdocConfig);
|
const content = Markdoc.transform(ast, markdocConfig);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue