summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/markdoc/src/index.ts')
-rw-r--r--packages/integrations/markdoc/src/index.ts49
1 files changed, 17 insertions, 32 deletions
diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts
index 71e117de4..70d005ee5 100644
--- a/packages/integrations/markdoc/src/index.ts
+++ b/packages/integrations/markdoc/src/index.ts
@@ -3,13 +3,7 @@ import Markdoc from '@markdoc/markdoc';
import type { AstroConfig, AstroIntegration, ContentEntryType, HookParameters } from 'astro';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
-import type { InlineConfig } from 'vite';
-import {
- getAstroConfigPath,
- MarkdocError,
- parseFrontmatter,
- prependForwardSlash,
-} from './utils.js';
+import { getAstroConfigPath, MarkdocError, parseFrontmatter } from './utils.js';
type SetupHookParams = HookParameters<'astro:config:setup'> & {
// `contentEntryType` is not a public API
@@ -36,36 +30,27 @@ export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
addContentEntryType({
extensions: ['.mdoc'],
getEntryInfo,
+ getRenderModule({ entry }) {
+ validateRenderProperties(markdocConfig, config);
+ const ast = Markdoc.parse(entry.body);
+ const content = Markdoc.transform(ast, {
+ ...markdocConfig,
+ variables: {
+ ...markdocConfig.variables,
+ entry,
+ },
+ });
+ return {
+ code: `import { jsx as h } from 'astro/jsx-runtime';\nimport { Renderer } from '@astrojs/markdoc/components';\nconst transformedContent = ${JSON.stringify(
+ content
+ )};\nexport async function Content ({ components }) { return h(Renderer, { content: transformedContent, components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`,
+ };
+ },
contentModuleTypes: await fs.promises.readFile(
new URL('../template/content-module-types.d.ts', import.meta.url),
'utf-8'
),
});
-
- const viteConfig: InlineConfig = {
- plugins: [
- {
- name: '@astrojs/markdoc',
- async transform(code, id) {
- if (!id.endsWith('.mdoc')) return;
-
- validateRenderProperties(markdocConfig, config);
- const body = getEntryInfo({
- // Can't use `pathToFileUrl` - Vite IDs are not plain file paths
- fileUrl: new URL(prependForwardSlash(id), 'file://'),
- contents: code,
- }).body;
- const ast = Markdoc.parse(body);
- const content = Markdoc.transform(ast, markdocConfig);
-
- return `import { jsx as h } from 'astro/jsx-runtime';\nimport { Renderer } from '@astrojs/markdoc/components';\nconst transformedContent = ${JSON.stringify(
- content
- )};\nexport async function Content ({ components }) { return h(Renderer, { content: transformedContent, components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`;
- },
- },
- ],
- };
- updateConfig({ vite: viteConfig });
},
},
};