mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-04-05 23:37:19 +02:00
🔢 fix: Unescape LaTeX Numbers in Artifact Content Edit (#10476)
This commit is contained in:
parent
b8b1217c34
commit
3f62ce054f
4 changed files with 168 additions and 2 deletions
27
packages/api/src/utils/latex.ts
Normal file
27
packages/api/src/utils/latex.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Unescapes LaTeX preprocessing done by the frontend preprocessLaTeX function.
|
||||
* This reverses the escaping of currency dollar signs and other LaTeX transformations.
|
||||
*
|
||||
* The frontend escapes dollar signs for proper LaTeX rendering (e.g., $14 → \\$14),
|
||||
* but the database stores the original unescaped versions. This function reverses
|
||||
* that transformation to match database content.
|
||||
*
|
||||
* @param text - The escaped text from the frontend
|
||||
* @returns The unescaped text matching the database format
|
||||
*/
|
||||
export function unescapeLaTeX(text: string | null | undefined): string | null | undefined {
|
||||
if (!text || typeof text !== 'string') {
|
||||
return text;
|
||||
}
|
||||
|
||||
// Unescape currency dollar signs (\\$ or \$ → $)
|
||||
// This is the main transformation done by preprocessLaTeX for currency
|
||||
let result = text.replace(/\\\\?\$/g, '$');
|
||||
|
||||
// Unescape mhchem notation if present
|
||||
// Convert $$\\ce{...}$$ back to $\ce{...}$
|
||||
result = result.replace(/\$\$\\\\ce\{([^}]*)\}\$\$/g, '$\\ce{$1}$');
|
||||
result = result.replace(/\$\$\\\\pu\{([^}]*)\}\$\$/g, '$\\pu{$1}$');
|
||||
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue