Skip to content

wrapCode

Import from @varavel/vdl-plugin-sdk/utils/markdown.

function wrapCode(code, lang?): string;

Wraps source text in a fenced Markdown code block.

This helper is intended for generators that emit documentation examples, snippets, or inline reference material and need a reliable way to format code as Markdown without repeating string templates throughout the codebase.

When a language is provided, it becomes the fence info string so downstream renderers can apply syntax highlighting. Backticks inside the code content are escaped so generated blocks remain safe to embed inside surrounding Markdown.

Parameters

Parameter Type Default value Description
code string undefined Code or text content to place inside the fenced block.
lang string "" Optional language identifier for syntax highlighting.

Returns

string

A complete fenced Markdown code block.

Example

wrapCode("const answer = 42;", "ts");
// "```ts\nconst answer = 42;\n```"