fix(muya): keep the code block when a cut ends in its language line (#5371) by Jocs · Pull Request #5372 · marktext/marktext · GitHub
Skip to content

fix(muya): keep the code block when a cut ends in its language line (#5371) - #5372

Merged
Jocs merged 1 commit into
developfrom
fix/5371-cut-into-language-input
Sep 15, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/5371-cut-into-language-input

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Closes #5371

Summary

Replacing a selection that ends inside a code block's language box (typing, pasting or Backspace) corrupted that code block: the rest of the language was copied into the paragraph where the selection starts while the code block kept its whole language, and the code block lost its language box, so its language could no longer be edited until the document was reopened (undo did not bring it back). Inside a list item, the blocks between the start of the selection and the code block were also left in place. Shift+↓ from a paragraph above lands the end of the selection there.

A cross-block cut merges endBlock.text.substring(endOffset) into the start block and calls removeBlocks(startBlock, endBlock). With the language input as endBlock, the merged text is the rest of the language, and pruneAfterBranch removes the language input node itself (a content leaf, so no json op is sent). muyajs never removed a language input.

New resolveCutEnd() in packages/muya/src/clipboard/cut.ts: a cut that ends in a language line stops before that code block. Nothing from the language joins the start block, the blocks between the start and the code block are removed as usual (up to the last leaf before the code block), and the code block keeps its code and language input and loses only the selected start of its language. The #918 path (a cut that starts in a language line) resolves its end the same way, so a cut from one language line into another collapses the first code block and trims the second.

Steps to reproduce

Checked in the released MarkText 0.20.0-rc.2 (macOS) and in a desktop build of this branch, with real mouse and keyboard input.

  1. Open a document with:
    intro
    
    - item
    
      ```js
      code
      ```
    
      tail
  2. Click between in and tro.
  3. Press Shift+↓ twice (the selection ends at the start of the language box), then Backspace.
  4. Click in the code line and look for the language box above it.
Saved markdown Language box
0.20.0-rc.2 injs / - item / ```js code / tail — js duplicated, the selected item kept gone
This PR in / - ```js code / tail kept, editable

Top-level variant (Intro paragraph / code block / Outro, select Int|ro paragraph → j|s, type X): develop saves IntXs with the code block still ```js and no language box; this PR saves IntX and a code block with language s.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

Notes for reviewers

Rebased onto #5370 (#5368), which changed the same lines in collapseLanguageInputCut: the paragraph now replaces startBlock.getAnchor() (from #5370), and the blocks up to the resolved cut end are removed only when they lie outside that code block (this PR).


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop

…5371)

A cross-block cut merges the end leaf's text after the caret into the start
block and removes the end leaf's branch. When the selection ends inside a
later code block's language line (Shift+Down from a paragraph above lands
there), that merged the rest of the language into the start paragraph while
the code block kept its full language, and `pruneAfterBranch` removed the
language input node itself (a content leaf, so no json op), leaving a code
block whose language could not be edited until the document was reopened.
Inside a list item, the blocks between the start and the code block were
not removed either.

Stop such a cut before that code block: nothing from the language joins the
start block, the blocks before the code block are removed as usual, and the
code block keeps its code and language input and loses only the selected
start of its language. The #918 path (a cut that starts in a language line)
resolves its end the same way.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop
@Jocs
Jocs force-pushed the fix/5371-cut-into-language-input branch from d5adc04 to 2bbca37 Compare September 15, 2026 15:21
@Jocs
Jocs merged commit e542262 into develop Sep 15, 2026
7 checks passed
@Jocs
Jocs deleted the fix/5371-cut-into-language-input branch September 15, 2026 16:16
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] Cutting a selection that ends in a code block's language box duplicates text and removes the language box

1 participant