Repository navigation
feat(lab): add standalone Markdown <-> EditorState serializer helpers - #4544
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR Modified ComponentsRichTextEditor (@astryxdesign/lab) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: 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.
a27d519 to
4903d81
Compare
freddymeta
left a comment
There was a problem hiding this comment.
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:
lexicalimports stay type-only; runtime imports come only from subpackages (@lexical/headless,@lexical/markdown), keeping the sandbox build safe.- Extracting
DEFAULT_NODESintoeditorNodes.tsas a single source of truth is the right move — headless conversion now registers the same node set as the mounted editor, with aSYNC:note guarding drift. @lexical/headlesswired 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 → mdstability, custom empty-transformers, and consuming the output as a livedefaultValue. 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
RichTextViewread-only preview issue called out in the description looks fine to follow up separately; worth confirming it's preview caching and not aRichTextViewreactivity 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.
freddymeta
left a comment
There was a problem hiding this comment.
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.
What
Adds two standalone Markdown ⇄
EditorStateserializer helpers to the experimentalRichTextEditorsurface (@astryxdesign/lab):markdownToEditorStateJSON(markdown, options?)→ a serializedEditorStateJSON string (drop-in for the editor'sdefaultValue).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 adefaultValuefrom 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 thetransformersprop (#4416).How
markdownSerializers.ts: each helper spins up a headless Lexical editor (createHeadlessEditorfrom@lexical/headless) and runs$convertFromMarkdownString/$convertToMarkdownStringin it. Headless = no DOM, safe in Node/SSR.DEFAULT_NODESinto a sharededitorNodes.ts, imported by bothRichTextEditor.tsxand the serializers, so both register the same node set (headless conversion matches the mounted editor).@lexical/headlessas a new optional peer dependency (peerDependencies + peerDependenciesMetaoptional: true+ devDependencies), matching the other@lexical/*peers.lexicalimports stay type-only in the new files — no top-levellexicalvalue import, so the sandbox build stays safe (thedeclare-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)
markdownToEditorStateJSON,editorStateJSONToMarkdown, andMarkdownSerializerOptionsfromRichTextEditor/index.tsand the@astryxdesign/labbarrel.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 asdefaultValue.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/markdownsubpackages are imported — no top-levellexicalimport.)(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
DEFAULT_NODESextraction. Based directly onmain.