Skip to content

fix(frontmatter): preserve form values and allow a custom editor - #998

Merged
petyosi merged 4 commits into
mdx-editor:mainfrom
huyanxius:feat/19-structured-frontmatter
Nov 2, 2026
Merged

petyosi merged 4 commits into
mdx-editor:mainfrom
huyanxius:feat/19-structured-frontmatter

Conversation

@huyanxius

@huyanxius huyanxius commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Related to #19.

Narrowed implementation

  • Keep the existing small key-value form instead of replacing the default UI with a recursive typed editor.
  • Preserve existing false, 0, empty strings, nulls and structured values when editing unrelated fields. Load/dump with CORE_SCHEMA so dates remain strings. Object/array values are retained but not edited through text inputs.
  • Skip writes on unchanged or edit-reverted saves, preserving original YAML bytes/comments/formatting. Stable row identities preserve original value types even across Add/Delete, index changes and key renames.
  • Add frontmatterPlugin({ FrontmatterEditor }), frontmatterEditor$ and exported component props. Existing nodes update when the option changes; omitting it restores the default. Components receive delimiter-free yaml and onChange.
  • Keep malformed/non-object/unsafe-integer/recursive source unchanged with a translated controlled message, rather than retaining the raw editor.
  • Remove the recursive UI, associated story/tests and plugin-local history workaround. No external package or core history redesign is included.

Current main is integrated without rewriting branch history; the already-landed Firefox fixture has no feature diff. The former recursive implementation is preserved separately, not shipped as the default.

Verification

Head: f9922bee2aa8784f9f13a1a3584a07c7866802b8. Published tree 72758517d1d518187854edfbb0f70150fbe50030 matches the tested local tree exactly.

  • Full local Vitest: 265 passed, 1 existing skipped, 1 existing todo.
  • Focused default-form/extension tests: 25 passed, including four row-operation regressions verified failing before the final correction.
  • Full lint, typecheck, library/declaration build and 24-package Lexical lockstep check: passed.
  • Independent review passed after the row-baseline correction. Emitted declarations expose the requested component cell/props/option, with no removed recursive/history helpers.
  • Native browser execution is unavailable in the cloud sandbox; no new local browser or undo-fix guarantee is claimed.

Exact-head CI run 40541095166 completed successfully: Test, Build JS, Build Docs and Browser compatibility. Browser coverage passed 141 tests (47 each on Chromium, Firefox and WebKit). Build JS also passed packed React 18/19 consumer checks and the cross-version Markdown replay.

Maintainer re-review remains pending; successful tests do not constitute acceptance or merge.

@huyanxius
huyanxius marked this pull request as ready for review October 16, 2026 16:24

@petyosi petyosi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking on #19. The current form does lose data: main drops false, 0 and "" and turns dates into ISO timestamps. Keeping the original bytes on an unchanged save is good. But replacing the default frontmatter UI with a recursive typed editor is more than I want to maintain in the core package. I'd split this into two parts:

1. A small fix to the existing editor. Stop filtering falsy values, use CORE_SCHEMA so dates stay strings, and skip the write when nothing changed. That can merge quickly.

2. An extension point. Add frontmatterPlugin({ FrontmatterEditor }), stored in a cell and used by FrontmatterNode.decorate(), the same way imagePlugin({ ImageDialog }) works. The recursive editor can then ship as a separate package.

If parts of the current implementation stay, these need fixing:

  • The raw fallback can store delimiters. js-yaml accepts a leading ---. If you save ---\ntitle: fixed in the raw fallback, the delimiter is stored inside the node and export adds a second pair. On reload the frontmatter is empty and title: fixed lands in the document body.
  • The root type can be changed. If you switch the root type select to string and save, the output is ---\n''\n---. The root should stay an object.
  • frontmatterHistory.ts writes to @lexical/history internals. Core already manages that history in src/plugins/core/lexicalExtensions.ts. If there is an undo bug, please fix it there, with a test that fails on main.
  • The new labels and error messages are not translated.
  • Please drop the table-image-dialog.spec.ts hunk. That fixture is on main now.

Integrate current main without rewriting branch history. Restore the small default form, preserve falsy/date/unchanged values and row identity, expose the requested component cell, and remove recursive UI and local history manipulation.
@huyanxius huyanxius changed the title feat(frontmatter): support recursive typed YAML editing fix(frontmatter): preserve form values and allow a custom editor Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Narrowed this in f9922bee2aa8784f9f13a1a3584a07c7866802b8 following your review.

  • Restored the existing key/value form; removed the recursive/default raw UI and the plugin-local history workaround. No external package or core history change is included.
  • Preserved falsy values and dates with CORE_SCHEMA, and retained original YAML bytes on unchanged/edit-reverted saves. Regression tests also cover Add/Delete and key renames without losing original types. Structured values remain read-only in this form; unsupported source is preserved with a translated controlled message.
  • Added frontmatterPlugin({ FrontmatterEditor }), the component cell and exported props, including option-update/reset coverage.
  • Integrated current main; the table-image fixture no longer contributes to this diff.

Local checks passed: 25 focused tests, 265 full-suite tests (1 existing skip, 1 todo), lint, typecheck and build. Exact-head CI is green, including 141 browser tests across Chromium/Firefox/WebKit, packed React 18/19 consumers and cross-version Markdown replay. The PR description now reflects this smaller scope.

@petyosi
petyosi merged commit 67a841c into mdx-editor:main Nov 2, 2026
4 checks passed
@petyosi

petyosi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks. This is the shape I was after: a small fix for the existing form plus the FrontmatterEditor option.

I added a follow-up commit (b017477) for a few cases the form still changed on save:

  • multiline strings are now read-only like structured values, because a single-line input drops their line breaks as soon as it is focused;
  • only the serializer's final newline is removed, so |+ block scalars keep their trailing blank lines;
  • setYaml compares against the latest node, so a custom editor that keeps its onChange callback can revert to the original value.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 4.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants