Files
openclaw/src/shared/markdown-code.ts
Peter Steinberger 49cc59b1e8 refactor: consolidate markdown code fences, error coercion, and byte-identical helper pairs (#99932)
* refactor(shared): add markdown code span/fence helpers and migrate seven call sites

* refactor(normalization-core): add canonical toErrorObject and migrate six copies

* refactor: consolidate byte-identical helper pairs onto owner modules

* refactor: reuse canonical path and token helpers in session tools and credentials

* refactor(packages): dedupe compaction summarization tail and session dir parsing

* fix(security): keep missing extensions dir silent in shared plugin dir lister

* refactor: reuse media-core chunk reader and shared dreaming session key helper

* fix(plugins): register error-coercion subpath in sdk alias table

* chore(plugin-sdk): re-pin callable export count after rebase

* chore(plugin-sdk): re-pin callable export count after rebase
2026-07-04 08:40:41 -04:00

33 lines
1.1 KiB
TypeScript

// Markdown code spans/fences that survive embedded backticks.
function longestBacktickRun(value: string): number {
let longest = 0;
let current = 0;
for (const char of value) {
if (char === "`") {
current += 1;
longest = Math.max(longest, current);
continue;
}
current = 0;
}
return longest;
}
/**
* Wraps text in an inline code span whose delimiter is longer than any
* backtick run inside it. Edge backticks and newlines get spacer padding so
* renderers do not glue the delimiter onto the content.
*/
export function formatInlineCodeSpan(value: string): string {
const delimiter = "`".repeat(longestBacktickRun(value) + 1);
const padding = value.startsWith("`") || value.endsWith("`") || value.includes("\n") ? " " : "";
return `${delimiter}${padding}${value}${padding}${delimiter}`;
}
/** Wraps text in a fenced code block whose fence is longer than any run inside it. */
export function formatFencedCodeBlock(text: string, language?: string): string {
const fence = "`".repeat(Math.max(3, longestBacktickRun(text) + 1));
return `${fence}${language ?? ""}\n${text}\n${fence}`;
}