mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 18:00:15 +01:00
✏️ feat: LaTeX parsing for Messages (#1585)
* feat: Beta features tab in Settings and LaTeX Parsing toggle * feat: LaTex parsing with spec
This commit is contained in:
parent
638f9242e5
commit
a8d6bfde7a
17 changed files with 291 additions and 22 deletions
24
client/src/utils/latex.ts
Normal file
24
client/src/utils/latex.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Regex to check if the processed content contains any potential LaTeX patterns
|
||||
const containsLatexRegex =
|
||||
/\\\(.*?\\\)|\\\[.*?\\\]|\$.*?\$|\\begin\{equation\}.*?\\end\{equation\}/;
|
||||
// Regex for inline and block LaTeX expressions
|
||||
const inlineLatex = new RegExp(/\\\((.+?)\\\)/, 'g');
|
||||
// const blockLatex = new RegExp(/\\\[(.*?)\\\]/, 'gs');
|
||||
const blockLatex = new RegExp(/\\\[(.*?[^\\])\\\]/, 'gs');
|
||||
|
||||
export const processLaTeX = (content: string) => {
|
||||
// Escape dollar signs followed by a digit or space and digit
|
||||
let processedContent = content.replace(/(\$)(?=\s?\d)/g, '\\$');
|
||||
|
||||
// If no LaTeX patterns are found, return the processed content
|
||||
if (!containsLatexRegex.test(processedContent)) {
|
||||
return processedContent;
|
||||
}
|
||||
|
||||
// Convert LaTeX expressions to a markdown compatible format
|
||||
processedContent = processedContent
|
||||
.replace(inlineLatex, (match: string, equation: string) => `$${equation}$`) // Convert inline LaTeX
|
||||
.replace(blockLatex, (match: string, equation: string) => `$$${equation}$$`); // Convert block LaTeX
|
||||
|
||||
return processedContent;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue