Repository navigation
feat(lab): add getMarkdown() to RichTextEditor imperative ref - #4509
Conversation
|
Deployment failed with the following error: View Documentation: https://vercel.com/docs/accounts/team-members-and-roles |
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR No new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Adds `getMarkdown()` to the RichTextEditorRef handle. Serializes the current content to a Markdown string via $convertToMarkdownString, run inside a read context. Honors the same `transformers` the editor is configured with (the prop added in meta#4416), so custom transformers round-trip to Markdown — mirroring the Lexical playground, where one transformer array feeds shortcut typing, import, and export. The bridge now receives the memoized transformers array. `@lexical/markdown` is a subpackage (built dist), so this adds no top-level `lexical` value import — keeping the sandbox build safe. - Extended RichTextEditorRef + EditorRefBridge - Updated doc.mjs ref best-practice note - Added getMarkdown() button to the ImperativeRef storybook story - Added 3 unit tests (plain text, heading with default transformers, and custom empty-transformers set proving the prop is honored) Depends on the merged ref API (meta#4417) and transformers prop (meta#4416).
Adds `getHTML()` to the RichTextEditorRef handle, serializing the current content to an HTML string via $generateHtmlFromNodes (run in a read context; requires a DOM — available in the browser and jsdom tests). Adds `@lexical/html` as a new OPTIONAL peer dependency (peerDependencies + peerDependenciesMeta.optional + devDependencies), matching the other @lexical/* peers. `@lexical/html` is a subpackage (built dist) so this adds no top-level `lexical` value import — the sandbox build stays safe. - Extended RichTextEditorRef + EditorRefBridge - Updated doc.mjs ref best-practice note - Added getHTML() button to the ImperativeRef storybook story - Added a unit test asserting the HTML contains the seeded paragraph text Stacked on the getMarkdown PR (meta#4509) since both extend the same ref handle.
9cd4b56 to
3991243
Compare
3991243 to
66893e6
Compare
humbertovirtudes
left a comment
There was a problem hiding this comment.
Nice, clean addition. getMarkdown() is the natural completion of the ref API after #4417 and #4416, and I like that it serializes through the same memoized transformers the editor is configured with rather than a hardcoded TRANSFORMERS default. That keeps typing shortcuts, import, and export all consistent for a consumer who layers in custom transformers, which mirrors how Lexical's own playground wires a single array through all three operations.
Triage: additive API on an existing lab component, non-breaking, low blast radius, fast path.
What's good:
- API convergence: matches EPS
RichTextArea'sgetMarkdown()for parity, and the name sits naturally next togetEditorState()/getEditor(). No new props, so no boolean/naming surface to worry about. - Correct Lexical usage:
$convertToMarkdownStringis run inside a.read()context, and the bridge'suseImperativeHandledep array is[editor, editable, transformers], so no stale closure and no re-register thrash. - Dependency-clean: uses the
@lexical/markdownsubpackage (already an optional peer dep) and avoids a top-levellexicalvalue import, so it sidesteps the raw-TSdeclaresandbox-build issue from #4417/#4502. The inline comment explains that "why", which is exactly the kind of non-obvious context a comment should carry. - Tests hit the contract, not the implementation: plain-text serialization, heading with the default transformers, and the empty-transformers case that proves the prop is honored (heading emits as plain
Title). Full surface updated:.tsx,.test.tsx,.doc.mjs, and the ImperativeRef story.
Not redundant: #4544 adds standalone serializer helpers that work without a mounted editor and explicitly complements this ref method, so the two are orthogonal.
No changeset, which is correct here: @astryxdesign/lab is private, and the sibling lab PR #4416 landed the same way.
Non-blocking / nit (optional, no need to hold for it):
- The two heading tests seed content via
$convertFromMarkdownString('# Title', TRANSFORMERS)inside aneditor.update()rather than through the public shortcut path. That's a reasonable way to get a deterministic heading node in jsdom, so no change needed, but a one-line note in the second test on why it bypasses shortcuts would save the next reader a beat.
No blocking issues. The needs:code-review gate is auto-applied for the API label, but this is a well-tested, dependency-conscious, additive lab change with clear sibling precedent.
Recap:
- PR: #4509 feat(lab): add getMarkdown() to RichTextEditor imperative ref
- Author: @potatowagon
- What: adds
getMarkdown()toRichTextEditorRef, serializing current content through the editor's configuredtransformers. - Recommendation: Approve. Green across build/test/lint/smoke; scope is clean.
What
Adds
getMarkdown()to theRichTextEditorimperative ref handle (RichTextEditorRef). It serializes the current editor content to a Markdown string.Why
Parity with EPS
RichTextArea(whose ref exposesgetMarkdown()), and the natural next step after the ref API (#4417) and thetransformersprop (#4416) landed.Crucially,
getMarkdown()uses the sametransformersthe editor is configured with — not a hardcoded default. This mirrors how Lexical itself works: in thelexical-playground, a single transformer array feeds all three markdown operations (shortcut typing viaregisterMarkdownShortcuts, import via$convertFromMarkdownString, and export via$convertToMarkdownString). So a consumer who passes customtransformersgets consistent behavior across typing shortcuts and markdown serialization.How
getMarkdown: () => stringtoRichTextEditorRef.EditorRefBridgenow receives the memoizedtransformersarray and implementsgetMarkdown()aseditor.getEditorState().read(() => $convertToMarkdownString(transformers))— run inside a read context, as the$-prefixed API requires.$convertToMarkdownStringcomes from@lexical/markdown(already an optional peer dep) — a subpackage that resolves to built dist, so this adds no top-levellexicalvalue import (which would otherwise break the sandbox build via lexical's raw-TSdeclarefields — the issue fixed in feat(lab): add imperative ref API to RichTextEditor #4417/feat(lab): add maxLength character counter to RichTextEditor #4502).Docs / stories / tests (SYNC contract)
RichTextEditor.doc.mjs— updated the ref best-practice note to mentiongetMarkdown().RichTextEditor.stories.tsx— added agetMarkdown()button to theImperativeRefstory.RichTextEditor.test.tsx— added 3 tests: plain-text serialization, heading with default transformers (# Title), and a custom empty-transformers set (heading emits as plainTitle, proving the prop is honored).Notes
getHTML()is split into a separate follow-up PR (it adds@lexical/htmlas a new optional peer dep), keeping this one dependency-clean.