mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -05:00
wip: dream api
This commit is contained in:
parent
035c0c4df2
commit
255b0b6e0c
1 changed files with 17 additions and 26 deletions
|
@ -3,7 +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 type { InlineConfig, TransformResult } from 'vite';
|
||||
import {
|
||||
getAstroConfigPath,
|
||||
MarkdocError,
|
||||
|
@ -41,36 +41,27 @@ export default function markdoc(markdocConfig: Config = {}): IntegrationWithPriv
|
|||
addContentEntryType({
|
||||
extensions: ['.mdoc'],
|
||||
getEntryInfo,
|
||||
getModule(entry: any /* TODO: typing */): Partial<TransformResult> {
|
||||
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 });
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue