Skip to content

feat(lab): add getMarkdown() to RichTextEditor imperative ref - #4509

Merged
potatowagon merged 1 commit into
meta:mainfrom
potatowagon:navi/feat/richtext-get-markdown
Jul 30, 2026
Merged

potatowagon merged 1 commit into
meta:mainfrom
potatowagon:navi/feat/richtext-get-markdown

Conversation

@potatowagon

@potatowagon potatowagon commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds getMarkdown() to the RichTextEditor imperative ref handle (RichTextEditorRef). It serializes the current editor content to a Markdown string.

Why

Parity with EPS RichTextArea (whose ref exposes getMarkdown()), and the natural next step after the ref API (#4417) and the transformers prop (#4416) landed.

Crucially, getMarkdown() uses the same transformers the editor is configured with — not a hardcoded default. This mirrors how Lexical itself works: in the lexical-playground, a single transformer array feeds all three markdown operations (shortcut typing via registerMarkdownShortcuts, import via $convertFromMarkdownString, and export via $convertToMarkdownString). So a consumer who passes custom transformers gets consistent behavior across typing shortcuts and markdown serialization.

How

  • Added getMarkdown: () => string to RichTextEditorRef.
  • EditorRefBridge now receives the memoized transformers array and implements getMarkdown() as editor.getEditorState().read(() => $convertToMarkdownString(transformers)) — run inside a read context, as the $-prefixed API requires.
  • $convertToMarkdownString comes from @lexical/markdown (already an optional peer dep) — a subpackage that resolves to built dist, so this adds no top-level lexical value import (which would otherwise break the sandbox build via lexical's raw-TS declare fields — 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 mention getMarkdown().
  • RichTextEditor.stories.tsx — added a getMarkdown() button to the ImperativeRef story.
  • RichTextEditor.test.tsx — added 3 tests: plain-text serialization, heading with default transformers (# Title), and a custom empty-transformers set (heading emits as plain Title, proving the prop is honored).

Notes

  • getHTML() is split into a separate follow-up PR (it adds @lexical/html as a new optional peer dep), keeping this one dependency-clean.

@vercel

vercel Bot commented Jul 29, 2026

Copy link
Copy Markdown

Deployment failed with the following error:

You don't have permission to create a Preview Deployment for this Vercel project: astryx.

View Documentation: https://vercel.com/docs/accounts/team-members-and-roles

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 29, 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 29, 2026
@github-actions

github-actions Bot commented Jul 29, 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.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 0B

Accessibility Audit

Status: No accessibility violations detected.


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

@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 30, 2026 8:52am

Request Review

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).
potatowagon added a commit to potatowagon/astryx that referenced this pull request Jul 30, 2026
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.
@potatowagon
potatowagon force-pushed the navi/feat/richtext-get-markdown branch from 9cd4b56 to 3991243 Compare July 30, 2026 08:45
@potatowagon potatowagon changed the title feat(lab): add getMarkdown() to RichTextEditor imperative ref feat(lab): add getMarkdown() and getHTML() to RichTextEditor imperative ref Jul 30, 2026
@potatowagon
potatowagon force-pushed the navi/feat/richtext-get-markdown branch from 3991243 to 66893e6 Compare July 30, 2026 08:49
@potatowagon potatowagon changed the title feat(lab): add getMarkdown() and getHTML() to RichTextEditor imperative ref feat(lab): add getMarkdown() to RichTextEditor imperative ref Jul 30, 2026
github-actions Bot added a commit that referenced this pull request Jul 30, 2026
@humbertovirtudes
humbertovirtudes self-requested a review July 30, 2026 09:11

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

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's getMarkdown() for parity, and the name sits naturally next to getEditorState() / getEditor(). No new props, so no boolean/naming surface to worry about.
  • Correct Lexical usage: $convertToMarkdownString is run inside a .read() context, and the bridge's useImperativeHandle dep array is [editor, editable, transformers], so no stale closure and no re-register thrash.
  • Dependency-clean: uses the @lexical/markdown subpackage (already an optional peer dep) and avoids a top-level lexical value import, so it sidesteps the raw-TS declare sandbox-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 an editor.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() to RichTextEditorRef, serializing current content through the editor's configured transformers.
  • Recommendation: Approve. Green across build/test/lint/smoke; scope is clean.

@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 30, 2026
@potatowagon
potatowagon merged commit e1c2bea into meta:main Jul 30, 2026
20 checks passed

This branch was successfully deployed

1 active deployment
Preview — 66893e69 Deployed Jul 30, 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.

2 participants