Skip to content

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

Draft
huyanxius wants to merge 1 commit into
mdx-editor:mainfrom
huyanxius:fix/733-nested-jsx-imports
Draft

huyanxius wants to merge 1 commit into
mdx-editor:mainfrom
huyanxius:fix/733-nested-jsx-imports

Conversation

@huyanxius

Copy link
Copy Markdown

Fixes #733.

Problem

Directives and tables export stored MDAST subtrees directly, so JSX inside them never reaches the JSX visitor that registers component imports. Inserting a descriptor-backed component into an admonition therefore saves the component without its root-level import.

#734 preserved existing imports, but left newly inserted nested components unresolved. This change keeps that existing-import behavior and supplies the missing root imports.

Changes

  • Collect descriptor-backed JSX references from the exported MDAST before the existing import assembly and deduplication. Exact descriptors retain priority over wildcard descriptors; HTML elements and fragments do not generate imports.
  • Suppress newly generated imports during table-cell export, as nested editors already do. Otherwise the cell's first exported node can be an import instead of a paragraph, corrupting its contents when the cell is saved.
  • Add 14 focused unit/integration tests, a small bug-733 example, and browser coverage for insertion, shared root/nested references, table contents, and save/reload.

Verification

Three-browser red/green run:

  • npm run typecheck, npm run lint, npm run test:once, and npm run build passed. Unit suite: 136 passed, with the existing 1 skipped and 1 todo unchanged.
  • Restoring the original exporter from 122347c makes the missing-import assertion fail in Chromium, Firefox, and WebKit.
  • The candidate passes all 9 focused browser cases with no retries or skipped cases. Save/reload compares the full parsed content, excluding source coordinates because blank lines normalize on reload.

The six files in this PR match the tested file blobs. The fork-only validation workflow and Playwright configuration are not included.

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.

// Lexical children, so their JSX references never reach the JSX visitor.
if (addImportStatements) {
const registerNestedComponents = (node: Mdast.Nodes) => {
if ((node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') && node.name !== null && !isMdastHTMLNode(node)) {
Comment on lines +220 to +221
if (descriptor?.source) {
registerReferredComponent(node.name)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

2 participants