The parseSource utility parses source code into HAST (Hypertext Abstract Syntax Tree) nodes with syntax highlighting using Starry Night. It converts code into highlighted HTML structures for display in documentation and demos.
import { createParseSource } from '@mui/internal-docs-infra/pipeline/parseSource';
// Initialize the parser (do this once, typically at app startup)
const parseSource = await createParseSource();
// Parse and highlight JavaScript code
const highlighted = parseSource('const x = 42;', 'example.js');
// Use the HAST tree for rendering (e.g., with hastToReact)
The parser automatically:
import { createParseSource } from '@mui/internal-docs-infra/pipeline/parseSource';
const parseSource = await createParseSource();
// JavaScript
const jsCode = parseSource('const x = 42;', 'example.js');
// TypeScript
const tsCode = parseSource('interface User { name: string; }', 'types.ts');
// CSS
const cssCode = parseSource('.button { color: blue; }', 'styles.css');
// HTML
const htmlCode = parseSource('<div>Hello</div>', 'index.html');
import { createParseSource } from '@mui/internal-docs-infra/pipeline/parseSource';
const parseSource = await createParseSource();
const files = [
{ name: 'App.tsx', content: 'export default function App() {}' },
{ name: 'styles.css', content: '.app { margin: 0; }' },
{ name: 'utils.ts', content: 'export const helper = () => {};' },
];
const highlighted = files.map((file) => ({
name: file.name,
hast: parseSource(file.content, file.name),
}));
For unsupported file extensions, the parser returns plain text:
const parseSource = await createParseSource();
// Unsupported extension - returns plain text node
const result = parseSource('Some content', 'file.xyz');
// {
// type: 'root',
// children: [{ type: 'text', value: 'Some content' }]
// }
// File without extension - also returns plain text
const readme = parseSource('# README', 'README');
After initialization, parseSource can be used directly from anywhere:
import { createParseSource, parseSource } from '@mui/internal-docs-infra/pipeline/parseSource';
// Initialize once (e.g., in app startup)
await createParseSource();
// Use the global instance anywhere in your code
function highlightCode(code: string, fileName: string) {
return parseSource(code, fileName); // Works without re-initialization
}
import { createParseSource } from '@mui/internal-docs-infra/pipeline/parseSource';
import { hastToReact } from '@mui/internal-docs-infra/hastUtils';
const parseSource = await createParseSource();
function HighlightedCode({ code, fileName }: { code: string; fileName: string }) {
const hast = parseSource(code, fileName);
const jsx = hastToReact(hast);
return <pre>{jsx}</pre>;
}
function createParseSource(): Promise<ParseSource>;
Initializes Starry Night and returns a configured parseSource function. This only needs to be called once per application.
Returns: A Promise that resolves to the parseSource function.
Side Effects: Stores the Starry Night instance globally for reuse.
function parseSource(source: string, fileName: string): HastNode[];
Parses source code into a HAST tree with syntax highlighting.
Parameters:
| Parameter | Type | Description |
|---|---|---|
source | string | The source code to parse and highlight |
fileName | string | File name (used to detect language via extension) |
Returns: HAST Root node containing highlighted code structure.
Throws: Error if createParseSource() has not been called first.
type ParseSource = (source: string, fileName: string) => HastNode[];
interface HastRoot {
type: 'root';
children: HastNode[];
}
type HastNode = HastRoot | HastElement | HastText;
createParseSource() creates a Starry Night instance with grammar definitionsstarryNightGutter() adds line number elements to the treeThe Starry Night instance is cached globally using __docs_infra_starry_night_instance__ to avoid re-initialization.
The parser supports languages based on file extensions:
| Language | Extensions |
|---|---|
| JavaScript | .js, .mjs, .cjs, .jsx |
| TypeScript | .ts, .tsx |
| CSS | .css |
| HTML | .html, .htm |
| JSON | .json |
| And more | Via Starry Night grammars |
See grammars.ts for the complete list of supported languages.
import { parseSource } from '@mui/internal-docs-infra/pipeline/parseSource';
// ❌ This will throw an error
parseSource('code', 'file.js');
// Error: Starry Night not initialized. Use createParseSource to create an initialized parseSource function.
// ✓ Initialize first
import { createParseSource } from '@mui/internal-docs-infra/pipeline/parseSource';
await createParseSource();
parseSource('code', 'file.js'); // Now works
const parseSource = await createParseSource();
// Handles empty content gracefully
const result = parseSource('', 'empty.js');
// Returns valid HAST tree with empty content
When NOT to use:
<pre> tags if highlighting isn't neededhastUtils - For converting HAST to React JSXCodeHighlighter - Uses parseSource for syntax highlightingloadPrecomputedCodeHighlighter - Build-time optimization using parseSource