Skip to content

🐛 fix(math): prevent dollar-space-dollar from being misidentified as inline math#125

Merged
Innei merged 2 commits intomasterfrom
fix/math-inline-dollar-space-false-match
Mar 9, 2026
Merged

🐛 fix(math): prevent dollar-space-dollar from being misidentified as inline math#125
Innei merged 2 commits intomasterfrom
fix/math-inline-dollar-space-false-match

Conversation

@Innei
Copy link
Member

@Innei Innei commented Mar 9, 2026

Summary

Fixes the issue where typing $ $ (dollar, space, dollar) incorrectly triggered inline math, creating an empty math node with italic placeholder styling.

Changes

  • Use negative lookahead (?!\s*$) in the inline math regex to exclude the dollar-space-dollar pattern
  • $ $ and $ $ no longer match
  • Valid patterns like $x$, $E=mc^2$ still work correctly

Made with Cursor

Summary by Sourcery

Bug Fixes:

  • Prevent $ $ and similar empty dollar-delimited sequences from being parsed as inline math placeholders.

…inline math

Use negative lookahead to exclude $ $ pattern, avoiding creation of
empty math nodes with italic placeholder styling.

Made-with: Cursor
@vercel
Copy link

vercel bot commented Mar 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 Mar 9, 2026 11:09am

Request Review

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 9, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the inline math markdown shortcut regex to avoid treating $ $ (and similar space-only content) as valid inline math while preserving valid math patterns.

Class diagram for MathPlugin inline math shortcut regex update

classDiagram
  class MathPlugin {
    +kernel
    +constructor(kernel)
    +init()
  }

  class IMarkdownShortCutService {
    <<interface>>
    +registerMarkdownShortCut(shortcut)
  }

  class MarkdownShortCut {
    +regExp
    +replace(textNode, match)
  }

  class MathInlineNodeFactory {
    +$createMathInlineNode(code)
  }

  MathPlugin --> IMarkdownShortCutService : requires
  MathPlugin --> MarkdownShortCut : registers
  MarkdownShortCut --> MathInlineNodeFactory : uses

  class InlineMathRegExp {
    +pattern "/\\$([^\\s$](?:[^$]*[^\\s$])?)\\$\\s?$/"
  }

  MarkdownShortCut --> InlineMathRegExp : uses
Loading

File-Level Changes

Change Details Files
Tighten inline math detection regex to reject empty or whitespace-only content between dollar delimiters.
  • Replaces the previous inline math regex that matched any non-dollar content between $ delimiters with a stricter pattern that requires at least one non-space, non-dollar character and disallows leading/trailing spaces only.
  • Ensures patterns like $ $ and $ $ no longer create inline math nodes, while valid expressions such as $x$ or $E=mc^2$ continue to match.
src/plugins/math/plugin/index.ts

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

👍 @Innei


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 left some high level feedback:

  • The implemented regex doesn’t match the described approach in the PR (negative lookahead for $\s*$); consider either switching to the negative lookahead version or updating the description so future readers aren’t confused about the intent.
  • The new pattern forbids leading/trailing spaces inside inline math (e.g. $ x$, $x $), not just the $ $ / $ $ cases mentioned; double-check this stricter behavior is desired or relax the regex to only exclude fully-blank math content.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The implemented regex doesn’t match the described approach in the PR (negative lookahead for `$\s*$`); consider either switching to the negative lookahead version or updating the description so future readers aren’t confused about the intent.
- The new pattern forbids leading/trailing spaces inside inline math (e.g. `$ x$`, `$x $`), not just the `$ $` / `$  $` cases mentioned; double-check this stricter behavior is desired or relax the regex to only exclude fully-blank math content.

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 Mar 9, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@lobehub/editor@125

commit: f2ef76f

…corresponding tests

Introduced `isLikelyMathContent` function to heuristically determine if a string resembles a math formula. Added unit tests to validate its functionality, covering various math and non-math inputs. Updated the math plugin to utilize this function for better inline math detection.

Signed-off-by: Innei <tukon479@gmail.com>
@Innei Innei merged commit e742978 into master Mar 9, 2026
6 checks passed
@Innei Innei deleted the fix/math-inline-dollar-space-false-match branch March 9, 2026 12:36
@lobehubbot
Copy link
Member

❤️ Great PR @Innei ❤️


The growth of project is inseparable from user feedback and contribution, thanks for your contribution!
项目的成长离不开用户反馈和贡献,感谢您的贡献!

github-actions bot pushed a commit that referenced this pull request Mar 9, 2026
### [Version&nbsp;4.0.2](v4.0.1...v4.0.2)
<sup>Released on **2026-03-09**</sup>

#### 🐛 Bug Fixes

- **math**: Prevent dollar-space-dollar from being misidentified as inline math.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's fixed

* **math**: Prevent dollar-space-dollar from being misidentified as inline math, closes [#125](#125) ([e742978](e742978))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@lobehubbot
Copy link
Member

🎉 This PR is included in version 4.0.2 🎉

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.

Markdown 解析错误

2 participants