Skip to content

🐛 Bug: JSON parsing bug: Incorrect handling of backslash-escaped quotes #2237

@Adityakk9031

Description

@Adityakk9031

Describe the bug

Describe the bug
The custom JSON parser (implemented in lib/getPartsOfJson.ts) incorrectly identifies string boundaries when a string ends with an escaped backslash.

The parser uses the regex /(?<!)"/g to find the closing quote of a string. This logic is flawed because it assumes any backslash immediately preceding a quote escapes that quote. However, in JSON, a backslash can itself be escaped.

For example, in the valid JSON string "slash: ", the first backslash escapes the second, and the following quote is the closing quote. Because the current regex sees a backslash before the quote, it fails to match it as the end of the string, causing the syntax highlighting and path detection to break for the entire remainder of the code block.

Steps To Reproduce

1.) Open any markdown file in the project (e.g., pages/understanding-json-schema/basics.md).
2.) Add a JSON code block that includes a trailing escaped backslash:

json

{
"bad_string": "backslash: \",
"next_key": "this will be broken"
}
3.) Run yarn dev and navigate to that page.
4.) Observe: The syntax highlighting "leaks" out of the first string. The comma, the second key, and the rest of the block will all be colored as if they were still part of the first string.

Expected Behavior

The parser should correctly handle parity for escape sequences. A quote is only escaped if it is preceded by an odd number of backslashes.

Screenshots

Normal :-

Image

Buggy :-

Image

Device Information [optional]

- OS:
- Browser:
- version:

Are you working on this issue?

Yes

Do you think this work might require an [Architectural Decision Record (ADR)]? (significant or noteworthy)

Yes

Metadata

Metadata

Assignees

Labels

Status: AcceptedIt's clear what the subject of the issue is about, and what the resolution should be.🐛 BugIndicates that the issue is a bug or defect.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions