Skip to content

feat(muya): preserve ATX and Setext heading source forms - #5878

Merged
Jocs merged 7 commits into
developfrom
codex/markdown-syntax-basic
Oct 24, 2026
Merged

Jocs merged 7 commits into
developfrom
codex/markdown-syntax-basic

Conversation

@Jocs

@Jocs Jocs commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Refs #2189. Based on the merged edit-transaction refactor #5872.

Result

Opening and saving headings currently normalizes source spacing and delimiters. Preserve ATX and Setext heading forms by default: editing ## title ### emits ## changed ### .

This PR is limited to headings. Earlier code-block and other syntax-preservation changes are withdrawn.

Design

  • ATX text owns the complete editable source line, including indentation, separators, closing hashes and trailing whitespace. meta.level retains its semantic role; no ATX syntax/body copy.
  • Setext text owns the original body lines and prefixes. Move the complete underline line from meta.underline into syntax.underline; no prefixes array or legacy-state migration layer.
  • Shared heading helpers handle level changes, paragraph conversion, cursor offsets and structural edits. All structural paste paths preserve closing syntax on the heading and append only the body tail to the final pasted block.
  • API commands and the actual front-menu Turn Into path share selection mapping. Menu mousedown retains the native selection so the conversion transaction records the original history endpoints. Caret/range positions survive level changes, paragraph conversion, undo/redo and subsequent typing.
  • The inline lexer recognizes closing hashes only when its top-level tokens start with an ATX header. Outline output consumes these tokens directly, without rewriting Setext tokens after parsing.
  • Carry Setext syntax through Blocks, cloning, replacement, JSON1 operations and history. Independent body and underline operations retain OT convergence.

All changes are inside packages/muya. No source buffer, persistent source-offset map, syntax-repair transaction rule or opt-in preservation flag. Block gaps, file-boundary blank lines and EOL bytes remain outside this PR's fidelity guarantee; container marker spelling is unchanged.

Validation

Latest follow-up: 9c98b814.

  • Muya unit: 3,041 passed.
  • CommonMark/GFM fixtures: 1,347 passed against the existing expected-failure baseline, with no new failures. Existing engine conformance limits remain.
  • Final frozen-revision Chromium: 591 passed, 3 existing skips.
  • The front-menu group passes 14 real-click cases, including 8 new regressions for caret/range conversion, undo/redo, typing, paragraph-to-heading conversion and an unrelated menu target.
  • Muya and root lint/typecheck, circular dependency check and git diff --check: passed, existing warnings only. GitHub checks are running for the latest revision.

A local full run reused a Vite server that was exiting and failed with connection-refused errors. A fresh, independently owned test server passed the full suite without changing repository test configuration, timeouts or assertions.

9c98b814 fixes the four subsequent review regressions: keep a separate list-marker line when the first heading has indentation; share body-bounded formatting across single/multiple blocks; shift non-ATX source selections by the actual inserted prefix; stop block-prefix parsing before interpreting heading body text as a reference definition. Twelve unit and ten browser regressions cover the reported examples and their adjacent cases. Final local validation: unit 3,041, spec 1,347, Chromium 591 passed / 3 existing skips; lint/types/circular checks passed. Fresh CI checks are running for this revision.

@Jocs Jocs left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I reviewed commit 66728a6f against base 3fcb5ce8, including the parser/serializer boundary, Block projection, transaction completion, creation paths, undo/redo coverage, and cursor APIs.

Recommendation: address the five inline findings before merging. Two are P1 issues (a document-loading exception and code corruption after export/reimport); three are P2 regressions involving language decoding and cursor mapping.

The overall direction is reasonable: separating source forms from semantic metadata, sharing ownership/cloning in SyntaxBlock, and recording required syntax corrections in the same transaction/history entry are useful foundations. Property-level correction operations also reduce interference between unrelated OT edits. However, the separation is incomplete: the lexer, source-form parser, and transaction repair rules each implement part of the Markdown grammar. The parser mismatch and info-string regression illustrate the maintenance risk. Semantic parsing should remain authoritative, while source-form extraction records spelling and transaction repair maintains explicit serialization invariants.

Additional coverage gaps:

  • Interactive fence creation still discards source forms. With preserveSyntax: true, entering ~~~~~ js and pressing Enter produces ~~~js followed by a three-tilde closing fence. ParagraphContent uses defaultCodeSyntax instead of extracting the typed fence length and padding. The language-selector creation path should be checked as well. This is a feature-coverage gap, rather than a claim that the old behavior newly regressed.
  • packages/muya/examples/src/data.ts still documents the old state schema: headings lack syntax, Setext headings use meta.underline, and code blocks use meta.type. These in-repository construction examples should migrate with the schema; this does not require a legacy-state compatibility layer.

Validation: the existing 3,022 unit tests and the 1,347 CommonMark/GFM baseline cases pass locally. Eight additional assertions reproduce the five inline findings. The conformance suite exercises the static HTML renderer, so it does not validate the new state parser/serializer path. Add semantic round-trip checks after editing (parse(print(editedState)) preserves block types, content, and semantic metadata), alongside source-string equality and Block/JSON-state consistency. Include empty-to-nonempty lines, insufficient prefixes, partially expanded tabs, escaped info strings, interactive creation, and cursor APIs. I did not rerun browser E2E tests.

Desktop default integration, external block gaps, EOF/EOL preservation, and other source-form families are explicitly deferred by this PR and are not counted as missing scope here.

Comment thread packages/muya/src/state/syntax.ts Resolved
Comment thread packages/muya/src/state/syntax.ts Resolved
Comment thread packages/muya/src/block/base/format.ts Resolved
Comment thread packages/muya/src/state/syntax.ts Resolved
@Jocs Jocs changed the title feat(muya): preserve source forms for headings and code blocks feat(muya): preserve ATX and Setext heading source forms Oct 24, 2026

@Jocs Jocs left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I found two P2 regressions that I recommend addressing before merge; see the inline comments.

Validation was run on 5981b909: all 3,013 existing unit tests passed after adjusting Vite's file-access setting for the isolated checkout. Focused regression tests reproduced both findings, and the caret-conversion test passes on the PR base. Browser E2E tests were not rerun.

I also checked the subsequent change in 7836af52; it only removes the Setext parsing fallback and does not change either affected path.

Comment thread packages/muya/src/clipboard/paste.ts Resolved
Comment thread packages/muya/src/muya.ts Resolved
@Jocs
Jocs merged commit 1f206c0 into develop Oct 24, 2026
8 checks passed
Zane-0x5a added a commit to Zane-0x5a/marktext that referenced this pull request Oct 27, 2026
Brings in v0.22.0 and v0.23.0 and the start of 0.24.0: Electron 45,
GitHub Alerts, foldable headings, per-tab reading mode and --read-only,
the docked document outline, multiple folder roots, preview tabs, recent
files in quick open, Italian, and the large-document fixes of marktext#4942 and
marktext#5863.

Conflicts:

- muya state/inlineRenderer: marktext#4942 caches reference definitions per
  candidate state, which supersedes the fork's per-revision cache. Took
  upstream's; the fork's display math cache in mathPreview stays.
- editor.vue: kept the fork's deferred content snapshot (no getState()
  deep clone per keystroke), document virtualization and camera, on top
  of upstream's reading mode, shortcut resolvers and image drop. The
  json-change handler keeps upstream's loadingContent/readOnly guard,
  the source handoff and reload take upstream's loadingContent blocks,
  and scrollToHeader reveals folded headings before following the
  target. publishContentChange uses getSelectionState().
- Print/PDF: the fork's paged preview replaces MarkdownPrint and the
  printToPDF export path, so printService, its specs and the pdf branch
  of handleResponseForExport keep the fork's side. marktext#3323 moved image
  resolution into the engine; exportHtml drops its own image rewrite and
  keeps the fork's rendered-heading TOC.
- marktext#5802's assertExportDestination moves to main/filesystem so Save PDF in
  the preview refuses the source document too.
- syntheticHistory keeps the 53-bit cyrb53 hash over upstream's BigInt
  FNV-64 (~6 ms per key on a 500 KB note); the store keeps isSameList
  and drops deep-equal.
- Sidebar rows keep draggable alongside preview tabs; locales keep both
  printPreview and readingMode, and it.json gets the printPreview strings.

Merge adjustments:

- SET_READ_ONLY publishes the pending snapshot first: a read-only tab
  drops content changes, so an edit made within 150 ms of entering
  reading mode would otherwise be missing from the tab.
- marktext#5798 moved notices to the bottom-right corner, over the preview's
  Save PDF and Print buttons, and an export notice stays while hovered.
  The preview lifts notices clear of its actions while it is open.
- Specs: the docked outline is expanded by default and narrows the
  column, so the virtualization specs open it only when it is closed,
  measure the settled position and place their target inside the
  viewport. The camera specs press Control once before measuring,
  because since marktext#5878 the first key press re-renders the caret's heading
  with its marks shown.

Verified on Windows 11 (175%): typecheck, lint, the desktop unit suite
(1467) and the muya unit suite (3978) pass. In background windows the
e2e suite (514 passed, 45 failed) fails only where a build of
upstream/develop fails on this machine too (copy-anchor-link,
document-outline, image clipboard/relative path, 5394, 5685,
toc-active-heading, outline-layout, ...: 56 cases in 19 specs there),
plus media-viewer cases that flake on both when the 1 px image's resize
bar takes the click. print-preview, export-pdf, document-virtualization
and sidebar-file-drag pass, and editor-camera passes on screen.
reading-mode:54 needs the camera focus fix in the next commit.
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