Skip to content

fix(cmake): only highlight standalone numbers, not digits in identifiers - #4438

Merged
joshgoebel merged 3 commits into
highlightjs:mainfrom
xianjianlf2:fix/cmake-number-boundary-4170
Jul 17, 2026
Merged

fix(cmake): only highlight standalone numbers, not digits in identifiers#4438
joshgoebel merged 3 commits into
highlightjs:mainfrom
xianjianlf2:fix/cmake-number-boundary-4170

Conversation

@xianjianlf2

Copy link
Copy Markdown
Contributor

What

Fixes the CMake grammar so a digit at the start of an identifier is no longer highlighted as a number. For example, in:

add_subdirectory(3rdparty/foo)

the leading 3 of 3rdparty was being wrapped in a hljs-number span.

Why

src/languages/cmake.js used hljs.NUMBER_MODE, whose begin (\b\d+(\.\d+)?) has only a leading word boundary, so it matched the 3 inside 3rdparty. This matches the fix suggested by the maintainer in the issue: use a number mode with a trailing \b as well.

How

Replaced hljs.NUMBER_MODE with an inline mode:

{ className: 'number', begin: /\b\d+(\.\d+)?\b/, relevance: 0 }

Testing

  • Extended the test/markup/cmake/ fixture with add_subdirectory(3rdparty/foo) and the expected markup where 3 is not wrapped in a number span.
  • Confirmed the fixture fails before the change and passes after (ONLY_LANGUAGES=cmake mocha test/markup).
  • Existing CMake version-number highlighting (2.8.8) is unchanged.
  • Added a CHANGES.md entry.

Closes #4170

The default number mode matched a leading digit even when it was the
start of an identifier, so the `3` in `add_subdirectory(3rdparty/foo)`
was highlighted as a number. Override the number mode with a `begin`
pattern that requires a trailing word boundary (`/\b\d+(\.\d+)?\b/`) so
only real, standalone numbers are matched.

Closes highlightjs#4170
Comment thread src/languages/cmake.js Outdated
hljs.QUOTE_STRING_MODE,
hljs.NUMBER_MODE
{
className: 'number',

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.

Suggested change
className: 'number',
scope: 'number',

Comment thread src/languages/cmake.js
// require a word boundary so digits that merely begin an
// identifier (e.g. the `3` in `3rdparty`) are not highlighted
begin: /\b\d+(\.\d+)?\b/,
relevance: 0

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.

Not necessary.

@joshgoebel joshgoebel 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.

Nice, looking good!

@xianjianlf2

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both addressed in 03f30a9 — switched to scope and dropped the comment.

@github-actions

Copy link
Copy Markdown

Build Size Report

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

2 files changed

Total change +55 B

View Changes
file base pr diff
es/languages/cmake.min.js 1.12 KB 1.14 KB +27 B
languages/cmake.min.js 1.12 KB 1.15 KB +28 B

@joshgoebel
joshgoebel merged commit d55d016 into highlightjs:main Jul 17, 2026
19 checks passed
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.

(cmake) Folders starting by a number highlighted incorrectly

2 participants