Skip to content

fix: close emphasis before a tilde after punctuation in GFM - #4298

Open
phatstraws wants to merge 1 commit into
markedjs:masterfrom
phatstraws:fix-em-strong-before-tilde
Open

phatstraws wants to merge 1 commit into
markedjs:masterfrom
phatstraws:fix-em-strong-before-tilde

Conversation

@phatstraws

Copy link
Copy Markdown

Marked version: 18.3.0 (master fe0d349)

Markdown flavor: GitHub Flavored Markdown

Description

With gfm: true, bold or italic text that ends in punctuation does not close when a ~ follows it:

**Note:**~

**Price:**~$5

*(approx.)*~

Expectation

Same as GitHub and the gfm reference on the demo page:

<p><strong>Note:</strong>~</p>
<p><strong>Price:</strong>~$5</p>
<p><em>(approx.)</em>~</p>

Result

<p>**Note:**~</p>
<p>**Price:**~$5</p>
<p>*(approx.)*~</p>

With gfm: false the same input renders correctly, and dropping the ~ also fixes it.

Cause

#3569 and #3577 made ~ count as a non-punctuation character next to a * delimiter in GFM, so *~~a~~*b closes. On the other side of the delimiter that also applies: in **Note:**~ the closing ** sits between : and ~, which matches rule (3) of emStrongRDelimAst (punctuation before, non-punctuation after) and is taken as a left delimiter only.

cmark-gfm handles this by looking past a run of ~ when it checks the characters around a delimiter. This change does that for the character after a delimiter in emStrongRDelimAstGfm: each lookahead now skips ~* before classifying the next character. The character before the delimiter keeps the current ~-as-letter handling, so the strikethrough_in_em_strong cases are unchanged. **ETA:**~3 days stays literal, which is also what GitHub does, since the closer is followed by a digit once the tilde is skipped.

Only the GFM * closer regex changes. CommonMark, pedantic and _ rules are untouched. npm run test:recheck -- inline.gfm.emStrongRDelimAst reports the new regex as safe.

Contributor

  • Test(s) exist to ensure functionality and minimize regression: test/specs/new/em_strong_before_tilde.md fails on master and passes with this change. npm run test:specs (2059) and npm run test:unit (288) pass, and npm run test:lint is clean.
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

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

With gfm on, ~ counts as a letter next to an emphasis delimiter so that
*~~a~~*b closes like GitHub does. That also made a closing delimiter
that follows punctuation and precedes ~ look like an opener only, so
**Note:**~ and **Price:**~$5 stayed literal.

Look past a run of ~ when checking the character after a closing *
delimiter, which is how cmark-gfm treats the tildes.
@vercel

vercel Bot commented Nov 29, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Compared GFM output with cmark-gfm on 326 inputs: *, **, _ and __ emphasis around plain, punctuation-ending and quoted text, followed by ~, ~~, ~$5, ~x, strikethrough and so on. Master differed from cmark-gfm on 136 of them and this branch on 40, with none getting worse. The remaining 40 are all _ / __ emphasis followed by ~ and a letter, which cmark-gfm doesn't emphasize; they're the same on master.

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