Skip to content

fix: handle backticks in link text for emphasis processing - #3779

Closed
sajdakabir wants to merge 10 commits into
markedjs:masterfrom
sajdakabir:fix-blockskip-backtick-issue-3777
Closed

fix: handle backticks in link text for emphasis processing#3779
sajdakabir wants to merge 10 commits into
markedjs:masterfrom
sajdakabir:fix-blockskip-backtick-issue-3777

Conversation

@sajdakabir

Copy link
Copy Markdown

[PR] fix: handle backticks in link-like bracket text during emphasis parsing

Marked version: 16.3.0
Markdown flavor: CommonMark | GitHub Flavored Markdown

Description

Expectation

Bold/italic should close correctly even when the text includes bracketed segments that look like links and contain code spans:

**Really weird edge case: bold around what looks like it might be a link, but is actually a link-looking thing with a code specifier in. [Like `this](https://github.com)`. It's now impossible to close the bold**

Expected output:

<p><strong>Really weird edge case: bold around what looks like it might be a link, but is actually a link-looking thing with a code specifier in. [Like <code>this](https://github.com)</code>. It's now impossible to close the bold</strong></p>

Result (before fix)

Emphasis remained unclosed because backticks within the bracketed segment were not masked, interfering with strong/em parsing.

Technical summary

  • The blockSkip regex (used to mask links/code/HTML before emphasis) didn’t handle backticks inside the bracket text [...].
  • Updated pattern for link text to:
    • Exclude backticks from the generic char class.
    • Explicitly match inline code spans within brackets.
    • Simplify escape handling with \\..

New blockSkip:

\[(?:[^\[\]\\`]|\\.|`[^`]*`)*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<(?!)[^<>]*?>

Contributor

  • Tests: existing suite passes (1705 tests), including:
    • angle_brackets (ensures < and > don’t break emphasis)
    • link_tick_redos (guards against ReDOS)

Committer

In most cases, this should be a different person than the contributor.

@vercel

vercel Bot commented Oct 3, 2025

Copy link
Copy Markdown

@sajdakabir is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @sajdakabir, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a specific parsing bug in the Markdown renderer that caused emphasis (like bold or italic) to fail to close correctly when encountering bracketed text that looked like a link but contained inline code spans (backticks). The core of the fix involves updating a regular expression (blockSkip) responsible for identifying and skipping certain text blocks, ensuring that backticks within these link-like structures are properly masked, thereby allowing the emphasis parser to function as expected.

Highlights

  • Emphasis Parsing Fix: Corrects an issue where bold/italic emphasis failed to close properly when text included bracketed segments resembling links that contained code spans (backticks).
  • blockSkip Regex Update: The blockSkip regular expression in src/rules.ts has been modified to properly mask backticks within link-like bracket text before emphasis processing.
  • Improved Markdown Robustness: This change enhances the parser's ability to correctly render complex Markdown constructs, specifically preventing parsing errors in edge cases involving nested formatting.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request fixes an issue where backticks in link text were not handled correctly during emphasis processing. The change updates the blockSkip regular expression to correctly identify and mask code spans within link text. The fix is effective for the described problem. I've added a comment with suggestions to improve the consistency and robustness of the regular expression.

Comment thread src/rules.ts Outdated

// sequences em should skip over [title](link), `code`, <html>
const blockSkip = /\[[^\[\]]*?\]\((?:\\[\s\S]|[^\\\(\)]|\((?:\\[\s\S]|[^\\\(\)])*\))*\)|`[^`]*?`|<(?! )[^<>]*?>/g;
const blockSkip = /\[(?:[^\[\]\\`]|\\.|`[^`]*`)*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<(?! )[^<>]*?>/g;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This is a good fix for handling backticks in link text. However, I have a couple of suggestions regarding the new regex pattern.

  1. Greedy vs. Non-greedy: The new pattern for code spans within link text is `[^`]*` (greedy), while the existing pattern for standalone code spans in blockSkip is `[^`]*?` (non-greedy). For consistency and to avoid potential subtle issues, it would be better to use the non-greedy version (*?) here as well.

  2. Multi-backtick code spans: A more significant point is that this regex pattern (both for standalone code spans and within links) only correctly handles single-backtick code spans. It doesn't support multi-backtick code spans (e.g., code ). This is a pre-existing limitation, but it's being replicated in the new logic. An unmasked code span containing characters like * could interfere with emphasis parsing, leading to incorrect rendering.

For example, with **[``*text*``](url)**, blockSkip would fail to mask the link and code span correctly, exposing *text* to the emphasis parser.

While a full fix for multi-backtick spans might be out of scope for this PR, I'd recommend at least making the quantifier non-greedy for consistency. The suggestion below applies this change.

Suggested change
const blockSkip = /\[(?:[^\[\]\\`]|\\.|`[^`]*`)*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<(?! )[^<>]*?>/g;
const blockSkip = /\B(?:[^\[\]\\`]|\\.|`[^`]*?`)*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<(?! )[^<>]*?>/g;

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/\[(?:[^\[\]\\`]|\\.|`[^`]*?`)*?\]\((?:\\.|[^\\\(\)]|\((?:\\.|[^\\\(\)])*\))*\)|`[^`]*?`|<(?! )[^<>]*?>/g

Is vulnerable to ReDOS. You can check it by pasting it into https://makenowjust-labs.github.io/recheck/playground/

@sajdakabir
sajdakabir requested a review from UziTech October 5, 2025 08:50
@sajdakabir

Copy link
Copy Markdown
Author
Screenshot 2025-10-05 at 2 20 59 PM @UziTech lmk if any changes are needed.

@vercel

vercel Bot commented Oct 7, 2025

Copy link
Copy Markdown

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

Project Deployment Preview Comments Updated (UTC)
marked-website Ready Ready Preview Comment Oct 12, 2025 3:32am

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you remove the indentation changes?

@sajdakabir

Copy link
Copy Markdown
Author

@UziTech its done

Can you remove the indentation changes?

@UziTech its done

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks 💯

@@ -0,0 +1,2 @@
<p><strong>Really weird edge case: bold around what looks like it might be a link, but is actually a link-looking thing with a code specifier in. [Like <code>this](https://github.com)</code>. It&#39;s now impossible to close the bold</strong>.</p>
<p><strong>Bold text with [unmatched `backtick](url) should close</strong>.</p> No newline at end of file

@styfle styfle Oct 8, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@UziTech I noticed this doesn't match the commonmark output.

Are we okay with that?

link to dingus

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@styfle thanks! I’ve updated the test — this should be resolved now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think @styfle was talking about the second paragraph. That is a different bug. Can we just remove the second paragraph from the test?

The first paragraph isn't actually testing this fix currently

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@UziTech so should i remove the second paragraph?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ya and undo the last fix

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@UziTech its done , let me know if any other changes are needed.

@UziTech

UziTech commented Oct 12, 2025

Copy link
Copy Markdown
Member

Looks like this was actually fixed in the latest version by #3785

@UziTech UziTech closed this Oct 16, 2025
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.

Link backtick processing seems subtly broken

3 participants