@@ -12,7 +12,7 @@ Current version indicated by LITEVER below.
1212-->
1313
1414<script id="init-config">
15- const LITEVER = 285 ;
15+ const LITEVER = 286 ;
1616 const urlParams = new URLSearchParams(window.location.search);
1717 var localflag = urlParams.get('local'); //this will be replaced automatically in embedded kcpp
1818 const STORAGE_PREFIX = (localflag?"e_":"")+"kaihordewebui_";
@@ -4151,6 +4151,15 @@ Current version indicated by LITEVER below.
41514151 {
41524152 return str.replace(new RegExp(escapeRegExp(find), (caseInsensitive?'gi':'g')), replace);
41534153 }
4154+ function literalReplace(str, search, replacement) {
4155+ // Handle edge cases
4156+ if (search === null || search === undefined || str === null || str === undefined || search === "") {
4157+ return str;
4158+ }
4159+ const index = str.indexOf(search);
4160+ if (index === -1) { return str; }
4161+ return str.slice(0, index) + replacement + str.slice(index + search.length);
4162+ }
41544163 function rgb_to_hex(rgbColor) { //convert rgb color to hex
41554164 rgbColor = rgbColor.split("(")[1];
41564165 rgbColor = rgbColor.split(")")[0];
@@ -6447,6 +6456,23 @@ Current version indicated by LITEVER below.
64476456 }
64486457 return outp + "</table>";
64496458 };
6459+ function stashLatex(text) { //this function preserves the contents of multiline latex blocks so they don't get corrupted by markdown
6460+ const latexBlocks = [];
6461+ let counter = 0;
6462+ text = text.replace(/(^ {0,6}\\\[\n([\s\S]*?)\n {0,6}\\\] {0,2}$|^\$\$\n([\s\S]*?)\n\$\$$|^ {2}\$\$\n {2}([\s\S]*?)\n {2}\$\$$|\$\$([^\n]+?)\$\$|\\\(([^\n]+?)\\\)|\\\[([^\n]+?)\\\])/gm, (match, p1, p2, p3, p4, p5, p6, p7) => {
6463+ const key = `%%LATEXBLK${counter++}%%`;
6464+ latexBlocks.push({ key, value: match });
6465+ return key;
6466+ });
6467+ return { "text":text, "latexBlocks":latexBlocks };
6468+ }
6469+ function unstashLatex(text, latexBlocks) {
6470+ for(let i=0;i<latexBlocks.length;++i)
6471+ {
6472+ text = literalReplace(text,latexBlocks[i].key,latexBlocks[i].value);
6473+ }
6474+ return text;
6475+ }
64506476 const replaceLatex = (input) =>{
64516477 //all latex patterns except inline tex
64526478 input = input.replace(/^<blockquote>(\\\[\n[\s\S]*?\n\\\]) {0,2}<\/blockquote> {0,2}$/gm, (match, p1) => {
@@ -6461,6 +6487,10 @@ Current version indicated by LITEVER below.
64616487 {
64626488 return match;
64636489 }
6490+ if(p6 && p6.startsWith("$")) //buggy match, do not proceed
6491+ {
6492+ return match;
6493+ }
64646494 return leadingWhitespace + temml.renderToString(content); // render LaTeX content
64656495 });
64666496 input = input.replace(/^<blockquote>\n([\s\S]*?)\n<\/blockquote>/gm, (match, p1) => {
@@ -6473,6 +6503,10 @@ Current version indicated by LITEVER below.
64736503 {
64746504 return match;
64756505 }
6506+ if(p1 && p1.startsWith("$")) //buggy match, do not proceed
6507+ {
6508+ return match;
6509+ }
64766510 return prefix+temml.renderToString(content); // render LaTeX content
64776511 });
64786512 input = input.replace(/(^|[^\\])\$([A-Za-z0-9])\$(?!\d)/g, (match, prefix, p1) => { //single letter or number
@@ -6539,6 +6573,13 @@ Current version indicated by LITEVER below.
65396573 append_spcetg = true;
65406574 }
65416575
6576+ let stashres = null;
6577+ if(renderLatex)
6578+ {
6579+ stashres = stashLatex(md);
6580+ md = stashres.text;
6581+ }
6582+
65426583 md = md.replace(/^###### (.*?)\s*#*$/gm, "<h6>$1</h6>")
65436584 .replace(/^##### (.*?)\s*#*$/gm, "<h5>$1</h5>")
65446585 .replace(/^#### (.*?)\s*#*$/gm, "<h4>$1</h4>")
@@ -6606,6 +6647,10 @@ Current version indicated by LITEVER below.
66066647 md = md.replace(/<\/code\><\/pre\>\n<pre\><code\>/g, "\n");
66076648 if(renderLatex)
66086649 {
6650+ if(stashres!=null)
6651+ {
6652+ md = unstashLatex(md,stashres.latexBlocks);
6653+ }
66096654 //to aid the latex renderer, we temporarily add newlines between some blocks
66106655 md = md.replace(/<\/li><li>/gm, "</li>\n<li>");
66116656 md = replaceLatex(md);
0 commit comments