feat: support extends with URL

This commit is contained in:
bholmesdev 2023-06-07 17:20:55 -04:00 committed by bholmesdev
parent 321164503a
commit abff1d696c
2 changed files with 23 additions and 16 deletions

View file

@ -130,12 +130,8 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration
renderComponent,
} from 'astro/runtime/server/index.js';
import { Renderer } from '@astrojs/markdoc/components';
import { collectHeadings, setupConfig, setupConfigSync, Markdoc } from '@astrojs/markdoc/runtime';
import userConfig from ${JSON.stringify(markdocConfigId)};${
astroConfig.experimental.assets
? `\nimport { experimentalAssetsConfig } from '@astrojs/markdoc/experimental-assets-config';\nuserConfig.nodes = { ...experimentalAssetsConfig.nodes, ...userConfig.nodes };`
: ''
}
import { collectHeadings, Markdoc } from '@astrojs/markdoc/runtime';
import { getConfig, getConfigSync } from ${JSON.stringify(markdocConfigId)};
const stringifiedAst = ${JSON.stringify(
/* Double stringify to encode *as* stringified JSON */ JSON.stringify(ast)
)};
@ -146,8 +142,7 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration
instead of the Content component. Would remove double-transform and unlock variable resolution in heading slugs. */
''
}
const headingConfig = userConfig.nodes?.heading;
const config = setupConfigSync(headingConfig ? { nodes: { heading: headingConfig } } : {});
const config = getConfigSync();
const ast = Markdoc.Ast.fromJSON(stringifiedAst);
const content = Markdoc.transform(ast, config);
return collectHeadings(Array.isArray(content) ? content : content.children);
@ -155,9 +150,8 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration
export const Content = createComponent({
async factory(result, props) {
const config = await setupConfig({
...userConfig,
variables: { ...userConfig.variables, ...props },
const config = await getConfig({
variables: props,
});
return renderComponent(

View file

@ -5,6 +5,7 @@ import { loadMarkdocConfig } from './load-config.js';
import type { AstroMarkdocConfig } from './config.js';
import type { Schema } from '@markdoc/markdoc';
import type { Render } from './config.js';
import { setupConfig } from './runtime.js';
export const markdocConfigId = 'astro:markdoc-config';
export const resolvedMarkdocConfigId = '\x00' + markdocConfigId;
@ -26,20 +27,32 @@ export function vitePluginMarkdocConfig({ astroConfig }: { astroConfig: AstroCon
if (!markdocConfigResult) {
return `export default {}`;
}
const { config, fileUrl } = markdocConfigResult;
const { config: unresolvedConfig, fileUrl } = markdocConfigResult;
const config = await setupConfig(unresolvedConfig);
const tagRenderPathnameMap = getRenderUrlMap(config.tags ?? {});
const nodeRenderPathnameMap = getRenderUrlMap(config.nodes ?? {});
const code = `import { resolveComponentImports } from '@astrojs/markdoc/runtime';
import markdocConfig from ${JSON.stringify(fileUrl.pathname)};
const code = `import { setupConfig, setupConfigSync, resolveComponentImports } from '@astrojs/markdoc/runtime';
import userConfig from ${JSON.stringify(fileUrl.pathname)};${
astroConfig.experimental.assets
? `\nimport { experimentalAssetsConfig } from '@astrojs/markdoc/experimental-assets-config';\nuserConfig.nodes = { ...experimentalAssetsConfig.nodes, ...userConfig.nodes };`
: ''
}
${getStringifiedImports(tagRenderPathnameMap, 'Tag')};
${getStringifiedImports(nodeRenderPathnameMap, 'Node')};
const tagComponentMap = ${getStringifiedMap(tagRenderPathnameMap, 'Tag')};
const nodeComponentMap = ${getStringifiedMap(nodeRenderPathnameMap, 'Node')};
export default resolveComponentImports(markdocConfig, tagComponentMap, nodeComponentMap);`;
console.log('$$$markdoc-config', code);
export async function getConfig(configOverrides = {}) {
const config = await setupConfig(userConfig, configOverrides);
return resolveComponentImports(config, tagComponentMap, nodeComponentMap);
}
${/* used by `getHeadings()`. This bypasses `extends` resolution, which can be async */ ''}
export function getConfigSync() {
return setupConfigSync(userConfig);
}`;
return code;
},
};