Skip to content

🐛 fix: prevent nesting of code nodes in selection processing#119

Open
ilimei wants to merge 5 commits intomasterfrom
fix/code_nest
Open

🐛 fix: prevent nesting of code nodes in selection processing#119
ilimei wants to merge 5 commits intomasterfrom
fix/code_nest

Conversation

@ilimei
Copy link
Collaborator

@ilimei ilimei commented Feb 9, 2026

💻 变更类型 | Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 🔨 chore
  • 📝 docs

🔀 变更说明 | Description of Change

  1. 修复 inlineCode markdown快捷键嵌套覆盖问题 [Bug] inline 代码块补全问题 #118

📝 补充信息 | Additional Information

Summary by Sourcery

Prevent markdown inline code shortcut from creating nested code nodes and allow text format transformers to cancel transformations via a boolean return value.

Bug Fixes:

  • Avoid nesting code nodes when applying the inline code markdown shortcut to a selection that already contains a code block.

Enhancements:

  • Allow text format transformer process functions to cancel a transform by returning false, restoring the original selection when canceled.

@vercel
Copy link

vercel bot commented Feb 9, 2026

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

Project Deployment Actions Updated (UTC)
lobe-editor Ready Ready Preview, Comment Feb 9, 2026 11:33pm

Request Review

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 9, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the markdown text-format transformer pipeline to allow transformer-specific cancellation, and uses that capability in the code plugin to prevent applying the inline code shortcut when the current selection already includes a code node (avoiding nested code nodes).

Sequence diagram for markdown text-format processing with cancellable transformers

sequenceDiagram
  actor User
  participant Editor
  participant MarkdownService
  participant TextFormatPipeline
  participant CodeShortcutTransformer
  participant Selection

  User->>Editor: Type inline code shortcut
  Editor->>MarkdownService: handleKeyPress
  MarkdownService->>TextFormatPipeline: $runTextFormatTransformers(selection)
  TextFormatPipeline->>CodeShortcutTransformer: match inline code pattern
  TextFormatPipeline->>Selection: create nextSelection with open/close tags removed
  TextFormatPipeline->>CodeShortcutTransformer: process(nextSelection)
  CodeShortcutTransformer->>Selection: getNodes()
  Selection-->>CodeShortcutTransformer: nodes including CodeNode
  CodeShortcutTransformer-->>TextFormatPipeline: return false
  TextFormatPipeline->>Editor: $setSelection(anchorNode.selectEnd())
  TextFormatPipeline-->>MarkdownService: transform cancelled
  MarkdownService-->>Editor: no inline code applied
  Editor-->>User: Selection restored without nested code node
Loading

Class diagram for updated TextFormatTransformer and CodePlugin behavior

classDiagram
  class TextFormatTransformer {
    <<type>>
    +format: ReadonlyArray~TextFormatType~
    +intraword: boolean
    +process(selection: RangeSelection) boolean void
    +tag: string
    +type: string
  }

  class TextFormatPipeline {
    +$runTextFormatTransformers(editor: LexicalEditor, selection: RangeSelection) boolean
  }

  class RangeSelection {
    +getNodes() Array~LexicalNode~
    +getTextContent() string
    +removeText() void
    +insertNodes(nodes: Array~LexicalNode~) void
    +focus: SelectionPoint
  }

  class SelectionPoint {
    +set(key: NodeKey, offset: number, type: string) void
  }

  class CodePlugin {
    +registerMarkdownShortCuts() void
  }

  class CodeShortcutTransformer {
    +process(selection: RangeSelection) boolean void
  }

  class CodeNode {
    +getType() string
    +getTextContent() string
  }

  class CursorNode {
    +$createCursorNode() CursorNode
  }

  class MarkdownService {
    +registerMarkdownShortCuts(transformers: Array~TextFormatTransformer~) void
  }

  TextFormatPipeline --> TextFormatTransformer : uses
  TextFormatPipeline --> RangeSelection : manipulates
  MarkdownService --> TextFormatPipeline : invokes
  MarkdownService --> TextFormatTransformer : registers
  CodePlugin --> MarkdownService : configures
  CodePlugin --> CodeShortcutTransformer : provides
  CodeShortcutTransformer ..|> TextFormatTransformer : conforms
  RangeSelection --> CodeNode : getNodes returns
  RangeSelection --> CursorNode : insertNodes
  CodeShortcutTransformer --> CodeNode : checks getType
  CodeShortcutTransformer --> CursorNode : inserts
Loading

File-Level Changes

Change Details Files
Allow text-format transformers to cancel a transformation and restore the original selection.
  • Extend the TextFormatTransformer process function signature to optionally return a boolean indicating whether to continue the transform.
  • Update $runTextFormatTransformers so that when a transformer’s process function returns false, it cancels the transform, restores the selection to the original anchor position, and skips further processing.
src/plugins/markdown/service/transformers.ts
Prevent inline code markdown shortcut from nesting code nodes within an existing code selection.
  • Add a guard in the code plugin’s markdown shortcut handler to detect if the current selection includes any CodeNode.
  • If a CodeNode is present in the selection, abort processing by returning false so the generic transformer pipeline cancels the transform.
  • Otherwise, keep the existing behavior of converting the selected text to a CodeNode and inserting a cursor node afterward.
src/plugins/code/plugin/index.ts

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@lobehubbot
Copy link
Member

👍 @ilimei


Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The new process?: (selection) => boolean | void contract combined with if (!matcher.process(nextSelection)) will treat existing process implementations that return void as a cancellation, effectively disabling those transformers; consider checking explicitly for false (e.g. if (matcher.process(nextSelection) === false)) so that legacy void return handlers continue to behave as before.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `process?: (selection) => boolean | void` contract combined with `if (!matcher.process(nextSelection))` will treat existing `process` implementations that return `void` as a cancellation, effectively disabling those transformers; consider checking explicitly for `false` (e.g. `if (matcher.process(nextSelection) === false)`) so that legacy `void` return handlers continue to behave as before.

## Individual Comments

### Comment 1
<location> `src/plugins/markdown/service/transformers.ts:328` </location>
<code_context>

     if (matcher.process) {
-      matcher.process(nextSelection);
+      if (!matcher.process(nextSelection)) {
+        // If process function returns false, cancel the transform and set selection to original position
+        $setSelection(anchorNode.selectEnd());
</code_context>

<issue_to_address>
**issue (bug_risk):** Distinguish explicit `false` from `undefined` when handling `process` return value to avoid breaking existing transformers.

With the new type `process?: (selection: RangeSelection) => boolean | void`, existing `process` functions that return nothing now yield `undefined`, which is falsy and will cancel the transform. Previously, the return value was ignored and all `process` handlers ran, so this change breaks backward compatibility. To preserve existing behavior while adding an explicit cancel signal, capture the return value and only cancel when it is strictly `false`, for example:

```ts
if (matcher.process) {
  const result = matcher.process(nextSelection);
  if (result === false) {
    $setSelection(/* original selection */);
    continue;
  }
  return true;
}
```

This keeps `void` returns as "proceed" and uses `false` to opt into cancellation.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 9, 2026

Open in StackBlitz

npm i https://pkg.pr.new/lobehub/lobe-editor/@lobehub/editor@119

commit: 57cf65b

…when process function cancels transformation
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