Skip to content

fix(muya): run paragraph commands on the whole table or code block (#4944) - #5358

Merged
Jocs merged 1 commit into
developfrom
fix/4944-paragraph-commands-whole-block
Sep 15, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/4944-paragraph-commands-whole-block

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Closes #4944

Summary

#4944 is a crash report without steps: Cannot use numerical key for object container, thrown by ot-json1 inside JSONState._flushOperationCache. Earlier fixes covered two actions that throw it (#4913, #5347). Fuzzing current develop found another one, fixed here.

The paragraph commands took "the block at the caret" to be the caret leaf's parent (_immediateBlockAtCursor() → content.parent). In a table cell that is the table.cell; in a code, math, html, diagram or front matter block it is the inner code node. Neither is a block in the json state. With the caret there:

  • a paragraph type the block cannot turn into (Heading 1, Bullet List, Horizontal Line, …) was inserted into the table row or into the code block,
  • Insert Paragraph Before/After inserted the paragraph there,
  • Insert Table inserted or replaced the table there,

and the json flush threw Cannot use numerical key for object container. After that it threw on every later edit, so nothing else reached the saved markdown. The desktop disables the Paragraph menu inside tables and code blocks, but the editor context menu's Insert Paragraph items and the command palette run the same commands there.

Resolve the block from the leaf's anchor (getAnchor(), the whole table or code-like block), as muyajs getAnchor did, and keep the muyajs rules for such a block (isAllowedTransformation, insertParagraph, createFigure):

  • it is never converted into another type,
  • Insert Table does not replace it just because its first leaf is empty; the table goes below it,
  • no paragraph is inserted before front matter.

Toggling a code block off (Code Fences with the caret in code) still works: _toggleIfActive runs first.

Steps to reproduce

Checked on the released MarkText 0.20.0-rc.2 (macOS) and on a desktop build of this PR (with #5357), with real mouse and keyboard input. To set up a document, open a new tab, switch to source code mode (⌘⌥S / Ctrl+Alt+S), paste the markdown, and switch back. After the first error every later edit throws again until the file is reopened, so use a new tab for each step.

| a | b |
| --- | --- |
| 1 | 2 |

```js
code
```

1. Insert Paragraph from the context menu

  1. Click in the table cell 2, right-click, choose Insert Paragraph After, then type below.
    • 0.20.0-rc.2: "Unexpected error" with Cannot use numerical key for object container; below is never saved.
    • With this PR: a paragraph below after the table.
  2. Click in code, right-click, choose Insert Paragraph Before, then type above.
    • 0.20.0-rc.2: the same error.
    • With this PR: a paragraph above before the code block.

2. Paragraph commands from the command palette

The Paragraph menu (and its shortcuts) is disabled in tables and code blocks, but the command palette (⌘⇧P / Ctrl+Shift+P) runs the same commands.

  1. Click in the table cell 2 (or in code), open the command palette and run Heading 1.
    • 0.20.0-rc.2: the same error; nothing typed afterwards is saved.
    • With this PR: nothing changes, and typing continues in the cell or the code.
  2. Click in code, run Table, and click OK in the dialog.
    • 0.20.0-rc.2: the same error.
    • With this PR: the table is inserted after the code block.
  3. Click in code, run Paragraph (the command named just "Paragraph", not Create / Delete / Reset Paragraph).
    • 0.20.0-rc.2: no error dialog, but the code block is gone from the saved document.
    • With this PR: nothing changes.

3. Front matter

---
title: x
---

body

Click in title: x, right-click, choose Insert Paragraph Before.

  • 0.20.0-rc.2: the same error.
  • With this PR: nothing changes (no paragraph is inserted before front matter).

Also covered by the unit tests: Bullet List / Horizontal Line with the caret in a math or html block (the same error), and Heading 1 in the empty language input of a code block without a language (on develop the code block is replaced by an empty heading).

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

  • New unit tests in src/__tests__/paragraphMenuInTableOrCode4944.spec.ts (13 cases). Each flushes the json state and compares it with the block tree. All 13 fail on develop.
  • New e2e spec editing/paragraph-menu-in-table-or-code-4944.spec.ts (table cell, code block; checks the saved markdown and page errors). Both fail on develop; Chromium repeated 6/6 with the fix.
  • Real desktop app (Playwright _electron, real mouse clicks and keyboard; the context-menu items were driven through the mt::cm-insert-paragraph message they send): every step above on the packaged 0.20.0-rc.2 gives the listed result, and on a build:unpack of this branch (with fix(muya): don't drop blocks when a cut ends inside a code block (#4903, #5148) #5357) gives no error and the listed document.
  • muya: vitest run 230 files / 1642 tests (existing updateParagraph, insertParagraph, createTable and front matter specs included); lint:types, eslint on the changed files and check-circular pass.
  • Fuzzing with random edits, pastes, cuts, undo/redo, search/replace and paragraph commands on nested documents: this error came from paragraph commands in 2 of 4 seeds before the fix and 0 of 90 seeds after it (run together with fix(muya): don't drop blocks when a cut ends inside a code block (#4903, #5148) #5357).
  • Manually tested on: macOS

Notes for reviewers

The fuzzing run also hit errors outside #4944, not changed here: a table's get path on a detached row (7 of 90 seeds), Cannot read properties of undefined (reading 'trim') in the table serializer, Invalid offset - splits unicode bytes, and a Maximum call stack size exceeded in fast-diff.


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

…4944)

The paragraph commands took "the block at the caret" to be the caret
leaf's parent. In a table cell that is the cell, and in a code, math, html,
diagram or front matter block it is the inner code node; neither is a
block in the json state. So with the caret there:

- Paragraph > Heading 1 (or another type the block cannot turn into)
  inserted the heading into the table row or into the code block,
- Insert Paragraph Before/After inserted the paragraph there,
- Insert Table inserted or replaced the table there,

and the json flush threw "Cannot use numerical key for object container",
then kept throwing on every later edit, so nothing else reached the saved
markdown. The desktop disables the Paragraph menu in tables and code
blocks, but the command palette runs these commands directly.

Resolve the block from the leaf's anchor (the whole table or code-like
block), as muyajs `getAnchor` did, and keep muyajs's rules for it: such a
block is never converted into another type, Insert Table never replaces
it because its first leaf is empty, and no paragraph is inserted before
front matter. Toggling a code block off still works.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop
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.

Unexpected error with message Cannot use numerical key for object container

1 participant