Skip to content

fix(editor): preserve safe rich Markdown literals - #22425

Closed
nwparker wants to merge 3 commits into
mainfrom
nwparker/external-22014-markdown-escaping
Closed

nwparker wants to merge 3 commits into
mainfrom
nwparker/external-22014-markdown-escaping

Conversation

@nwparker

@nwparker nwparker commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​179 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​179
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​27 $\color{#cf222e}{\Huge{\mathbf{−}}}$​6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​21

ELI5

Editing rich Markdown keeps safe underscores and nested callouts readable in the saved source.

What Changed

Extend the existing literal-source preservation guard to Unicode intraword underscore runs and nested quote/list blocks. Each optional unescape is accepted only after reparsing produces the same editor node. Keep reference-like text conservative, bound the newly added validation to 50,000 code units per block and reuse the existing node cache. Preserve the existing large paragraph/heading bracket path, so long wiki-link prose keeps its established source fidelity.

Why

Removing escapes by regex alone can turn literal __init__ or ___init___ into emphasis on reopening. The existing parse-and-node-equality guard proves the saved syntax retains its meaning. Adapted from @nvimq #22123; attribution is preserved.

Linked Issue

Related to #22014 and #22123. This covers safe underscores and nested callouts; it does not claim to preserve every source-formatting detail.

Visual Proof

Real hidden macOS Orca save/reopen flow in a folder workspace. Before/after saved source, baseline b76bc79d73d and the same preservation change from the prior validation pass. Cropped to exclude local paths. Equality was checked from saved Source-to-Rich state to reopened state, not against an unsaved pre-serialization snapshot.

22014-before.png
22014-after.png

Testing

  • I manually tested these changes locally
  • Automated tests added/updated

Branch checks: all 368 rich-Markdown tests pass (2 skipped), including the existing large malformed-link paragraph regression; renderer types and changed-code quality pass. Prior full focused packet: 141 tests and hidden save/reopen checks passed; 11 regressions failed on baseline.

Review

Reviewed Unicode and escaped-backslash handling, delimiter runs, reference-definition interactions across blocks, raw HTML/code source ownership, bounded reparse work and cache invalidation. Unsafe or unproven changes retain the canonical escaped output.

Agent skill upstream boundary

  • Not applicable; no agent skill source changes.

Notes

Blank-line/final-newline normalization, other escape characters, definition-like/unsafe blocks and oversized blocks outside the existing paragraph/heading bracket path remain unchanged. The size cap and cache tests are not latency benchmarks. Local validation did not include native Windows/Linux/WSL, live SSH, mobile, the full suite or packaging. Required PR Checks and packaging passed in CI; the completed current-head Pullfrog review also reports 1,703 editor tests passed with two skipped. The additional full-diff Pullfrog review completed successfully at 7a1ddd397320 with no new issues; its 43 focused tests also passed. On September 25, those 43 tests passed again locally, and a source/dependency compatibility review against main (85ac14e9c263) found no overlapping Markdown implementation changes or parser dependency changes. The branch remains mergeable without updating its source. Host-aware save routing and remote wire behavior are unchanged.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • Before/after screenshots attached
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered
  • Required PR Checks verification, static analysis, types, tests and packaging pass; focused local checks are listed above

@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 25 seconds.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: e02f1246-99fa-42e5-8584-ab912e5e59ad

📥 Commits

Reviewing files that changed from the base of the PR and between 0b16a31 and 7a1ddd3.

📒 Files selected for processing (2)
  • src/renderer/src/components/editor/rich-markdown-escape-fidelity.test.ts
  • src/renderer/src/components/editor/rich-markdown-literal-serialization.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nwparker

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nwparker

Copy link
Copy Markdown
Contributor Author

@pullfrog Please review the current head, 7a1ddd3. The initial review is still running against 4f3b095. Since then, CI identified the existing large malformed-link paragraph regression; the size limit now preserves the existing paragraph/heading bracket path. All 368 rich-Markdown tests pass locally (2 skipped), along with renderer types and changed-code quality.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The 50,000-code-unit block cap drops literal preservation for large blocks. That regresses the pre-existing rich-markdown-inline-source-tokenization test (a single ~112k-character paragraph) and re-introduces the excessive escaping #22014 reports for any block above the cap. Please resolve before merge (details inline).

Reviewed changes

  • Nested blocks now participate — LITERAL_BLOCK_TYPES adds blockquote, bulletList, orderedList, and taskList, so callouts and list bodies are unescaped and validated as whole containers (rich-markdown-literal-serialization.ts:7). This is the actual fix for the > [!NOTE] corruption in #22014.
  • Unicode-aware optional-escape stripping — withoutOptionalEscapes consumes escaped backslashes first and only strips \_ runs flanked by \p{L}\p{N}\p{M} (rich-markdown-literal-serialization.ts:16), replacing the previous unconditional per-character strip.
  • Bounded reparse work — blocks over 50,000 code units skip validation, and unchanged candidates skip the parse entirely (rich-markdown-literal-serialization.ts:6, :38, :46).
  • Broader definition bail-out — the document-wide guard now matches ]: anywhere rather than only line-start definitions (rich-markdown-literal-serialization.ts:71).
  • Tests — 25 new cases in rich-markdown-escape-fidelity.test.ts covering intraword underscores, delimiter runs, nested containers, and the size boundary.

I independently fuzzed the cross-block property (~9k differential editor round-trips): the per-block eq check plus the doc-wide ]: guard hold up, and I found no input where an emitted candidate recombines into a different document or corrupts a transport envelope.

ℹ️ Nitpicks

  • The document-wide ]: guard (rich-markdown-literal-serialization.ts:71) now bails on any ]: anywhere, so ordinary prose such as user_name [a]:b disables preservation for the whole file and leaves user\_name escaped — broader than needed to catch the nested-definition case. Matching the definition shape (optional blockquote/list prefix, then [label]:) would keep the fix without over-bailing.
  • keeps literal delimiter runs and backslashes safe (rich-markdown-escape-fidelity.test.ts:56) only asserts reopen equality, so it cannot distinguish a preserved-but-escaped output from the intended unescaped one. Asserting the exact expected string would actually pin the __init__ / _word_ behavior the test name claims.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Prior round (4f3b095): broadened LITERAL_BLOCK_TYPES to blockquote/lists, made withoutOptionalEscapes Unicode-aware and escaped-backslash-safe, added a 50,000-code-unit block cap, and widened the ]: reference-definition guard. I flagged that the cap broke the pre-existing rich-markdown-inline-source-tokenization large-paragraph test.
  • This round (7a1ddd3): the cap now exempts paragraph/heading blocks whose serialized markdown contains escaped brackets (rich-markdown-literal-serialization.ts:42), restoring the pre-PR large wiki-link paragraph path while still bounding the newly added nested-container and underscore-only validation.

I re-ran the full src/renderer/src/components/editor suite at 7a1ddd3: 1703 passed, 2 skipped, 0 failed — the previously failing large-paragraph test now passes. The preservesBrackets exemption is exactly the base guard's /\\[[\]]/ condition for paragraph/heading, so it restores existing behavior rather than opening a new path; no new correctness or safety concern. The prior review thread has been resolved.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@nwparker

Copy link
Copy Markdown
Contributor Author

Follow-up to the review of 4f3b095245d2:

  • The large-paragraph regression was already fixed in 7a1ddd397320; the original exact-source test remains unchanged and passes, as do all 368 rich-Markdown tests (2 skipped). Current PR Checks is green.
  • The broad reference-like guard is deliberately conservative in this scoped change. Definitions can affect other blocks, including nested containers; the tests cover those interactions. Narrowing it with another Markdown-shape regex would need a separate grammar/edge-case review. The description lists definition-like text among the remaining formatting limits.
  • The delimiter/backslash cases assert the intended safety property: reopening must preserve the complete editor tree. Literal __init__ and _word_ cannot simply be emitted unescaped, because that would turn them into emphasis. Exact output is pinned for intraword identifiers in the separate preservation cases; the delimiter cases allow safe canonical escaping while rejecting semantic corruption.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the full net diff at head 7a1ddd3 (no prior Pullfrog review exists on this PR, so the network was the two-file change set).

  • Extended the literal-source preservation guard to intraword escaped underscores (Unicode-aware and intraword-only) and to nested blockquote/list block types, replacing the old unconditional \_ unescape with a stricter regex.
  • Bounded the expanded validation to blocks over 50,000 code units (MAX_LITERAL_BLOCK_CODE_UNITS) while exempting paragraph/heading blocks that contain escaped brackets, so long malformed-link prose keeps its established source fidelity.
  • Broadened the reference-definition early-out to \][ \t]*: and added a per-block cache plus a candidate === markdown short-circuit, with rich-markdown-escape-fidelity.test.ts covering Unicode identifiers, delimiter runs, nested containers, definitions, call caching, and the size cap.

The per-block parse-and-node-equality guard remains the safety net: unescaping is accepted only when reparsing the block reproduces the same node, so unsafe or unproven changes retain the canonical escaped output. The head commit's preservesBrackets bypass restores the pre-existing behavior for the dominant large-block path rather than opening a new one. Focused tests pass at head (rich-markdown-escape-fidelity, rich-markdown-literal-serialization, rich-markdown-inline-source-tokenization — 43 tests), and the existing rich-markdown-inline-source-tokenization.test.ts large malformed-link paragraph is the regression the head commit restores.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

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.

1 participant