[Bug] Cutting a selection that ends in a code block's language box duplicates text and removes the language box · Issue #5371 · marktext/marktext · GitHub
Skip to content

[Bug] Cutting a selection that ends in a code block's language box duplicates text and removes the language box #5371

Closed

Description

@Jocs

Description

Replacing a selection that ends inside a code block's language box (typing, pasting or pressing Backspace over it) corrupts that code block:

  • the part of the language after the caret is copied into the paragraph where the selection starts, while the code block keeps its full language, so the text appears twice;
  • the code block loses its language box: it is gone from the editor, the language can no longer be edited, and undo does not bring it back until the document is reopened;
  • when the code block is inside a list item, the paragraphs between the start of the selection and the code block are not removed.

Shift+↓ from a paragraph above a code block puts the end of the selection in its language box.

Steps to Reproduce

  1. Open a document with:
    intro
    
    - item
    
      ```js
      code
      ```
    
      tail
  2. Click between in and tro.
  3. Press Shift+↓ twice (the selection now ends at the start of the language box js), then press Backspace.
  4. Click in the code line: there is no language box above it any more.

Top-level variant: Intro paragraph / ```js code block / Outro; select from Int|ro paragraph to j|s and type X.

Expected Behavior

The selection ends inside the code block's language line, so that code block stays with its code and its language box; only the selected part of the language is removed, and nothing from the language is merged into the paragraph:

Document Selection Expected
nested (above) `in tro→
top-level `Int ro paragraph→j

Actual Behavior

Measured with real mouse and keyboard input in MarkText 0.20.0-rc.2 on macOS, and on develop @ 8bcb1e0 (muya e2e host):

Document Result
nested saved injs / - item / code block ```js / tail: js is duplicated, the selected item is kept, and the code block has no language box
top-level saved IntXs / code block ```js / Outro: s is duplicated, and the code block has no language box, also after undo

No error is shown.

Screenshots / Logs

Analysis: the cross-block cut in cutSelection() (packages/muya/src/clipboard/cut.ts) merges endBlock.text.substring(endOffset) into the start block and then calls removeBlocks(startBlock, endBlock). With the language box as endBlock, the merged text is the rest of the language, and pruneAfterBranch removes the language box node itself (a content leaf, so no json op is sent) plus nothing before it inside the list item. muyajs never removed a language input (removeTextOrBlock skips it).

Operating System

macOS

MarkText Version

0.20.0-rc.2, develop @ 8bcb1e0

OS Version

macOS 27.0

Reproducibility

  • I can reliably reproduce this issue

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions