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} />;
}
function loadServerCodeMeta(url: string): Promise<Code>;
Loads code metadata from a demo index file.
Parameters:
| Parameter | Type | Description |
|---|---|---|
url | string | File URL to the demo index file (with file://) |
Returns: Promise resolving to a Code object mapping variant names to file URLs or metadata.
Throws: Error if file cannot be read or parsed.
function createLoadServerCodeMeta(options?: CreateLoadCodeMetaOptions): LoadCodeMeta;
Factory function that creates a custom loadCodeMeta function.
Parameters:
| Parameter | Type | Description |
|---|---|---|
options | CreateLoadCodeMetaOptions? | Configuration (currently empty) |
Returns: A LoadCodeMeta function.
type LoadCodeMeta = (url: string) => Promise<Code>;
type Code = {
[variantName: string]:
| string
| {
url: string;
fileName: string;
namedExport: string;
};
};
interface CreateLoadCodeMetaOptions {
// No options currently available
}
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 itloadServerVariantMeta - For loading individual variant metadataloadServerSource - For loading source code contentCodeHighlighter - Uses this for server-side code loadingloadPrecomputedCodeHighlighter - Build-time alternative