Skip to content

feat(lab): add standalone Markdown <-> EditorState serializer helpers - #4544

Merged
potatowagon merged 3 commits into
meta:mainfrom
potatowagon:navi/feat/richtext-serializer-utils
Jul 31, 2026
Merged

potatowagon merged 3 commits into
meta:mainfrom
potatowagon:navi/feat/richtext-serializer-utils

Conversation

@potatowagon

@potatowagon potatowagon commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds two standalone Markdown ⇄ EditorState serializer helpers to the experimental RichTextEditor surface (@astryxdesign/lab):

  • markdownToEditorStateJSON(markdown, options?) → a serialized EditorState JSON string (drop-in for the editor's defaultValue).
  • editorStateJSONToMarkdown(json, options?) → a Markdown string.

Why

The editor already exposes getMarkdown() on its ref, but that needs a mounted editor. These helpers work without one — e.g. to produce a defaultValue from Markdown on the server (SSR), or to serialize saved editor state to Markdown off-screen. This is the "generic Markdown ⇄ EditorState helpers could ship in OSS" gap from the EPS → Astryx parity plan.

Both accept the same { transformers, nodes } the editor uses, so custom transformers/nodes round-trip consistently — the same single-source-of-truth model as the transformers prop (#4416).

How

  • New markdownSerializers.ts: each helper spins up a headless Lexical editor (createHeadlessEditor from @lexical/headless) and runs $convertFromMarkdownString / $convertToMarkdownString in it. Headless = no DOM, safe in Node/SSR.
  • Extracted DEFAULT_NODES into a shared editorNodes.ts, imported by both RichTextEditor.tsx and the serializers, so both register the same node set (headless conversion matches the mounted editor).
  • Added @lexical/headless as a new optional peer dependency (peerDependencies + peerDependenciesMeta optional: true + devDependencies), matching the other @lexical/* peers.
  • lexical imports stay type-only in the new files — no top-level lexical value import, so the sandbox build stays safe (the declare-fields trap from feat(lab): add imperative ref API to RichTextEditor #4417/feat(lab): add maxLength character counter to RichTextEditor #4502).

Docs / exports / tests (SYNC contract)

  • Exported markdownToEditorStateJSON, editorStateJSONToMarkdown, and MarkdownSerializerOptions from RichTextEditor/index.ts and the @astryxdesign/lab barrel.
  • RichTextEditor.doc.mjs — added a usage note.
  • RichTextEditor.test.tsx — 5 tests: heading parse, full round-trip, custom empty-transformers behavior, and consuming the output as defaultValue.

Testing

CI runs the full build/typecheck/lint/test. (Authored on a devserver without a local pnpm toolchain, so relying on CI. Only @lexical/headless + @lexical/markdown subpackages are imported — no top-level lexical import.)

(tested from local host, the storybook preview doesnt seem to update with the latest deployment)

Screen.Recording.2026-07-31.at.3.05.53.PM.mov

^ call out: all works but the read only one doesnt seem to update. will fix in a later PR as need to merge this PR first to unblock the toolbar work

Notes

@vercel

vercel Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Jul 31, 2026 6:58am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 30, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Jul 30, 2026
@github-actions

github-actions Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

RichTextEditor (@astryxdesign/lab) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 800 -
Complexity N/A Very High (51) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/lab N/A 3.4KB 1.4KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

Adds two headless helpers to the RichTextEditor surface:
  - markdownToEditorStateJSON(markdown, opts?) -> serialized EditorState JSON
    (drop-in for the editor's `defaultValue`)
  - editorStateJSONToMarkdown(json, opts?)      -> Markdown string

Unlike the ref's getMarkdown(), these need NO mounted editor — they spin up a
headless Lexical editor (@lexical/headless) and run
$convertFromMarkdownString / $convertToMarkdownString. Useful for producing a
`defaultValue` from Markdown on the server, or serializing saved state to
Markdown off-screen.

Both accept the same { transformers, nodes } the editor uses, so custom
transformers/nodes round-trip consistently (mirrors the transformers-as-single-
source-of-truth model from meta#4416).

- Extracted DEFAULT_NODES into a shared editorNodes.ts (used by the editor and
  the serializers so both register the same nodes).
- Added @lexical/headless as a new OPTIONAL peer dep.
- `lexical` imports stay type-only (no top-level value import) — sandbox-safe.
- Exported from RichTextEditor/index.ts + lab barrel.
- Updated doc.mjs usage notes; added 5 unit tests (heading parse, round-trip,
  custom empty-transformers, defaultValue consumption).
…ayground

The Markdown serializers playground story imports markdownToEditorStateJSON /
editorStateJSONToMarkdown from @astryxdesign/lab, whose markdownSerializers
module imports createHeadlessEditor from @lexical/headless. Because the lab is
aliased to its TS source in the storybook Vite config and @lexical/headless is
declared as an OPTIONAL peer of the lab, Vite's optional-peer-dep resolver
stubbed the module with a throwing shim ("does not provide an export named
'createHeadlessEditor'").

Declare @lexical/headless and @lexical/markdown as direct devDependencies of
the storybook app so Vite pre-bundles the real packages and resolves the
serializer imports to them.
@potatowagon
potatowagon force-pushed the navi/feat/richtext-serializer-utils branch from a27d519 to 4903d81 Compare July 31, 2026 06:55
github-actions Bot added a commit that referenced this pull request Jul 31, 2026

@freddymeta freddymeta 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.

Approve — clean, well-scoped addition.

What it does: Adds headless markdownToEditorStateJSON / editorStateJSONToMarkdown helpers so Markdown ⇄ EditorState conversion works without a mounted editor (SSR / off-screen), complementing the ref's getMarkdown().

Strengths:

  • lexical imports stay type-only; runtime imports come only from subpackages (@lexical/headless, @lexical/markdown), keeping the sandbox build safe.
  • Extracting DEFAULT_NODES into editorNodes.ts as a single source of truth is the right move — headless conversion now registers the same node set as the mounted editor, with a SYNC: note guarding drift.
  • @lexical/headless wired as an optional peer (peerDependencies + peerDependenciesMeta.optional + devDependencies), matching the sibling @lexical/* peers; lockfile and Storybook deps updated.
  • SYNC contract complete: barrel exports (incl. MarkdownSerializerOptions), doc note, and 5 tests covering heading parse, full round-trip, md → json → md stability, custom empty-transformers, and consuming the output as a live defaultValue. That last integration test is the high-value one.
  • {transformers, nodes} options mirror the editor props, so custom config round-trips consistently.

Non-blocking notes:

  • The new Storybook story uses raw inline styles / hex — consistent with the existing stories in this file, so no change needed here; a separate pass could tokenize the story chrome if desired.
  • The deferred RichTextView read-only preview issue called out in the description looks fine to follow up separately; worth confirming it's preview caching and not a RichTextView reactivity bug.
  • Minor: consider a plain feat/ branch prefix for future PRs to match the repo's branch-naming convention.

CI is green across build/test/lint/storybook/check-components. LGTM.

@potatowagon
potatowagon enabled auto-merge (squash) July 31, 2026 07:24
@potatowagon
potatowagon disabled auto-merge July 31, 2026 07:36
@potatowagon
potatowagon enabled auto-merge (squash) July 31, 2026 07:36
@potatowagon
potatowagon requested a review from freddymeta July 31, 2026 07:42

@freddymeta freddymeta 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.

Re-approving — no code changes since the prior review. Type-only lexical imports keep the sandbox build safe, the shared DEFAULT_NODES extraction is the right single-source-of-truth move, optional-peer dependency wiring matches the sibling @lexical/* peers, and the SYNC contract (barrel exports, doc note, 5 tests incl. round-trip + live defaultValue consumption) is complete. CI green. LGTM.

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Jul 31, 2026
@potatowagon
potatowagon merged commit e9ff278 into meta:main Jul 31, 2026
24 checks passed

This branch was successfully deployed

1 active deployment
Preview — 4903d81f Deployed Jul 31, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants