refactor: move to separate file
This commit is contained in:
parent
d87544c241
commit
0ab7d0dbd1
2 changed files with 56 additions and 45 deletions
|
@ -21,6 +21,7 @@ import type * as rollup from 'rollup';
|
||||||
import { normalizePath } from 'vite';
|
import { normalizePath } from 'vite';
|
||||||
import { loadMarkdocConfig, type MarkdocConfigResult } from './load-config.js';
|
import { loadMarkdocConfig, type MarkdocConfigResult } from './load-config.js';
|
||||||
import { setupConfig } from './runtime.js';
|
import { setupConfig } from './runtime.js';
|
||||||
|
import { markdocConfigId, vitePluginMarkdocConfig } from './vite-plugin-config.js';
|
||||||
|
|
||||||
type SetupHookParams = HookParameters<'astro:config:setup'> & {
|
type SetupHookParams = HookParameters<'astro:config:setup'> & {
|
||||||
// `contentEntryType` is not a public API
|
// `contentEntryType` is not a public API
|
||||||
|
@ -61,9 +62,6 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration
|
||||||
}
|
}
|
||||||
const userMarkdocConfig = markdocConfigResult?.config ?? {};
|
const userMarkdocConfig = markdocConfigResult?.config ?? {};
|
||||||
|
|
||||||
const markdocConfigId = 'astro:markdoc-config';
|
|
||||||
const resolvedMarkdocConfigId = '\x00' + markdocConfigId;
|
|
||||||
|
|
||||||
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));
|
||||||
return {
|
return {
|
||||||
|
@ -224,48 +222,7 @@ export default function markdocIntegration(legacyConfig?: any): AstroIntegration
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
vitePluginMarkdocConfig({ astroConfig }),
|
||||||
name: '@astrojs/markdoc:config',
|
|
||||||
resolveId(this: rollup.PluginContext, id: string) {
|
|
||||||
if (id === markdocConfigId) {
|
|
||||||
return resolvedMarkdocConfigId;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
load(id: string) {
|
|
||||||
if (id !== resolvedMarkdocConfigId) return;
|
|
||||||
|
|
||||||
// TODO: migrate config loader, invalidate on change
|
|
||||||
if (!markdocConfigResult) {
|
|
||||||
return `export default {}`;
|
|
||||||
}
|
|
||||||
const { config, fileUrl } = markdocConfigResult;
|
|
||||||
let componentPathnameByTag: Record<string, string> = {};
|
|
||||||
const { tags = {}, nodes = {} /* TODO: nodes */ } = config;
|
|
||||||
for (const [name, value] of Object.entries(tags)) {
|
|
||||||
if (value.render instanceof URL) {
|
|
||||||
componentPathnameByTag[name] = value.render.pathname;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let stringifiedComponentImports = '';
|
|
||||||
let stringifiedComponentMap = '{';
|
|
||||||
for (const [tag, componentPathname] of Object.entries(componentPathnameByTag)) {
|
|
||||||
stringifiedComponentImports += `import ${tag} from ${JSON.stringify(
|
|
||||||
componentPathname + '?astroPropagatedAssets'
|
|
||||||
)};\n`;
|
|
||||||
stringifiedComponentMap += `${tag},\n`;
|
|
||||||
}
|
|
||||||
stringifiedComponentMap += '}';
|
|
||||||
const code = `import { resolveComponentImports } from '@astrojs/markdoc/runtime';
|
|
||||||
import markdocConfig from ${JSON.stringify(fileUrl.pathname)};
|
|
||||||
${stringifiedComponentImports};
|
|
||||||
|
|
||||||
const tagComponentMap = ${stringifiedComponentMap};
|
|
||||||
export default resolveComponentImports(markdocConfig, tagComponentMap);`;
|
|
||||||
|
|
||||||
console.log('$$$markdoc-config', code);
|
|
||||||
return code;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
54
packages/integrations/markdoc/src/vite-plugin-config.ts
Normal file
54
packages/integrations/markdoc/src/vite-plugin-config.ts
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
import type { AstroConfig } from 'astro';
|
||||||
|
import type { Plugin } from 'vite';
|
||||||
|
import type { PluginContext } from 'rollup';
|
||||||
|
import { loadMarkdocConfig } from './load-config.js';
|
||||||
|
|
||||||
|
export const markdocConfigId = 'astro:markdoc-config';
|
||||||
|
export const resolvedMarkdocConfigId = '\x00' + markdocConfigId;
|
||||||
|
|
||||||
|
export function vitePluginMarkdocConfig({ astroConfig }: { astroConfig: AstroConfig }): Plugin {
|
||||||
|
return {
|
||||||
|
name: '@astrojs/markdoc:config',
|
||||||
|
resolveId(this: PluginContext, id: string) {
|
||||||
|
if (id === markdocConfigId) {
|
||||||
|
return resolvedMarkdocConfigId;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async load(id: string) {
|
||||||
|
if (id !== resolvedMarkdocConfigId) return;
|
||||||
|
|
||||||
|
// TODO: invalidate on change
|
||||||
|
const markdocConfigResult = await loadMarkdocConfig(astroConfig);
|
||||||
|
|
||||||
|
if (!markdocConfigResult) {
|
||||||
|
return `export default {}`;
|
||||||
|
}
|
||||||
|
const { config, fileUrl } = markdocConfigResult;
|
||||||
|
let componentPathnameByTag: Record<string, string> = {};
|
||||||
|
const { tags = {}, nodes = {} /* TODO: nodes */ } = config;
|
||||||
|
for (const [name, value] of Object.entries(tags)) {
|
||||||
|
if (value.render instanceof URL) {
|
||||||
|
componentPathnameByTag[name] = value.render.pathname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let stringifiedComponentImports = '';
|
||||||
|
let stringifiedComponentMap = '{';
|
||||||
|
for (const [tag, componentPathname] of Object.entries(componentPathnameByTag)) {
|
||||||
|
stringifiedComponentImports += `import ${tag} from ${JSON.stringify(
|
||||||
|
componentPathname + '?astroPropagatedAssets'
|
||||||
|
)};\n`;
|
||||||
|
stringifiedComponentMap += `${tag},\n`;
|
||||||
|
}
|
||||||
|
stringifiedComponentMap += '}';
|
||||||
|
const code = `import { resolveComponentImports } from '@astrojs/markdoc/runtime';
|
||||||
|
import markdocConfig from ${JSON.stringify(fileUrl.pathname)};
|
||||||
|
${stringifiedComponentImports};
|
||||||
|
|
||||||
|
const tagComponentMap = ${stringifiedComponentMap};
|
||||||
|
export default resolveComponentImports(markdocConfig, tagComponentMap);`;
|
||||||
|
|
||||||
|
console.log('$$$markdoc-config', code);
|
||||||
|
return code;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue