export function sanitizeContent(str: string): string {
// (1) Add closing backticks if they are missing such that we render a code block or inline
// element during streaming.
// Regular expression to find either a single backtick or triple backticks
const regex = /(`{1,3})/g;
let singleBackticks = 0;
let tripleBackticks = 0;
// Search for all backticks in the string and update counts
let match;
while ((match = regex.exec(str)) !== null) {
if (match[1] === "```") {
tripleBackticks++;
} else if (match[1] === "`") {
singleBackticks++;
}
}
// Append closing backticks if needed
if (tripleBackticks % 2 !== 0) {
if (str.endsWith("`")) {
str += "``";
} else if (str.endsWith("``")) {
str += "`";
} else {
str += str.includes("\n") ? "\n```" : "```";
}
} else if (singleBackticks % 2 !== 0) {
str += "`";
}
return str;
}
export function detectLanguage(children: React.ReactNode) {
if (Array.isArray(children) && children[0]) {
return children[0].props.className?.replace("language-", "") || "text";
}
return "text";
}
/**
* Converts consecutive newlines (\n\n) into hard breaks to preserve line spacing.
* Inserts an empty line with a non-breaking space to create visual spacing.
*/
export function preserveLineBreaks(content: string): string {
// Replace
and \n\n with \n \n\n to insert an empty line with content
// This creates visual spacing between paragraphs
return content.replace(/(\n\n\n\n|
)/g, "\n\n \n\n");
}
/**
* Preprocesses content to escape dollar signs that are likely NOT inlione LaTeX math. This helps
* prevent false positives when enabling single $ math rendering.
*
* Currrently not used as this was causing issues. See https://github.com/dust-tt/tasks/issues/5739
*
* Patterns that are escaped:
* 1. Solo dollar signs: `$` (not part of a pair)
* 1. Currency amounts: $100, $5.99, $1,000, $50k, $2.5M, $1 billion
* 2. Shell/code variables: $HOME, $PATH, ${variable}
*/
export function preprocessDollarSigns(content: string): string {
let processed = content;
processed = processed
.split("\n")
.map((line) => {
const unescapedDollarMatches = line.match(/(? 1) {
// 2. Protect currency patterns
// Matches: $100, $5.99, $1,000.50, $50k, $2.5M, $1 billion, etc.
line = line.replace(
/(?