Skip to content

feat(rust): registerMarkdownShortcuts and KEY_ENTER_COMMAND, with a JS-recorded scripts/markdown-shortcuts vector - #157

Open
tmm wants to merge 1 commit into
rust-markdownfrom
rust-markdown-shortcuts
Open

tmm wants to merge 1 commit into
rust-markdownfrom
rust-markdown-shortcuts

Conversation

@tmm

@tmm tmm commented Nov 14, 2026

Copy link
Copy Markdown
Member

Description

The Rust port had no markdown shortcuts: typing # , - or **bold**
left the markdown as text. It also had no KEY_ENTER_COMMAND, which the
shortcuts' Enter handler listens on.

  • markdown::register_markdown_shortcuts ports MarkdownShortcuts.ts.
    An update listener runs the element, multiline element, text match and
    text format transformers as markdown is typed. A KEY_ENTER_COMMAND
    handler runs the code fence and the element transformers that trigger
    on Enter. It honours the collaboration, historic and composition-end
    tags, the typed-one-character check ($getOffsetInParent) and
    HISTORY_PUSH_TAG.
  • KEY_ENTER_COMMAND takes a KeyboardEvent (modifier keys and
    preventDefault), or None for JS's null. Its JS handlers are
    ported: registerRichText's (with $escapeFormatsForTrigger on the
    default triggers, and the block-decorator NodeSelection case),
    registerPlainText's, and CodeExtension's $exitCodeNodeOnEnter
    handler in register_code.
  • ops.ts gets a markdown script flag
    (registerMarkdownShortcuts(editor, TRANSFORMERS)) and
    KEY_ENTER_COMMAND. The Rust and Swift script runners read both.
  • scripts/markdown-shortcuts (15 cases) is recorded from JS. It covers
    headings, quotes, lists, code fences on space and on Enter, inline
    formats and links, what cancels them, several characters at once, the
    composition-end, collaboration and historic tags, and undo. It also adds
    one KEY_ENTER_COMMAND case each to scripts/rich-text-commands,
    scripts/plain-text and scripts/code, leaving their existing cases
    unchanged.

Swift matched JS on every new case, so no Swift source changes.

Test plan

Before

$ cargo run -p lexical-conformance -- scripts/
| `scripts/markdown-shortcuts` | – | – | – | – | – | not implemented |

After

$ cargo run -p lexical-conformance
| `scripts/code` | 10 | 10 | 0 | 0 | 0 | pass |
| `scripts/markdown-shortcuts` | 15 | 15 | 0 | 0 | 0 | pass |
| `scripts/plain-text` | 5 | 5 | 0 | 0 | 0 | pass |
| `scripts/rich-text-commands` | 7 | 7 | 0 | 0 | 0 | pass |
| **Total** | 655 | 655 | 0 | 0 | 0 | |

$ swift test --filter ScriptTests   # Linux, Swift 6.4.2
✔ Suite ScriptTests passed

$ pnpm exec vitest run src/scripts.test.ts   # packages/lexical-conformance
      Tests  19 passed (19)

cargo fmt --all --check, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings, cargo test --workspace --all-features --locked, RUSTDOCFLAGS='-D warnings' cargo doc --workspace --all-features --no-deps --locked and
cargo +1.88 check -p lexical-editor --all-features --locked pass.

…S-recorded scripts/markdown-shortcuts vector

## Description

The Rust port had no markdown shortcuts: typing `# `, `- ` or `**bold**`
left the markdown as text. It also had no `KEY_ENTER_COMMAND`, which the
shortcuts' Enter handler listens on.

- `markdown::register_markdown_shortcuts` ports `MarkdownShortcuts.ts`.
  An update listener runs the element, multiline element, text match and
  text format transformers as markdown is typed. A `KEY_ENTER_COMMAND`
  handler runs the code fence and the element transformers that trigger
  on Enter. It honours the collaboration, historic and composition-end
  tags, the typed-one-character check (`$getOffsetInParent`) and
  `HISTORY_PUSH_TAG`.
- `KEY_ENTER_COMMAND` takes a `KeyboardEvent` (modifier keys and
  `preventDefault`), or `None` for JS's `null`. Its JS handlers are
  ported: `registerRichText`'s (with `$escapeFormatsForTrigger` on the
  default triggers, and the block-decorator NodeSelection case),
  `registerPlainText`'s, and `CodeExtension`'s `$exitCodeNodeOnEnter`
  handler in `register_code`.
- `ops.ts` gets a `markdown` script flag
  (`registerMarkdownShortcuts(editor, TRANSFORMERS)`) and
  `KEY_ENTER_COMMAND`. The Rust and Swift script runners read both.
- `scripts/markdown-shortcuts` (15 cases) is recorded from JS. It covers
  headings, quotes, lists, code fences on space and on Enter, inline
  formats and links, what cancels them, several characters at once, the
  composition-end, collaboration and historic tags, and undo. It also adds
  one `KEY_ENTER_COMMAND` case each to `scripts/rich-text-commands`,
  `scripts/plain-text` and `scripts/code`, leaving their existing cases
  unchanged.

Swift matched JS on every new case, so no Swift source changes.

## Test plan

### Before

```
$ cargo run -p lexical-conformance -- scripts/
| `scripts/markdown-shortcuts` | – | – | – | – | – | not implemented |
```

### After

```
$ cargo run -p lexical-conformance
| `scripts/code` | 10 | 10 | 0 | 0 | 0 | pass |
| `scripts/markdown-shortcuts` | 15 | 15 | 0 | 0 | 0 | pass |
| `scripts/plain-text` | 5 | 5 | 0 | 0 | 0 | pass |
| `scripts/rich-text-commands` | 7 | 7 | 0 | 0 | 0 | pass |
| **Total** | 655 | 655 | 0 | 0 | 0 | |

$ swift test --filter ScriptTests   # Linux, Swift 6.4.2
✔ Suite ScriptTests passed

$ pnpm exec vitest run src/scripts.test.ts   # packages/lexical-conformance
      Tests  19 passed (19)
```

`cargo fmt --all --check`, `cargo clippy --workspace --all-targets
--all-features --locked -- -D warnings`, `cargo test --workspace
--all-features --locked`, `RUSTDOCFLAGS='-D warnings' cargo doc
--workspace --all-features --no-deps --locked` and
`cargo +1.88 check -p lexical-editor --all-features --locked` pass.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1daf1-150e-709b-aaed-996443a64459

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.

2 participants