API Reference
Core Rendering
ENGINE_PROFILES
interface DiagramEngineProfile { /** Runtime type: 'chromium' for browser-backed engines, 'wasm' for in-process */ runtime: 'chromium' | 'wasm' /** Whether this engine needs a BrowserPool instance */ requiresBrowserPool: boolean /** Whether renders must be serialized within a type lane */ serializedWithinLane: boolean /** Ordering priority for lane scheduling (lower runs first) */ laneOrder: number}const ENGINE_PROFILES: Record<DiagramType, DiagramEngineProfile>Static engine metadata used by the render pipeline and CLI planning.
getEngineProfile(type)
function getEngineProfile(type: DiagramType): DiagramEngineProfileReturns engine metadata for a specific diagram type.
render(source, type, options?)
Render a diagram from a source string.
function render( source: string, type: DiagramType, options?: RenderOptions,): Promise<RenderResult>| Parameter | Type | Required | Description |
|---|---|---|---|
source |
string |
Yes | Diagram source content |
type |
DiagramType |
Yes | Engine to use |
options |
RenderOptions |
No | Rendering config |
Behavior: Acquires the browser pool for browser-backed engines. For theme: 'both', renders both variants. Applies postProcessDarkSvg() to Mermaid dark SVGs when contrastOptimize is true. Converts to raster via sharp for non-SVG formats.
renderFile(filePath, options?)
Render a file from disk. Type inferred from extension.
function renderFile( filePath: string, options?: RenderOptions,): Promise<RenderResult>renderAll(options?)
Batch render all diagrams in a directory tree. Writes to .diagramkit/ folders.
function renderAll(options?: BatchOptions): Promise<RenderAllResult>Returns RenderAllResult with rendered, skipped, failed, failedDetails, and optional metrics.
Steps: discover files, filter by type, check manifest, render stale files, update manifest, clean orphans.
renderDiagramFileToDisk(file, options?)
Render a single file and write output to disk. Returns written filenames.
function renderDiagramFileToDisk( file: DiagramFile, options?: RenderOptions & { config?: DiagramkitConfig; outDir?: string; formats?: OutputFormat[] },): Promise<string[]>watchDiagrams(options)
Watch for changes and re-render. Returns cleanup function.
function watchDiagrams(options: WatchOptions): () => Promise<void>Watched: **/*.mermaid, **/*.mmd, **/*.mmdc, **/*.excalidraw, **/*.drawio, **/*.drawio.xml, **/*.dio, **/*.dot, **/*.gv, **/*.graphviz
Browser Lifecycle
warmup()
function warmup(): Promise<void>Pre-warm the browser pool. Optional — launches automatically on first render.
dispose()
function dispose(): Promise<void>Close browser pool and release resources. Safe to call multiple times.
createRendererRuntime()
function createRendererRuntime(): RendererRuntimeCreates an isolated runtime with its own BrowserPool. Use this when you need lifecycle isolation from the default singleton runtime.
Do not mix runtime.dispose() with the top-level dispose() for the same work unit. Runtime methods manage their own pool; the top-level API manages the shared singleton pool.
DiagramkitError
class DiagramkitError extends Error { code: DiagramkitErrorCode}Typed error class used for user-facing and programmatic failures.
File Discovery
Import these utilities from
diagramkit/utils, not from the maindiagramkitentry.
import { findDiagramFiles, filterByType } from 'diagramkit/utils'findDiagramFiles(dir, config?)
function findDiagramFiles( dir: string, config?: Partial<DiagramkitConfig>,): DiagramFile[]Recursively find all diagram files. Skips hidden dirs and node_modules.
filterByType(files, type, config?)
function filterByType( files: DiagramFile[], type: DiagramType, config?: Partial<DiagramkitConfig>,): DiagramFile[]Manifest
filterStaleFiles(files, force, formats?, config?, theme?)
function filterStaleFiles( files: DiagramFile[], force: boolean, formats?: OutputFormat[], config?: Partial<DiagramkitConfig>, theme?: Theme,): StaleFile[]Filter to files needing re-render. Caches manifests per directory.
planStaleFiles(files, force, formats?, config?, theme?)
function planStaleFiles( files: DiagramFile[], force: boolean, formats?: OutputFormat[], config?: Partial<DiagramkitConfig>, theme?: Theme,): StalePlanEntry[]Returns detailed staleness reasons per file without rendering.
isStale(file, formats?, config?, theme?, manifest?)
function isStale( file: DiagramFile, formats?: OutputFormat[], config?: Partial<DiagramkitConfig>, theme?: Theme, manifest?: Manifest,): booleanReturns true if: no manifest entry, hash changed, format changed, outputs missing, or manifest disabled.
readManifest(sourceDir, config?)
function readManifest( sourceDir: string, config?: Partial<DiagramkitConfig>,): ManifestgetDiagramsDir(sourceDir, config?)
function getDiagramsDir(sourceDir: string, config?: Partial<DiagramkitConfig>): stringensureDiagramsDir(sourceDir, config?)
function ensureDiagramsDir(sourceDir: string, config?: Partial<DiagramkitConfig>): stringLike getDiagramsDir but creates the directory if missing.
Extension Utilities
getExtensionMap(overrides?)
function getExtensionMap(overrides?: Record<string, DiagramType>): Record<string, DiagramType>getDiagramType(filename, map?)
function getDiagramType(filename: string, map?: Record<string, DiagramType>): DiagramType | nullLongest-match-first resolution.
getMatchedExtension(filename, map?)
function getMatchedExtension(filename: string, map?: Record<string, DiagramType>): string | nullgetAllExtensions(map?)
function getAllExtensions(map?: Record<string, DiagramType>): string[]getExtensionsForType(type, map?)
function getExtensionsForType(type: DiagramType, map?: Record<string, DiagramType>): string[]Configuration
getDefaultConfig()
function getDefaultConfig(): DiagramkitConfigloadConfig(overrides?, dir?, configFile?, options?)
function loadConfig( overrides?: Partial<DiagramkitConfig>, dir?: string, configFile?: string, options?: { strict?: boolean; warn?: (message: string) => void },): DiagramkitConfigMerge order: defaults, global, env vars, local (or explicit configFile), overrides.
getFileOverrides(filePath, config?, rootDir?)
function getFileOverrides( filePath: string, config?: Partial<DiagramkitConfig>, rootDir?: string,): FileOverride | undefinedResolve per-file overrides from the overrides config. Matches against exact filenames, relative paths, and glob patterns. Returns the merged override for the file, or undefined if no overrides match.
defineConfig(config)
function defineConfig(config: Partial<DiagramkitConfig>): Partial<DiagramkitConfig>Identity helper for TypeScript config file autocomplete. Returns the input unchanged but provides type checking in diagramkit.config.ts files.
defaultMermaidDarkTheme
const defaultMermaidDarkTheme: Record<string, string>SVG-to-Raster Conversion
convertSvg(svg, options)
function convertSvg( svg: Buffer | string, options: ConvertOptions,): Promise<Buffer>Also available from diagramkit/convert.
Color Utilities
postProcessDarkSvg(svg)
function postProcessDarkSvg(svg: string): stringWCAG contrast optimization for dark SVGs. Exported from diagramkit/utils and diagramkit/color.
Output Utilities
atomicWrite(path, content)
function atomicWrite(path: string, content: Buffer): voidWrite via .tmp + rename to prevent partial files.
stripDiagramExtension(filename, map?)
function stripDiagramExtension(filename: string, map?: Record<string, DiagramType>): stringRemoves the detected diagram extension from the filename.
Utilities (Advanced)
These functions are available from diagramkit/utils for advanced use cases such as custom build pipelines.
writeManifest(sourceDir, manifest, config?)
function writeManifest( sourceDir: string, manifest: Manifest, config?: Partial<DiagramkitConfig>,): voidWrite manifest atomically (.tmp + rename) to prevent corruption on crash.
hashFile(filePath)
function hashFile(filePath: string): stringCompute SHA-256 content hash of a file. Returns a sha256: prefixed hex string (first 16 chars).
renderGraphviz(source, options?)
function renderGraphviz( source: string, options?: { theme?: Theme; contrastOptimize?: boolean },): Promise<{ lightSvg?: string; darkSvg?: string }>Low-level Graphviz renderer used by the unified engine pipeline.
cleanOrphans(files, config?, roots?)
function cleanOrphans( files: DiagramFile[], config?: Partial<DiagramkitConfig>, roots?: string[],): voidRemove orphaned outputs and manifest entries for diagram sources that no longer exist.
updateManifest(files, formats?, config?, theme?)
function updateManifest( files: RenderableFile[], formats?: OutputFormat[], config?: Partial<DiagramkitConfig>, theme?: Theme,): voidUpdate manifests after successful renders. Groups files by directory and merges formats with existing entries.
writeRenderResult(name, outDir, result, naming?)
function writeRenderResult( name: string, outDir: string, result: RenderResult, naming?: OutputNamingOptions,): string[]Write whichever variants were returned by the renderer. Returns an array of written filenames.
getExpectedOutputNamesMulti(name, formats, theme?, naming?)
function getExpectedOutputNamesMulti( name: string, formats: OutputFormat[], theme?: Theme, naming?: OutputNamingOptions,): string[]Get expected output names across multiple formats and theme variants.
getExpectedOutputNames(name, format?, theme?, naming?)
function getExpectedOutputNames( name: string, format?: OutputFormat, theme?: Theme, naming?: OutputNamingOptions,): string[]Get expected output names for a single format and theme.
getOutputFileName(name, variant, format?, naming?)
function getOutputFileName( name: string, variant: 'light' | 'dark', format?: OutputFormat, naming?: OutputNamingOptions,): stringGet the output filename for a specific variant and format. Pattern: {prefix}{name}{suffix}-{variant}.{format}.
getOutputVariants(theme?)
function getOutputVariants(theme?: Theme): ('light' | 'dark')[]Get the output variants for a given theme. Returns ['light'], ['dark'], or ['light', 'dark'].
SVG Validation
Import these functions from
diagramkit/utilsordiagramkit/validate. They are also surfaced through the CLI asdiagramkit validateand run automatically after everydiagramkit render.
import { validateSvg, validateSvgFile, validateSvgDirectory, formatValidationResult,} from 'diagramkit/utils'validateSvg(svg, filePath?, options?)
function validateSvg( svg: string, filePath?: string, options?: SvgValidateOptions,): SvgValidationResultStructural + WCAG 2.2 AA contrast validation of an SVG string. Returns issues with codes (MISSING_SVG_TAG, LOW_CONTRAST_TEXT, EXTERNAL_RESOURCE, …), severity, message, and a remediation suggestion.
validateSvgFile(filePath, options?)
function validateSvgFile(filePath: string, options?: SvgValidateOptions): SvgValidationResultvalidateSvgDirectory(dir, options?)
function validateSvgDirectory( dir: string, options?: { recursive?: boolean } & SvgValidateOptions,): SvgValidationResult[]formatValidationResult(result)
function formatValidationResult(result: SvgValidationResult): stringFormat a SvgValidationResult into the human-readable lines used by diagramkit validate.
See Types for SvgIssue, SvgIssueCode, SvgValidationResult, and SvgValidateOptions shapes.
Subpath Exports
| Import | Content |
|---|---|
diagramkit |
Main API |
diagramkit/utils |
Discovery, manifest, extensions, output, color, validation |
diagramkit/color |
Color utilities (contrast, WCAG ratios) |
diagramkit/convert |
SVG-to-raster conversion |
diagramkit/validate |
SVG structural + WCAG contrast validation |