Warning
This is an internal project, and is not intended for public use. No support or stability guarantees are provided.
The enhanceCodeEmphasis source enhancer adds visual emphasis to specific lines in code examples by marking them with a data-hl attribute. This allows you to highlight important code patterns or call attention to specific sections in demos using simple comment annotations.
@highlight comments@highlight-start / @highlight-end pairs@highlight-text "text"@highlight "description")! at the end of descriptions (e.g., @highlight "We must do this!")import { enhanceCodeEmphasis } from '@mui/internal-docs-infra/pipeline/enhanceCodeEmphasis';
// Use as a source enhancer
const sourceEnhancers = [enhanceCodeEmphasis];
// Pass to CodeHighlighter or use with enhanceCode
The enhancer processes @highlight comments in your source files and adds data-hl to the corresponding line elements in the HAST tree.
Emphasize the line containing the comment:
export default function Button() {
return (
<button className="primary">Click me</button> {/* @highlight */}
);
}
The line with the <button> element will be emphasized.
Add context about what's being emphasized:
export default function Component() {
const [count, setCount] = useState(0); // @highlight "We track state"
return <div>{count}</div>;
}
End a description with ! to mark it as strong emphasis (data-hl="strong"):
export default function Component() {
const apiKey = process.env.API_KEY; // @highlight "We must provide the API key!"
return <div>...</div>;
}
Strong emphasis is useful for highlighting critical code that users must pay attention to or modify.
Emphasize a range of lines between start and end markers:
export default function Component() {
return (
// @highlight-start
<div>
<h1>Heading 1</h1>
<p>Some content</p>
</div>
// @highlight-end
);
}
Lines 4-7 (from <div> through </div>) will be emphasized. The range starts at the first line after @highlight-start and ends at the last line before @highlight-end.
Explain what the emphasized section demonstrates:
export default function Component() {
return (
// @highlight-start "We add a heading with an h1"
<div>
<h1>Heading 1</h1>
</div>
// @highlight-end
);
}
Highlight specific text within a line:
export default function Component() {
return (
<div>
<h1>Heading 1</h1> {/* @highlight-text "Heading 1" */}
</div>
);
}
This wraps the specified text in a <span data-hl=""> element, allowing you to highlight a specific word or phrase within a line rather than the entire line.
The enhancer recognizes these comment patterns:
| Pattern | Effect |
|---|---|
@highlight | Emphasize current line |
@highlight "description" | Emphasize with description |
@highlight-start | Start multi-line block |
@highlight-start "desc" | Start block with description |
@highlight-end | End multi-line block |
@highlight-text "text" | Highlight specific text within the line |
Descriptions are provided as quoted strings:
// @highlight "We initialize the state"
const [value, setValue] = useState(0);
// @highlight-start "We render the component"
<div>
<p>Content</p>
</div>;
// @highlight-end
By default, @highlight comments are stripped from the rendered code. To show the comment syntax in documentation (while still applying the emphasis), use the displayComments code block attribute:
```jsx displayComments
export default function Button() {
return (
<button className="primary">Click me</button> {/* @highlight */}
);
}
```
This is useful for documentation pages where you want to show users the comment syntax itself.
You can emphasize multiple individual lines:
export default function Form() {
const [name, setName] = useState(''); // @highlight
const [email, setEmail] = useState(''); // @highlight
return (
<form>
<input value={name} onChange={(e) => setName(e.target.value)} />
<input value={email} onChange={(e) => setEmail(e.target.value)} /> {/* @highlight */}
</form>
);
}
The enhancer handles nested emphasis blocks using a stack-based algorithm. Lines that fall within multiple emphasis ranges are automatically marked with strong emphasis (data-hl="strong"):
export default function Component() {
return (
// @highlight-start "outer block"
<div>
<header>Title</header>
{/* @highlight-start "inner block" */}
<main>
<p>Content</p>
</main>
{/* @highlight-end */}
<footer>Footer</footer>
</div>
// @highlight-end
);
}
In this example:
<div>, <header>, <footer>, </div>) get normal emphasis<main>, <p>, </main>) get strong emphasis because they're nested within both rangesdata-hl-position) are preserved from the innermost rangeCombine both patterns in the same file:
export default function Dashboard() {
const [data, setData] = useState([]); // @highlight
return (
<div>
<Header />
{/* @highlight-start */}
<Chart data={data} />
<Table data={data} />
{/* @highlight-end */}
<Footer /> {/* @highlight */}
</div>
);
}
notableCommentsPrefix: ['@highlight']@highlight, @highlight-start, @highlight-end, and @highlight-text patterns@highlight-start to the last line before @highlight-enddataHl: '' (or dataHl: 'strong' for nested lines or descriptions ending with !) to line elementsLines in the HAST tree have this structure after enhancement:
{
type: 'element',
tagName: 'span',
properties: {
className: 'line',
dataLn: 3, // Line number
dataHl: '', // Added by enhancer (or 'strong' for nested/!)
dataHlDescription: 'We track state', // Optional description
dataHlPosition: 'single', // 'single' | 'start' | 'end' for line position
},
children: [/* code tokens */]
}
For text highlighting with @highlight-text "text", the specified text within the line is wrapped in a span:
{
type: 'element',
tagName: 'span',
properties: { dataHl: '' },
children: [{ type: 'text', value: 'text' }]
}
The enhancer adds these attributes to line elements:
| Attribute | Type | Description |
|---|---|---|
data-hl | string | "" for normal, "strong" if description ends with ! or line is nested |
data-hl-description | string | Description text (if provided in comment) |
data-hl-position | string | "single" for single-line, "start" or "end" for multiline range bounds (from innermost range) |
For single-line emphasis with @highlight:
data-hl is set (or data-hl="strong" if description ends with !)data-hl-description is set if a description was provideddata-hl-position="single" is set to distinguish from multiline range boundariesFor multi-line emphasis with @highlight-start / @highlight-end:
data-hl is set on all lines in the range (or data-hl="strong" if description ends with ! or line is nested)data-hl-description is set on the first line (if provided)data-hl-position="start" is set on the first line (only for ranges with more than one line)data-hl-position="end" is set on the last line (only for ranges with more than one line)For text highlighting with @highlight-text "text":
<span data-hl=""> elementMulti-line directives are paired using a stack:
@highlight-start pushes onto the stack@highlight-end pops from the stack and emphasizes the rangeConfigure in your webpack loader or server-side loading:
import { enhanceCodeEmphasis } from '@mui/internal-docs-infra/pipeline/enhanceCodeEmphasis';
import { createLoadServerSource } from '@mui/internal-docs-infra/pipeline/loadServerSource';
import { loadCodeVariant } from '@mui/internal-docs-infra/pipeline/loadCodeVariant';
const sourceEnhancers = [enhanceCodeEmphasis];
// Create a loadSource that extracts emphasis comments
const loadSource = createLoadServerSource({
notableCommentsPrefix: ['@highlight'],
removeCommentsWithPrefix: ['@highlight'],
});
// Use with loadCodeVariant
const { code } = await loadCodeVariant(url, variantName, variant, {
loadSource,
sourceEnhancers,
sourceParser,
});
// Or with CodeHighlighter
<CodeHighlighter
sourceEnhancers={sourceEnhancers}
// ... other props
/>
// Or with useCode
const { selectedFile } = useCode(props, {
sourceEnhancers,
});
The default comment prefix is @highlight, exported as EMPHASIS_COMMENT_PREFIX:
import { EMPHASIS_COMMENT_PREFIX } from '@mui/internal-docs-infra/pipeline/enhanceCodeEmphasis';
console.log(EMPHASIS_COMMENT_PREFIX); // '@highlight'
To use a different prefix, you would need to create a custom enhancer based on this implementation.
The data-hl attribute can be targeted with CSS. First, ensure lines are displayed as blocks for proper highlighting:
/* Required: Display lines as blocks for line-based highlighting */
.frame[data-lined] {
display: block;
white-space: normal;
}
.frame[data-lined] .line {
display: block;
white-space: pre;
}
/* Highlight emphasized lines */
.line[data-hl] {
background-color: rgba(255, 255, 0, 0.1);
border-left: 2px solid #ffd700;
padding-left: 8px;
}
/* Strong emphasis for critical lines (descriptions ending with !) */
.line[data-hl='strong'] {
background-color: rgba(255, 100, 100, 0.15);
border-left: 3px solid #ff4444;
}
/* Single-line emphasis - rounded on all corners */
.line[data-hl-position='single'] {
border-radius: 4px;
}
/* Multiline block start - rounded top corners */
.line[data-hl-position='start'] {
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
/* Multiline block end - rounded bottom corners */
.line[data-hl-position='end'] {
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
/* Dark mode variant */
@media (prefers-color-scheme: dark) {
.line[data-hl] {
background-color: rgba(255, 255, 0, 0.05);
border-left-color: #b8860b;
}
.line[data-hl='strong'] {
background-color: rgba(255, 100, 100, 0.1);
border-left-color: #cc3333;
}
}
You can display the description using CSS ::before:
/* Show description as tooltip */
.line[data-hl-description]::before {
content: attr(data-hl-description);
position: absolute;
/* ... tooltip styles */
}
Use single-line emphasis for specific statements:
// ✓ Good - highlights the important line
const result = await fetchData();
// ✗ Avoid - too vague
function processData() {
const result = await fetchData();
return result;
}
Add context when the emphasis isn't obvious:
// ✓ Good - explains what to notice
<div className="container"> {/* @highlight "We add the container class" */}
// ✗ Less helpful - just marks the line
<div className="container"> {/* @highlight */}
Use multi-line emphasis for logical code blocks:
// ✓ Good - emphasizes the complete pattern
// @highlight-start "We handle the form submission"
<form onSubmit={handleSubmit}>
<input type="text" value={name} onChange={handleChange} />
<button type="submit">Submit</button>
</form>
// @highlight-end
Use the appropriate comment style for your language:
// JSX/TSX - use {/* */} for inline, // for block
<div>{/* @highlight */}</div>
// @highlight-start
// JavaScript/TypeScript - use //
const x = 42; // @highlight
// CSS - use /* */
.button {
color: blue; /* @highlight */
}
Check that:
notableCommentsPrefix: ['@highlight']If you have unmatched @highlight-start or @highlight-end:
@highlight-end → ignored (no effect)@highlight-start → ignored (no matching end)Check your code for balanced pairs.
Verify that:
dataLn properties)/**
* Source enhancer that adds emphasis to code lines based on @highlight comments.
*/
export const enhanceCodeEmphasis: SourceEnhancer;
/**
* The prefix used to identify emphasis comments in source code.
*/
export const EMPHASIS_COMMENT_PREFIX: '@highlight';
/**
* Source enhancer function type.
*/
type SourceEnhancer = (
root: HastRoot,
comments: SourceComments | undefined,
fileName: string,
) => HastRoot | Promise<HastRoot>;
/**
* Comments extracted from source code, keyed by line number.
*/
type SourceComments = Record<number, string[]>;