- scripts/diagrams/build-diagram-assets
scripts/diagrams/build-diagram-assets module.
- scripts/diagrams/format-mermaid
Deterministic formatter for Mermaid diagram source strings.
This module operates on raw Mermaid source text and enforces:
- Structural indentation
- Whitespace normalization
- Mermaid grammar safety (Mermaid v11 compatible)
Characteristics:
- Pure function (no side effects)
- Idempotent (repeated runs produce the same output)
- Project-agnostic (safe to reuse elsewhere)
IMPORTANT:
- This module does NOT read or write files
- It does NOT validate diagram correctness
- It only normalizes formatting
- DIAGRAM_BLOCK_RE
Regex finds: diagram:
.../- CORE_TYPES
VALID_MERMAID_TYPES --------------------------------------------------------------------------- Hybrid strict mode: - CORE_TYPES are always allowed - EXTENDED_TYPES are allowed but validated more strictly /
- OUTPUT_DIR
Output directory for rendered PNG assets. This directory is expected to be committed or published alongside generated documentation. /
- VIEWPORT
Fixed viewport ensures: - Consistent diagram scaling - Predictable text wrapping - Stable screenshot output across environments /
- addReactKeys(file, api) ⇒
string Main codemod function that traverses the AST to find JSX elements returned from array
.map()calls and adds missingkeyprops. The function usesjscodeshiftto manipulate the AST and applies the following logic:- Identify
.map()calls and their callback functions. - For each JSX element returned from the callback, check if it already has a
keyprop. - If not, attempt to add a
keyprop using the following priority: a. If the callback has an item parameter (e.g.,item), useitem.idas the key. b. If the callback has an index parameter (e.g.,index), use the index as a fallback key. - Skip any JSX elements that already have a
keyprop defined.
- Identify
- fixJsdocImportTypes(file, api) ⇒
string Codemod function that rewrites unsupported JSDoc
import()type references into plain type identifiers.- fixUnusedMapIndex(file, api) ⇒
string Main codemod function that traverses the AST to find
.map()calls and renames unused index parameters to_index. The function usesjscodeshiftto manipulate the AST and applies the following logic:- Identify
.map()calls and their callback functions. - Check if the callback has at least two parameters (item and index).
- Verify that the second parameter (index) is an Identifier.
- Traverse the callback body to check if the index parameter is referenced.
- If the index parameter is not used, rename it to
_index.
- Identify
- stripInit()
Removes the Mermaid init block for structural validation only. /
- extractSources()
Extracts all Mermaid source strings from a diagram block. /
- lintDiagram()
Performs structural validation on a single Mermaid diagram block. /
- run()
ENTRYPOINT BEHAVIOR /
- normalizeMermaidSource(source) ⇒
string Normalizes Mermaid diagram source for consistent linting. This function ensures:
- Proper newline after init block
- Blank line after diagram declaration
- Consistent line endings
- Trims leading whitespace This is a best-effort normalization to improve linting consistency. It does NOT attempt to fully parse or validate Mermaid syntax. It simply applies common formatting fixes to reduce false positives in linters.
- normalizeDiagrams(input) ⇒
array Normalizes a diagram collection into a consistent array format. Supports both object maps and legacy arrays. Applies Mermaid source normalization. This function is designed to be flexible and forgiving, allowing for various input shapes while ensuring a predictable output structure for downstream processing.
- normalizeMermaidSource(source) ⇒
string Normalizes a Mermaid diagram source string by removing init blocks, normalizing line endings, and trimming leading whitespace.
- render()
Renders all Mermaid diagram blocks to PNG images.
Execution flow:
- Ensure output directory exists
- Launch headless Chromium
- Load Mermaid in an isolated HTML shell
- Inject diagram source one at a time
- Screenshot rendered output
- Close browser
This function is intentionally sequential to:
- Avoid race conditions in Mermaid rendering
- Prevent memory pressure from concurrent pages
scripts/diagrams/build-diagram-assets module.
Main execution function for building diagram assets.
Kind: inner method of scripts/diagrams/build-diagram-assets
Deterministic formatter for Mermaid diagram source strings.
This module operates on raw Mermaid source text and enforces:
- Structural indentation
- Whitespace normalization
- Mermaid grammar safety (Mermaid v11 compatible)
Characteristics:
- Pure function (no side effects)
- Idempotent (repeated runs produce the same output)
- Project-agnostic (safe to reuse elsewhere)
IMPORTANT:
- This module does NOT read or write files
- It does NOT validate diagram correctness
- It only normalizes formatting
Formats a Mermaid diagram source string into a deterministic, readable, and Mermaid-safe structure.
This function is intentionally:
- Pure (no side effects)
- Idempotent (safe to run multiple times)
- Structural-only (never changes semantic meaning)
It does NOT validate diagrams — linting is handled separately.
Kind: Exported function
Returns: string - Canonically formatted Mermaid source.
| Param | Type | Description |
|---|---|---|
| source | string |
Raw Mermaid source text. |
Regex finds: diagram: ... /
VALID_MERMAID_TYPES --------------------------------------------------------------------------- Hybrid strict mode: - CORE_TYPES are always allowed - EXTENDED_TYPES are allowed but validated more strictly /
Output directory for rendered PNG assets. This directory is expected to be committed or published alongside generated documentation. /
Fixed viewport ensures: - Consistent diagram scaling - Predictable text wrapping - Stable screenshot output across environments /
Main codemod function that traverses the AST to find JSX elements
returned from array .map() calls and adds missing key props.
The function uses jscodeshift to manipulate the AST and applies the following logic:
- Identify
.map()calls and their callback functions. - For each JSX element returned from the callback, check if it already has a
keyprop. - If not, attempt to add a
keyprop using the following priority: a. If the callback has an item parameter (e.g.,item), useitem.idas the key. b. If the callback has an index parameter (e.g.,index), use the index as a fallback key. - Skip any JSX elements that already have a
keyprop defined.
Kind: global function
Returns: string - The transformed source code with added key props.
| Param | Type | Description |
|---|---|---|
| file | * |
The file object provided by jscodeshift, containing the source code to be transformed. |
| api | * |
The jscodeshift API object, used for AST manipulation. |
Codemod function that rewrites unsupported JSDoc import() type references into plain type identifiers.
Kind: global function
Returns: string - The transformed source code with fixed JSDoc import types.
| Param | Type | Description |
|---|---|---|
| file | * |
The file object provided by jscodeshift, containing the source code to be transformed. |
| api | * |
The jscodeshift API object, used for AST manipulation. |
Main codemod function that traverses the AST to find .map() calls and renames unused index parameters to _index.
The function uses jscodeshift to manipulate the AST and applies the following logic:
- Identify
.map()calls and their callback functions. - Check if the callback has at least two parameters (item and index).
- Verify that the second parameter (index) is an Identifier.
- Traverse the callback body to check if the index parameter is referenced.
- If the index parameter is not used, rename it to
_index.
Kind: global function
Returns: string - Updated source code after codemod transformations.
| Param | Type |
|---|---|
| file | * |
| api | * |
Removes the Mermaid init block for structural validation only. /
Extracts all Mermaid source strings from a diagram block. /
Performs structural validation on a single Mermaid diagram block. /
RULE: Diagram type validation (hybrid strict). /
Kind: inner constant of lintDiagram
ENTRYPOINT BEHAVIOR /
Normalizes Mermaid diagram source for consistent linting. This function ensures:
- Proper newline after init block
- Blank line after diagram declaration
- Consistent line endings
- Trims leading whitespace This is a best-effort normalization to improve linting consistency. It does NOT attempt to fully parse or validate Mermaid syntax. It simply applies common formatting fixes to reduce false positives in linters.
Kind: global function
Returns: string - The normalized Mermaid diagram source string.
| Param | Type | Description |
|---|---|---|
| source | string |
The raw Mermaid diagram source string. |
Example
const source = `%%{init: {"theme": "dark"}}%%
graph TD
A --> B
`;
const normalized = normalizeMermaidSource(source);
console.log(normalized);
// Output:
// %%{init: {"theme": "dark"}}%%
// graph TD
// A --> BNormalizes a diagram collection into a consistent array format. Supports both object maps and legacy arrays. Applies Mermaid source normalization. This function is designed to be flexible and forgiving, allowing for various input shapes while ensuring a predictable output structure for downstream processing.
Kind: global function
Returns: array - An array of normalized diagram entries, each with a consistent structure.
| Param | Type | Description |
|---|---|---|
| input | object | array |
The raw diagram collection, either as an object map or an array. |
Example
// Object map input
const diagramsMap = {
"diagram1": { diagram: "graph TD\nA --> B" },
"diagram2": { diagram: "graph LR\nC --> D" }
};
const normalizedFromMap = normalizeDiagrams(diagramsMap);
console.log(normalizedFromMap);
// Output:
// [
// { key: "diagram1", diagram: "graph TD\nA --> B" },
// { key: "diagram2", diagram: "graph LR\nC --> D" }
// ]
// Array inputNormalizes a Mermaid diagram source string by removing init blocks, normalizing line endings, and trimming leading whitespace.
Kind: global function
Returns: string - The normalized Mermaid diagram source string.
| Param | Type | Description |
|---|---|---|
| source | string |
The Mermaid diagram source string. |
Example
const source = `%%{init: {"theme": "dark"}}%%
graph TD
A --> B
`;
const normalized = normalizeMermaidSource(source);
console.log(normalized);
// Output:
// graph TD
// A --> BRenders all Mermaid diagram blocks to PNG images.
Execution flow:
- Ensure output directory exists
- Launch headless Chromium
- Load Mermaid in an isolated HTML shell
- Inject diagram source one at a time
- Screenshot rendered output
- Close browser
This function is intentionally sequential to:
- Avoid race conditions in Mermaid rendering
- Prevent memory pressure from concurrent pages
Kind: global function
Throws:
ErrorIf Playwright fails to launch or rendering fails.