Skip to content

[lexical-mdast] Feature: Unify MdastExtension and inherit export rules - #9198

Merged
etrepum merged 7 commits into
meta:mainfrom
etrepum:codex/mdast-extension-inherited-rules
Sep 23, 2026
Merged

etrepum merged 7 commits into
meta:mainfrom
etrepum:codex/mdast-extension-inherited-rules

Conversation

@etrepum

@etrepum etrepum commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Breaking Changes

MdastImportExtension and MdastExportExtension and their associated config and output types are now deprecated, you should move all configurations to the unified MdastExtension. Since this is a new @experimental package, this deprecation will only last for one release.

Description

Simplify the @lexical/mdast interface by consolidating import and export into a single MdastExtension instead of separate MdastImportExtension and MdastExportExtension. The original separation was confusing (export rules were configured on the import extension) and really only saved about 4kB for a niche import-only use case.

Other improvements:

  • Widens export type to also accept a concrete LexicalNode class (or the node type string)
  • Export rules follow inheritance and node overrides, most specific rule first. Previously it required an exact type match.
  • Add import/export middleware through context.next(): delegate conversion of the same node to remaining handlers and receive an output array to return or modify. Export chains try same-type rules before ancestor rules; import chains follow contribution order. Nested conversion has independent continuations, and exhausting a chain reaches the generic fallback. null retains its existing default-export/omit-import meaning; [] omits content in both directions.
  • Preserve formatted tabs and overlapping text formats through middleware by grouping eligible output after handlers run. Keep custom structure and metadata intact. Document that these outputs bypass text merging and require valid standalone serialization.

Test plan

Additional unit tests to cover new behavior

…herit export rules

## Description

The core mdast registry is named `MdastImportExtension` even though it configures both import and export. Export rules currently match only exact type strings, so a text rule does not handle tabs or custom text subclasses.

- Make `MdastExtension` the configurable core extension with both import and export APIs. Preserve `MdastImportExtension` as a `@deprecated` alias, its existing output type, and the `MdastExportExtension` compatibility API.
- Allow export rule types to be strings or node classes. Resolve each registered node's nearest matching ancestor at editor build time, preserving contribution order for rules targeting the same type. Ancestors need not be registered; abstract node classes are supported too.
- Let tabs inherit the text rule, and keep inherited custom handlers active during text-run accumulation and selection export.
- Update TypeScript/Flow declarations, documentation, and demo configuration.

The existing `MdastExtension` import/export bundle and the old configurable registry are consolidated. Consequently, import-only editors now also include the serializer; the documentation and size-probe comments call out this packaging change.

## Test plan

### Before

Run the new regression tests on `83e8b4925`:

```sh
node node_modules/vitest/vitest.mjs --project unit --run \
  packages/lexical-mdast/src/__tests__/unit/MdastExportRules.test.ts \
  packages/lexical-mdast/src/__tests__/unit/MdastExtension.test.ts
```

```text
Test Files  2 failed (2)
     Tests  10 failed | 7 passed (17)
```

### After

```sh
pnpm run test-unit packages/lexical-mdast dev-examples/mdast-editor
```

```text
Test Files  7 passed (7)
     Tests  223 passed (223)
```

Also passed:

- Source tree-shaking tests (335 tests).
- `pnpm exec tsc --noEmit` and the mdast demo's TypeScript check.
- `pnpm run build-types`.
- `pnpm run flow` (`Found 0 errors`).
- ESLint and Prettier on the changed code/files; `git diff --check`.

Tests ran on Linux with Node 24 and jsdom. The real-browser/OS matrix was not exercised.
…in mdast rules

## Description

Use node class references for built-in and demo export rules without adding package dependencies. Normalize rule keys to strings and reject classes with no own node type using an invariant during editor construction.

Document the current API without migration history or obsolete bundle-size measurements.

## Test plan

### Before

On aa5bbba with the new rejection tests:

```sh
node node_modules/vitest/vitest.mjs --project unit --run packages/lexical-mdast/src/__tests__/unit/MdastExportRules.test.ts
```

```text
Test Files  1 failed (1)
     Tests  3 failed | 9 passed (12)
```

### After

```sh
pnpm run test-unit packages/lexical-mdast dev-examples/mdast-editor scripts/__tests__/unit/treeShakingSource.test.ts
```

```text
Test Files  8 passed (8)
     Tests  560 passed (560)
```

TypeScript checks for the monorepo and mdast demo, ESLint, Prettier, and git diff --check also pass. Tests ran on Linux with Node 24 and jsdom; the real-browser/OS matrix was not exercised.
## Description

Both deprecated extension names now alias MdastExtension, and both deprecated
output types alias the fully documented MdastExtensionOutput interface. Update
the demo to use the canonical extension and remove the unused source shim.

Reject export-rule classes that inherit their type without declaring one,
including after node metadata has been cached. Keep legacy static getType
classes supported. Add formatted-tab regressions for the built-in text rule
and the demo's HTML text rule, and preserve the accumulated Markdown format
when importing a TabNode.

Remove the redundant import-only size probe and restore the README's
legacy-versus-mdast size comparison without its import-only row. Correct the
probe's production-artifact filename check.

## Test plan

### Before

Run the updated regression tests on c0b85fd:

```sh
node node_modules/vitest/vitest.mjs --project unit --run \
  packages/lexical-mdast/src/__tests__/unit/MdastExtension.test.ts \
  packages/lexical-mdast/src/__tests__/unit/MdastExportRules.test.ts \
  packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts \
  dev-examples/mdast-editor/src/__tests__/unit/MdastCustomConstructs.test.ts
```

```text
Test Files  4 failed (4)
     Tests  6 failed | 154 passed (160)
```

### After

```sh
pnpm run test-unit packages/lexical-mdast dev-examples/mdast-editor scripts/__tests__/unit/treeShakingSource.test.ts
```

```text
Test Files  8 passed (8)
     Tests  565 passed (565)
```

Also passed:
- pnpm exec tsc --noEmit
- pnpm --dir dev-examples/mdast-editor exec tsc --noEmit
- pnpm run build-types
- pnpm run flow (Found 0 errors)
- ESLint and Prettier on changed files
- node --check dev-examples/mdast-editor/size/measure.mjs
- git diff --check

Tests ran on Linux with Node 24 and jsdom. The real-browser/OS matrix was not
exercised. The README restores the existing size snapshot; it was not remeasured.
## Description

The handler type documentation promises a chain of registered handlers, but
compilation selects one handler per type. Document the existing behavior:
a null export result uses the default export, while a null import result
omits the node and its children. Clarify this in the package README too.

Add regressions for null export results with both string and class rules,
including lower-priority rules and a custom parent TextNode rule, and for
null import results. Comment that export rules resolve against registered
node classes, including replacements.

## Test plan

### Before

This is a documentation correction; the new behavior tests already pass on
the existing implementation. The inaccurate documentation was:

```sh
rg -n 'defers to the next registered handler' packages/lexical-mdast/src/types.ts
```

```text
106: * nodes. Returning `null` defers to the next registered handler.
155: * defers to the next registered handler.
```

### After

```sh
pnpm run test-unit packages/lexical-mdast/src/__tests__/unit/MdastExportRules.test.ts packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts
```

```text
Test Files  2 passed (2)
     Tests  113 passed (113)
```

Also passed: pnpm exec tsc --noEmit, ESLint and Prettier on changed TypeScript
files, and git diff --check. Tests ran on Linux with Node 24 and jsdom; the
real-browser/OS matrix was not exercised.
## Description

Import and export handlers currently cannot delegate to lower-priority rules.
Compile ordered handler chains and expose context.next(), which converts the
same node through the remaining handlers and returns a normalized output array.
Export chains put same-type rules before ancestor rules, nearest first; import
chains retain contribution order. Each continuation captures its node, chain,
and position so nested conversion cannot disturb it.

Preserve null semantics: default export on export, omission on import. An empty
array omits content in either direction. Exhausting a chain invokes the generic
fallback. Keep the ordinary text accumulation fast path, and group eligible
formatted output after custom text middleware runs so pass-through middleware
preserves tabs and overlapping formats. Preserve custom structure and metadata.

Update TypeScript, Flow, package documentation, and website documentation.

## Test plan

### Before

Run the middleware regressions against 666d57d:

```sh
node node_modules/vitest/vitest.mjs --project unit --run packages/lexical-mdast/src/__tests__/unit/MdastMiddleware.test.ts
```

```text
Test Files  1 failed (1)
     Tests  15 failed | 1 passed (16)
```

### After

```sh
pnpm run test-unit packages/lexical-mdast dev-examples/mdast-editor scripts/__tests__/unit/treeShakingSource.test.ts
```

```text
Test Files  9 passed (9)
     Tests  584 passed (584)
```

Coverage includes mixed string/class priority, unregistered ancestors, node
replacements, nested import/export, selection slicing, generic fallbacks,
null/empty output, formatted tabs in inline/block walks, overlapping formats,
and preservation of custom output and metadata.

Also passed:
- pnpm exec tsc --noEmit
- pnpm --dir dev-examples/mdast-editor exec tsc --noEmit
- pnpm run build-types
- pnpm run flow (Found 0 errors)
- ESLint and Prettier on changed TypeScript files
- git diff --check

Tests ran on Linux with Node 24 and jsdom; the real-browser/OS matrix was not
exercised.
… output

## Description

The uppercase middleware example only visits top-level text output, so bold,
italic, and inline-code output remain unchanged. Walk the returned mdast tree
to uppercase text inside formatting containers and inline code.

Explain that extra fields such as data and custom structures prevent merging
text output, preserving those fields while making middleware responsible for
valid standalone serialization. Compare next(), null, and empty-array results
for import and export in one table.

## Test plan

### Before

Extract the original README snippet and invoke its export handler with next()
returning representative mdast output (capture the handler with import stubs):

```sh
node /tmp/mdast-readme-probe.cjs /tmp/mdast-readme-before.ts
```

```text
plain: PLAIN (PASS)
bold: bold (FAIL)
italic: italic (FAIL)
nested: nested (FAIL)
code: code (FAIL)
```

### After

```sh
node /tmp/mdast-readme-probe.cjs packages/lexical-mdast/README.md
```

```text
plain: PLAIN (PASS)
bold: BOLD (PASS)
italic: ITALIC (PASS)
nested: NESTED (PASS)
code: CODE (PASS)
```

The extracted example also passes TypeScript checking against the repository's
source declarations. git diff --check passes. This change only edits the
README; the runtime test suite and real-browser/OS matrix were not rerun.
@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 23, 2026 3:47am UTC
lexical-playground Ready Ready Preview Sep 23, 2026 3:47am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 23, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 23, 2026
@etrepum
etrepum added this pull request to the merge queue Sep 23, 2026
Merged via the queue into meta:main with commit 0095060 Sep 23, 2026
46 checks passed
@etrepum etrepum mentioned this pull request Sep 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 7fb05f68 Deployed Sep 23, 2026 by vercel[bot]
Preview – lexical-playground — 7fb05f68 Deployed Sep 23, 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 Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants