Skip to content

🐛 fix(markdown): add remarkMath and remarkCjkFriendly to formatMarkdo…#124

Open
PoseidonLi0514 wants to merge 1 commit intolobehub:masterfrom
PoseidonLi0514:master
Open

🐛 fix(markdown): add remarkMath and remarkCjkFriendly to formatMarkdo…#124
PoseidonLi0514 wants to merge 1 commit intolobehub:masterfrom
PoseidonLi0514:master

Conversation

@PoseidonLi0514
Copy link

@PoseidonLi0514 PoseidonLi0514 commented Mar 3, 2026

💻 变更类型 | Change Type

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

🔀 变更说明 | Description of Change

Fixed the issue where underscores in LaTeX math formulas were being incorrectly escaped.

Problem:

When users input math formulas containing underscores (e.g., $a_n$) in the editor, the markdown content retrieved via getDocument('markdown') would escape the underscore to $a\_n$, causing incorrect math rendering.

Root Cause:

The formatMarkdown method in markdown-data-source.ts only used the remarkGfm plugin during markdown serialization, without loading the remarkMath plugin. This caused remark to fail to recognize the $...$ math formula syntax, treating it as plain text and escaping the underscore _ as a potential emphasis marker.

Solution:

Added remarkMath and remarkCjkFriendly plugins to the formatMarkdown method, aligning with the parsing flow in parse.ts:

// Before fix
remark()
  .use([[remarkGfm, { singleTilde: false }]])
  .processSync(markdown);

// After fix
remark()
  .use(remarkCjkFriendly)
  .use(remarkMath)
  .use([[remarkGfm, { singleTilde: false }]])
  .processSync(markdown);

Result:

  • Before: Input $a_n$ → Output $a\_n$
  • After: Input $a_n$ → Output $a_n$

📝 补充信息 | Additional Information

  • This fix ensures that parsing (parse) and serialization (formatMarkdown) use the same set of remark plugins, guaranteeing data consistency
  • The remarkMath plugin handles both parsing and serialization of math formulas, correctly recognizing $...$ and $$...$$ syntax after being added
  • Also added remarkCjkFriendly to maintain full consistency with parse.ts

🔗 Related Issues

Fixes lobehub/lobehub#12303

…wn to prevent underscore escaping in math formulas
@vercel
Copy link

vercel bot commented Mar 3, 2026

@PoseidonLi0514 is attempting to deploy a commit to the LobeHub OSS Team on Vercel.

A member of the Team first needs to authorize it.

@lobehubbot
Copy link
Member

👍 @PoseidonLi0514


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.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 3, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates markdown serialization to use the same remark plugin stack as parsing by adding remarkMath and remarkCjkFriendly to formatMarkdown, fixing incorrect escaping of underscores in LaTeX math formulas.

Sequence diagram for updated markdown serialization with remark plugins

sequenceDiagram
    participant MarkdownDataSource
    participant remarkProcessor as remark
    participant remarkCjkFriendly
    participant remarkMath
    participant remarkGfm

    MarkdownDataSource->>remarkProcessor: remark()
    activate remarkProcessor
    remarkProcessor->>remarkCjkFriendly: use(remarkCjkFriendly)
    remarkProcessor->>remarkMath: use(remarkMath)
    remarkProcessor->>remarkGfm: use(remarkGfm, singleTilde=false)
    MarkdownDataSource->>remarkProcessor: processSync(markdown)
    remarkProcessor->>remarkMath: handle_inline_and_block_math
    remarkMath-->>remarkProcessor: preserve_math_syntax
    remarkProcessor->>remarkGfm: apply_gfm_rules
    remarkGfm-->>remarkProcessor: format_markdown
    remarkProcessor-->>MarkdownDataSource: result.toString()
    deactivate remarkProcessor
Loading

Class diagram for MarkdownDataSource formatMarkdown plugin usage

classDiagram
    class DataSource {
    }

    class MarkdownDataSource {
      -formatMarkdown(markdown: string) string
    }

    class remark {
      +use(plugin: any) remark
      +use(plugin: any, options: any) remark
      +processSync(markdown: string) VFile
    }

    class remarkCjkFriendly {
    }

    class remarkMath {
    }

    class remarkGfm {
    }

    class VFile {
      +toString() string
    }

    MarkdownDataSource --|> DataSource
    MarkdownDataSource ..> remark : uses
    remark ..> remarkCjkFriendly : plugin
    remark ..> remarkMath : plugin
    remark ..> remarkGfm : plugin
    remark --> VFile : returns
Loading

File-Level Changes

Change Details Files
Align markdown serialization plugins with parsing to correctly handle math and CJK text.
  • Extend remark pipeline in formatMarkdown to include remarkCjkFriendly before other plugins.
  • Add remarkMath to the remark pipeline so inline and block math syntax is recognized during serialization.
  • Keep remarkGfm configuration (singleTilde: false) unchanged while augmenting the plugin chain.
src/plugins/markdown/data-source/markdown-data-source.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

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 reviewed your changes and they look great!


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.

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: Underscore "_" is automatically escaped to "\_" after sending message, causing subscript rendering issues

2 participants