Warning
This is an internal project, and is not intended for public use. No support or stability guarantees are provided.
The loadServerCodeMeta utility parses demo files to extract variant information and resolves file paths for code metadata. It reads demo index files, finds createDemo factory calls, and builds a Code object mapping variant names to their file URLs.
Note
This function is primarily used internally by
CodeHighlighterwhen rendering demos on the server. Most users won't need to call it directly.
import { loadServerCodeMeta } from '@mui/internal-docs-infra/pipeline/loadServerCodeMeta';
// Load metadata from a demo index file
const code = await loadServerCodeMeta('file:///app/components/button/demos/basic/index.ts');
// Returns: { Default: 'file:///app/components/button/demos/basic/Component.tsx' }
The function:
createDemo() or createDemoWithVariants() callCode object ready for use with CodeHighlighterFor demos with a single component:
// Demo file: app/components/button/demos/basic/index.ts
// export const DemoButtonBasic = createDemo(import.meta.url, Button);
const code = await loadServerCodeMeta('file:///app/components/button/demos/basic/index.ts');
// Returns:
// {
// Default: 'file:///app/components/button/demos/basic/Button.tsx'
// }
For demos with multiple styling approaches:
// Demo file: app/components/button/demos/styled/index.ts
// export const DemoButtonStyled = createDemoWithVariants(
// import.meta.url,
// { CssModules, Tailwind }
// );
const code = await loadServerCodeMeta('file:///app/components/button/demos/styled/index.ts');
// Returns:
// {
// CssModules: 'file:///app/components/button/demos/styled/css-modules/Button.tsx',
// Tailwind: 'file:///app/components/button/demos/styled/tailwind/Button.tsx'
// }
When variants use named exports instead of default exports:
// Demo file: app/components/form/demos/validation/index.ts
// export const DemoFormValidation = createDemoWithVariants(
// import.meta.url,
// { Basic: BasicForm, Advanced: AdvancedForm }
// );
const code = await loadServerCodeMeta('file:///app/components/form/demos/validation/index.ts');
// Returns:
// {
// Basic: {
// url: 'file:///app/components/form/demos/validation/BasicForm.tsx',
// fileName: 'BasicForm.tsx',
// namedExport: 'BasicForm'
// },
// Advanced: {
// url: 'file:///app/components/form/demos/validation/AdvancedForm.tsx',
// fileName: 'AdvancedForm.tsx',
// namedExport: 'AdvancedForm'
// }
// }
Create a custom loadCodeMeta function using the factory:
import { createLoadServerCodeMeta } from '@mui/internal-docs-infra/pipeline/loadServerCodeMeta';
const customLoadCodeMeta = createLoadServerCodeMeta({
// Configuration options (currently none available)
});
const code = await customLoadCodeMeta('file:///path/to/demo/index.ts');
Using with server-side rendering:
import { loadServerCodeMeta } from '@mui/internal-docs-infra/pipeline/loadServerCodeMeta';
import { CodeHighlighter } from '@mui/internal-docs-infra/CodeHighlighter';
async function DemoPage() {
const code = await loadServerCodeMeta('file:///app/demos/example/index.ts');
return <CodeHighlighter code={code} loadCodeMeta={loadServerCodeMeta} />;
}
readFileparseCreateFactoryCall to find createDemo() or createDemoWithVariants() callsresolveVariantPathsWithFs to convert relative imports to absolute file URLsCode object with variant metadataThe function handles:
file:// protocolCodeHighlighter on the serverWhen NOT to use:
CodeHighlighter if you already have itDefault loadServerCodeMeta function that resolves variant paths from demo files.
This function is used to load code metadata for demos, specifically resolving paths for variants defined in the demo files.
It reads the demo file, parses it to find createDemo calls with variants, and resolves the paths for those variants.
It returns a Code object mapping variant names to their resolved file URLs.
| Parameter | Type | Description |
|---|---|---|
| url | string |
Promise<Code>type CreateLoadCodeMetaOptions = {};type DirectoryEntry = { name: string; isFile: boolean; isDirectory: boolean };type ResolveModulePathOptions = { extensions?: string[] };loadServerVariantMeta - For loading individual variant metadataloadServerSource - For loading source code contentCodeHighlighter - Uses this for server-side code loadingloadPrecomputedCodeHighlighter - Build-time alternative