Skip to content

[Bug] preprocessLaTeX mutates fenced code block content in Default render mode #146

Description

@rickintoplace

Describe the bug

In Default render mode, fenced code blocks containing regex character classes are rendered incorrectly.

Reproduction

Input:

|[\]\\]/g
const re = /[.*+?^${}()|[\]\\]/g;

Actual result in Default mode

|[$\$/g
const re = /[.*+?^${}()|[$\$/g;

Expected behavior

Expected result

|[\]\\]/g
const re = /[.*+?^${}()|[\]\\]/g;

Screenshots or logs

The same content renders correctly in Markdown and Plaintext mode.
The issue only occurs in Default, which calls:

preprocessLaTeX(contentToRender)

The problem appears to be in MarkdownRenderer.jsx, inside preprocessLaTeX.

Code blocks are temporarily replaced with placeholders, but then restored before these global replacements are executed:

processedContent = processedContent
  .replace(/\\\[/g, "$$")
  .replace(/\\\]/g, "$$")
  .replace(/\\\(/g, "$")
  .replace(/\\\)/g, "$");

As a result, code block content is modified. For example, the \] sequence inside a regex is interpreted as a LaTeX delimiter and replaced.

There is also a second issue: in JavaScript replacement strings, "$$" means a single literal $, not two dollar signs. So this replacement:

.replace(/\\\]/g, "$$")

inserts $, not $$.

Suggested fix

Restore code blocks only after the LaTeX delimiter replacements, and use replacement callbacks for literal dollar signs:

  processedContent = processedContent.replace(/<<LATEX_(\d+)>>/g, (_, i) => {
    return latexExpressions[parseInt(i)];
  });

- processedContent = processedContent.replace(
-   /<<CODE_BLOCK_(\d+)>>/g,
-   (_, i) => {
-     return codeBlocks[parseInt(i)];
-   }
- );

  processedContent = processedContent
-   .replace(/\\\[/g, "$$")
-   .replace(/\\\]/g, "$$")
-   .replace(/\\\(/g, "$")
-   .replace(/\\\)/g, "$");
+   .replace(/\\\[/g, () => "$$")
+   .replace(/\\\]/g, () => "$$")
+   .replace(/\\\(/g, () => "$")
+   .replace(/\\\)/g, () => "$");

+ processedContent = processedContent.replace(
+   /<<CODE_BLOCK_(\d+)>>/g,
+   (_, i) => {
+     return codeBlocks[parseInt(i)];
+   }
+ );

Environment

No response

Confirmation

  • I have confirmed this bug exists on the latest version
  • I have searched through existing issues
  • I have checked the Network connection
  • I reproduced the bug couple of times to ensure the source of that

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions