fence
text as a Markdown code block in lang: its fence longer than any run of backticks inside it.
export function fence(text: string, lang: string): string { const longest = Math.max(0, ...[...text.matchAll(/`+/g)].map((m) => m[0].length)); const f = "`".repeat(Math.max(3, longest + 1)); return `${f}${lang}\n${text}\n${f}`; }