Skip to content

fix: preserve $$ and other special chars in user input template substitution#6755

Open
octo-patch wants to merge 2 commits intoChatGPTNextWeb:mainfrom
octo-patch:fix/issue-6574-template-replace-special-chars
Open

fix: preserve $$ and other special chars in user input template substitution#6755
octo-patch wants to merge 2 commits intoChatGPTNextWeb:mainfrom
octo-patch:fix/issue-6574-template-replace-special-chars

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #6574

Problem

When a user sends a message containing $$...$$ (LaTeX display math), the double dollar signs get silently converted to single dollar signs ($...$, inline math) in the stored message. The preview bubble (shown while typing) renders correctly because it uses the raw input string directly, but after submission the content passes through fillTemplateWith(), which uses String.prototype.replace() with the user input as the literal replacement string.

JavaScript's String.prototype.replace() treats several patterns in the replacement string as special directives:

Pattern Meaning
$$ Inserts a literal $
$& Inserts the matched substring
$' Inserts the string after the match
$` Inserts the string before the match

So "{{input}}".replace(/{{input}}/g, "$$e=mc^2$$") returns "$e=mc^2$" — stripping one pair of dollar signs.

The same issue affects any user input containing $&, $', or $`.

Solution

Use a function callback in .replace() instead of a string literal. When the replacement is a function, JavaScript passes no special treatment to its return value:

// Before (buggy)
output = output.replace(regex, value.toString());

// After (fixed)
output = output.replace(regex, () => value.toString());

Testing

// Verify the fix
const buggy = "{{input}}".replace(/{{input}}/g, "$$e=mc^2$$");
// → "$e=mc^2$"  ← WRONG

const fixed = "{{input}}".replace(/{{input}}/g, () => "$$e=mc^2$$");
// → "$$e=mc^2$$"  ← CORRECT

All other special patterns ($&, $', $`) are also fixed by this single change.

octo-patch added 2 commits April 12, 2026 12:14
…Grok-4 models (fixes ChatGPTNextWeb#6593)

xAI's Grok-3-mini and Grok-4 models do not accept the presence_penalty
and frequency_penalty request parameters. Sending these params causes
an API error. This change detects these model families and removes the
unsupported parameters from the request payload.
…hatGPTNextWeb#6574)

When user input contains JavaScript special replacement patterns like
$$, $&, $', or $`, String.prototype.replace() interprets them as
replacement directives:
  - $$ → $ (literal dollar sign)
  - $& → the matched substring
  - $' → string after match
  - $` → string before match

This caused e.g. $$e=mc^2$$ (display math) to be stored as
$e=mc^2$ (inline math) after going through fillTemplateWith().

Fix: use a function callback in .replace() to treat the replacement
value as a literal string, bypassing all special replacement patterns.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 使用$$ $$块表示行间公式时不能正确显示,并且会自动去掉一对$。

1 participant