fix(muya): keep every line when a multi-line block becomes a heading (#5366) by Jocs · Pull Request #5367 · marktext/marktext · GitHub
Skip to content

fix(muya): keep every line when a multi-line block becomes a heading (#5366) - #5367

Merged
Jocs merged 1 commit into
developfrom
fix/5366-heading-from-multiline-text
Sep 15, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/5366-heading-from-multiline-text

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026

Copy link
Copy Markdown
Member

Closes #5366

Summary

Turning a block whose text has a line break into an ATX heading kept only the first line in the saved file: a paragraph written as Line one Shift+Enter line two became # Line one with Paragraph › Heading 1, and ###### Line one with Promote Heading; a two-line setext heading lost its second line on Promote. The editor kept showing the second line until the document was reopened.

buildHeadingBlock() (packages/muya/src/block/blockTransforms.ts), used by replaceBlockByLabel for Heading 1–6 and by Promote / Demote, stored the block's text with its newlines. _serializeAtxHeading() reads a heading with /(#{1,6})(.*)/, and . stops at the first newline, so the rest was dropped on save.

An ATX heading is a single line, so join the lines with a space, as muyajs did (newText.replace(/\n/g, ' ')) and as Format already does when it turns a heading line into a quote. Single-line blocks are unaffected, and the caret keeps its offset because the text length does not change.

Steps to reproduce

Checked in the released MarkText 0.20.0-rc.2 (macOS) and in a desktop build of this branch, by clicking the Paragraph menu items with the caret placed by a real click.

  1. In a new document, type Line one, press Shift+Enter, type line two.
  2. Choose Paragraph › Heading 1 (⌘+1) or Promote Heading (⌘+=).
  3. Switch to source code mode, or save and reopen the file.
Block Command 0.20.0-rc.2 This PR
paragraph Line one ⏎ line two Heading 1 # Line one # Line one line two
paragraph Line one ⏎ line two Promote Heading ###### Line one ###### Line one line two
setext heading Line one ⏎ line two / --- Promote Heading # Line one # Line one line two

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__/headingFromMultilineText5366.spec.ts (4 cases: Heading 1 and Promote on a paragraph with a line break, Promote on a two-line setext heading, Heading 3 on a three-line paragraph). Each checks the json state against the block tree and the saved markdown. All 4 fail on develop.
  • New e2e spec editing/heading-from-multiline-text-5366.spec.ts: types the line break with real Shift+Enter, then Heading 2. Fails on develop (## first), passes with the fix.
  • Real desktop app (Playwright _electron, menu items): the first two rows of the table on the packaged 0.20.0-rc.2 and on a build:unpack of this branch.
  • muya: vitest run 235 files / 1667 tests; lint:types, eslint on the changed files and check-circular pass.
  • Manually tested on: macOS

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

…5366)

Heading 1-6 and Promote / Demote Heading build the ATX heading with
`buildHeadingBlock`, which kept the block's newlines in the heading text.
The serializer reads an ATX heading with `/(#{1,6})(.*)/`, so everything
after the first newline was dropped on save: `Line one` + Shift+Enter +
`line two` turned into `# Line one`, and a two-line setext heading lost its
second line on Promote. The editor kept showing the lost text until the
document was reopened.

Join the lines with a space, as muyajs did and as the heading-to-quote
conversion in `Format` already does.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop
@Jocs
Jocs merged commit bd23726 into develop Sep 15, 2026
7 checks passed
@Jocs
Jocs deleted the fix/5366-heading-from-multiline-text branch September 15, 2026 14:48
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] Turning a paragraph with a line break into a heading drops every line after the first

1 participant