Skip to content

fix(core): include JSX imports from nested editors - #989

Merged
petyosi merged 8 commits into
mdx-editor:mainfrom
huyanxius:fix/733-nested-jsx-imports
Nov 2, 2026
Merged

petyosi merged 8 commits into
mdx-editor:mainfrom
huyanxius:fix/733-nested-jsx-imports

Conversation

@huyanxius

@huyanxius huyanxius commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #733.

Implementation

  • Register JSX references in stored directive/table MDAST subtrees so newly inserted components receive root imports.
  • Carry internal per-specifier provenance (source, imported/local name, default/namespace kind), rebuilding only used imports. Preserve aliases, namespaces and inherited-versus-local ownership through nested editing.
  • Remove raw import-block preservation, runtime MDX reparsing/cache, exported-binding traversal and global mdast augmentation. ImportStatement keeps its original public shape; this does not introduce general ESM preservation.
  • Use the shared Unicode-aware component identifier guard for root and nested JSX, retaining intrinsic/member/custom-element exclusions and fragment handling.
  • Preserve table contents when nested exports contain imports.
  • Reduce the unit matrix to 8 contract cases, alongside 18 real-editor integrations and existing browser scenarios.

Current main is integrated without rewriting branch history. The already-landed Firefox fixture has no feature diff, and the unrelated Picsum stubs have been removed.

Verification

Head: 9a64b4b09b1e23c2afcf1bfd0b3fa855a9c5417c. Published tree 491dd2e57ac176e108f567a32897b1df8a675b5b matches the tested local tree exactly.

  • Local full Vitest: 266 passed, 1 existing skipped, 1 existing todo.
  • Full lint, typecheck, library/declaration build and 24-package Lexical lockstep check: passed.
  • Emitted declarations contain the original two ImportStatement fields and no new metadata/global augmentation. A bundled MDXEditor-only consumer (without jsxPlugin) retains no acorn/MDX-parser modules. This is dependency-graph validation, not a runtime consumer test.
  • Local native browser startup was blocked by cloud sandbox setup before tests ran. Browser/package/cross-version results must come from the exact-head upstream run below.

Exact-head CI run 40539590451: Test, Build Docs and Build JS have passed, including packed React consumers and cross-version Markdown. Browser compatibility is still running; no browser pass is claimed yet.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Import generation has unresolved correctness issues that can produce invalid MDX.

Review effort: Balanced
Findings: 2 High severity

Open (2)
What changed in this PR

Addresses #733 by extending MDXEditor’s export pipeline to generate root imports for JSX inside directives and tables.

Changes:

  • Collect component references from exported MDAST subtrees.
  • Suppress newly generated imports during table-cell export.
  • Add regression tests and an interactive reproduction example.
File Description
tests/​browser/​nested-jsx-imports.spec.ts Tests insertion, import deduplication, and save/reload.
src/​test/​nested-jsx-imports.test.tsx Tests subtree traversal and import selection.
src/​test/​nested-jsx-imports-integration.test.tsx Tests nested-editor and table-cell exports.
src/​plugins/​table/​TableEditor.tsx Disables fresh imports during cell export.
src/​exportMarkdownFromLexical.ts Collects descriptor-backed JSX references from MDAST.
src/​examples/​bug-733.tsx Adds an interactive nested-JSX reproduction.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/exportMarkdownFromLexical.ts Resolved
Comment thread src/exportMarkdownFromLexical.ts Resolved

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

Two export regressions need fixes before merging. The fragment failure was reproduced with the exporter and a minimal visitor on main and this revision. Full test suites were not run because dependencies were not installed.

Comment thread src/exportMarkdownFromLexical.ts Resolved
Comment thread src/exportMarkdownFromLexical.ts Resolved

@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 picking this up. The admonition case from #733 now gets its import, and the TableEditor change fixes a real bug where a cell with a source-backed component was wiped. Some documents that round-trip on main now break, though.

1. Nested member-expression and non-HTML lowercase tags produce invalid imports (src/exportMarkdownFromLexical.ts, registerNestedComponents)

The new walk registers node.name as-is. With a wildcard descriptor { name: '*', source: '@fallback' }:

Input main this PR
:::tip\n<UI.Badge />\n::: unchanged, parses import { UI.Badge } from '@fallback', MDX parse error
:::tip\n<my-element />\n::: parses import { my-element } …, parse error
<UI.Badge /> in a table cell parses parse error
<svg /> / <foo /> in a directive no import import { svg } / import { foo }

Once saved, the editor can't load the document again. Root-level <UI.Badge /> already has this problem on main, but this change spreads it to directives and tables. Please use one identifier predicate for both registration paths (this walk and LexicalJsxVisitor.traverseNestedJsxNodes), e.g. only register names matching /^[A-Z_$][\w$]*$/, or the root object of a member expression. That also replaces the img special case. Currently <Outer><img /></Outer> at root still emits import { Outer, img } through the visitor path.

2. Root imports used only in nested content get the wildcard source

import { Foo } from './custom'\n\n:::tip\n<Foo />\n::: with a wildcard @fallback now exports import { Foo } from '@fallback'. With import { Foo as Bar } from './x' and nested <Bar />, it imports a name that doesn't exist. Main drops the import; this PR replaces it with a wrong one, silently. The root ESM is discarded at import time, so nested nodes have no importStatement to carry the original source. Please either carry the root import declarations through to nested JSX nodes, or skip registration when the source can't be resolved, rather than falling back to the wildcard.

3. Export cost

Each mdxjsEsm node without ESTree data is parsed again (micromark + acorn) on every root export, and that runs on every update. With 100 directives carrying ESM, exportLexicalTreeToMdast goes from ~0.7 ms to ~6.4 ms. Caching the fallback parse by node.value fixes this.

Smaller things

  • Most of the 74 unit cases are a 14 × 2 × 2 matrix of export-declaration shapes. Together with collectPatternBindings, that's more ESM coverage than the editor needs. A handful of representative shapes plus the integration tests would be easier to maintain.
  • Please also check that a table cell holding a JSX node with an existing importStatement (e.g. pasted from root) doesn't put that import at children[0] and wipe the cell, since addImportStatements: false only blocks new imports.

@huyanxius
huyanxius marked this pull request as ready for review October 17, 2026 05:51
Maintainer review identified overlapping declaration coverage in nested JSX import tests.

Combine representative binding and re-export cases while retaining exact and wildcard descriptor checks.

Preserve parser metadata and reconstructed ESM coverage without changing production or integration tests.

Copy link
Copy Markdown
Contributor Author

Follow-up on the October 13 review, verified against current head ca64d4c:

  1. Both JSX registration paths now use the same identifier guard. Fragments, member-expression names, lowercase/custom tags, and nested intrinsic images do not produce invalid generated imports. Actual Lexical fragment and root/nested round-trip regressions are covered.
  2. Original root imports are carried through nested-editor reconstruction, preserving source, default/named aliases, and namespace bindings instead of replacing them with the wildcard source. Preserved imports and locally declared exports participate in deduplication.
  3. Metadata-less ESM parsing is cached by source text with a bounded cache. Regression coverage verifies reuse for identical text and reparsing after a text change.
  4. The table exporter selects the paragraph explicitly rather than assuming children[0]. The integration case includes a JSX node with an existing aliased importStatement and verifies the cell content survives.
  5. ca64d4c now trims the oversized declaration matrix to five representative positive groups and six negative groups, retaining exact/wildcard and ESTree/fallback coverage plus all integration tests. This commit changes only the unit-test file.

Validation: 250 unit/integration tests passed (70 nested-JSX cases), with the existing 1 skipped and 1 todo; typecheck, lint, and build passed. Current upstream CI passes Build JS, Build Docs, and Test.

Browser compatibility is still red: 140/141 passed, including all 12 nested-JSX cases. The sole Firefox linked-image failure occurs in setup while waiting for the initial image, before any link interaction. The artifact trace shows slow picsum.photos resolution/redirect and unfinished fastly.picsum.photos image requests, consistent with external image-loading latency. Production code and browser fixtures are unchanged from the previous all-green head; no timeout or assertion was weakened. I attempted a failed-job rerun, but GitHub returned 403 “Resource not accessible by integration”, so no rerun started. Could someone with Actions permission rerun that failed job?

The PR description now reflects the current head and this outstanding CI result.

The linked-image setup can time out while Picsum redirects or image responses remain pending.

Fulfill the two existing image URLs with a fixed SVG before navigating to the story.

Keep the original URLs, link assertions, and timeout while removing external image availability from the test.
@huyanxius

Copy link
Copy Markdown
Contributor Author

Fixed the linked-image setup in 44cbc3d85bc80bca3dd98eacee8837a5462c03e9. The two existing Picsum URLs now receive a fixed 200×300 SVG before navigation, following the table-image test fixture pattern. All original URLs, link/Markdown assertions, and timeouts remain unchanged; no production code changed.

The unchanged test passed once locally. Holding the image requests pending then reproduced the same 5-second setup failure seen in the upstream trace. After the fix, both original scenarios and the temporary stalled-network diagnostic passed in Chromium, Firefox, and WebKit. The diagnostic is excluded from the commit.

Exact-head CI run 39464626931 has completed:

  • Browser compatibility: 141/141 passed, including all linked-image and nested-JSX cases.
  • Test: 250 passed, with the existing 1 skipped and 1 todo; lint and typecheck passed.
  • Build Docs: passed.
  • Build JS: library build, Lexical graph, and packed React 18/19 consumers passed. Cross-version verification then failed installing the legacy package: npm reported read ECONNRESET fetching https://registry.npmjs.org/@mdxeditor%2feditor, before legacy replay.

The unchanged npm run test:cross-version completed locally through the published 4.0.4 / Lexical 0.35 replay. Local checks also passed 93 related unit/integration tests, 30 repository browser cases, typecheck, lint, build, and the Lexical graph check.

The PR is conflict-free, but CI remains red and the review state is still CHANGES_REQUESTED. Could a maintainer rerun the failed Build JS job at this head and re-review the addressed changes? My upstream access is read-only; no rerun was attempted.

@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 the follow-up. The identifier guard and carrying root imports through nested editors both work now: UI.Badge, my-element, svg, aliases and namespace imports in directives and table cells all round-trip. What's left:

1. Import blocks are now re-emitted verbatim. importText stores the whole mdxjsEsm node, and consecutive ESM lines form one node. So import { A } from './a'\nimport { B } from './b'\n\n<A /> now keeps the unused B, and import { A } from './a'\nexport const meta = 1\n\n<A /> keeps the export. Main drops both. Whether a declaration survives now depends on whether it happens to share a block with a used import. Please carry structured data per specifier (source, imported name, local name, default/namespace) instead of the raw text, and rebuild only the specifiers in use. That also removes the need for the fallback MDX parse, esmParseCache and collectPatternBindings.

2. Local exports in nested content are only protected until the first edit. Take a directive that contains export const Existing = () => null and <Existing />. The nested editor drops the ESM node on import, as on main. After an edit and blur, collectPreservedBindings no longer sees Existing, and a wildcard descriptor then generates import { Existing } from '@fallback'. With (1) in place this logic can probably go. If it stays, please add a focus/edit/blur integration test for it.

3. Bundle and API surface. exportMarkdownFromLexical now imports micromark-extension-mdxjs and fromMarkdown at runtime, so users without jsxPlugin pull in acorn. The declare module 'mdast' augmentation ships to every consumer. Both go away with (1). Please keep the new ImportStatement fields internal.

Smaller things

  • /^[A-Z_$][\w$]*$/ now also applies at the root, so a root <Äpfel /> with a matching descriptor no longer gets its import. \p{Lu}/\p{ID_Continue} with the u flag would cover it.
  • Please drop the Firefox clipboard fixture (now on main) and the Picsum stubbing in linked-image.spec.ts. They are unrelated to #733.
  • The unit matrix is still 70 cases. A handful of cases plus the integration tests would be enough.

Integrate current main without rewriting branch history. Reconstruct only used import specifiers, keep metadata internal, remove runtime ESM reparsing, and preserve Unicode component identifiers.

Copy link
Copy Markdown
Contributor Author

Reworked this in 9a64b4b09b1e23c2afcf1bfd0b3fa855a9c5417c following your review.

  • Imports now use internal per-specifier provenance and emit only used bindings, including aliases/defaults/namespaces and nested ownership.
  • Removed runtime MDX reparsing/cache, raw import-block preservation, exported-binding traversal and global mdast augmentation. The public ImportStatement shape is unchanged.
  • Shared the Unicode-aware component guard between root and nested JSX; retained fragments, intrinsic/member exclusions and table content handling. Reduced the unit matrix to 8 contract tests plus 18 real-editor integrations.
  • Removed the unrelated Picsum stubs. After integrating current main, the Firefox fixture contributes no diff.

Local full tests passed (266, plus 1 existing skip/1 todo), along with lint, typecheck, build and declaration checks. The no-jsxPlugin consumer dependency graph retains no acorn/MDX-parser modules. Exact-head CI passed Test, Build JS and Build Docs, including packed React 18/19 consumers and cross-version replay. Browser validation remains incomplete: its job was cancelled while installing Playwright OS dependencies; the browser test step was skipped. This supersedes the earlier pending CI status in the description; no browser pass is claimed.

@petyosi
petyosi merged commit 436fc0b into mdx-editor:main Nov 2, 2026
3 of 4 checks passed
@petyosi

petyosi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks for reworking it. Rebuilding only the used specifiers fixed a number of cases main got wrong, too.

I added a follow-up commit (bdaf8ea) for three remaining cases:

  • a component declared with export inside a directive was also imported from a wildcard descriptor, and MDX hoists nested ESM, so the file declared it twice;
  • components named in uncased scripts (for example 组件) were treated as intrinsic elements;
  • a component renamed through a custom editor kept its old import binding.

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

[BUG] missing import statements when adding a jsx element inside a nested editor

3 participants