Skip to content

fix(markdown) Ignore mid_word underscores#4342

Open
danvk wants to merge 2 commits into
highlightjs:mainfrom
danvk:markdown-midword-underscores
Open

fix(markdown) Ignore mid_word underscores#4342
danvk wants to merge 2 commits into
highlightjs:mainfrom
danvk:markdown-midword-underscores

Conversation

@danvk

@danvk danvk commented Nov 11, 2025

Copy link
Copy Markdown
Contributor

Resolves #4279

Changes

This changes the markdown rule to ignore mid-word _ characters. This brings highlight.js in line with GFM (GitHub-Flavored Markdown). There's no official standard for Markdown and different renderers vary in how they handle mid_word underscores. Not italicizing them is the more conservative behavior, and avoids the common situation where a large fraction of your Markdown file turns italic with highlight.js when it does not with GitHub.

Checklist

  • Added markup tests
  • Updated the changelog at CHANGES.md

@github-actions

Copy link
Copy Markdown

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

4 files changed

Total change +22 B

View Changes
file base pr diff
es/core.min.js 8.19 KB 8.19 KB -1 B
es/highlight.min.js 8.19 KB 8.19 KB -1 B
es/languages/markdown.min.js 961 B 973 B +12 B
languages/markdown.min.js 960 B 972 B +12 B

Comment thread src/languages/markdown.js
begin: /_(?![_\s])/,
end: /_/,
begin: /(?<![a-zA-Z0-9])_(?![_\s])/,
end: /_(?![a-zA-Z0-9])/,

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.

This will have to sit until v12 unless you know how to do this without negative look-behind... this would be a breaking change for older versions of Safari that v11 still supports.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for taking a look @joshgoebel. Is there any guess when there might be a v12? Is it months, years, maybe never?

Claude came up with a more complex version that doesn't have any negative lookbehind. If this seems reasonable, I can move it over to this PR:
main...danvk:highlight.js:markdown-midword-underscore-no-lookbehind

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.

Hard to say, I would have thought a year ago but then I got busy with life.

I guess if we're never going to need to match on that last character for another mode then it could be gobbled up by the "guard" here, yes... When you pull that over please also leave your negative look-behind in place, just comment it out and add a short comment that it's for v12+.

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.

(markdown) snake_case term causes italics in rest of file

2 participants