Skip to content

fix: a trailing newline adds a phantom line number - #80

Open
lvolland wants to merge 1 commit into
speed-highlight:mainfrom
lvolland:fix/trailing-newline-gutter
Open

fix: a trailing newline adds a phantom line number#80
lvolland wants to merge 1 commit into
speed-highlight:mainfrom
lvolland:fix/trailing-newline-gutter

Conversation

@lvolland

@lvolland lvolland commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

a code block whose source ends with a newline gets one line number too many.

import { highlightText } from '@speed-highlight/core';

await highlightText('let a = 1\n', 'js')

today the gutter draws 2 numbers while the code column draws 1 line, so the last number sits next to nothing. expected is 1.

the count comes from src.split('\n').length, which counts the empty segment after the final \n. that segment gets no line box under white-space: pre, so it should get no number. the fix drops one trailing newline before counting, inline in the same expression, no new variable:

src.replace(/\n$/, '').split('\n').length

src/index.js goes 6190 -> 6209 bytes (+19).

notes

  • only sources ending in \n change. everything else is byte for byte identical, hideLineNumbers and multiline: false included.
  • \r\n is covered: 'a\r\nb\r\n' gives 2. i checked the counts against the rendered height in chrome, not just the string.
  • only one newline is stripped, so 'a\n\n' still gives 2.
  • dist/ is untouched, so the published bundle keeps the bug until minify-bot regenerates it.
  • no test ships with this. package.json only has build, and the fixtures in examples/languages/ drive tokenize, not highlightText, so there was nowhere to hang it without inventing a suite. happy to add one if you want :)
  • two older miscounts i left alone: an empty source still shows one number, and CR-only line endings still count as one line. both behave the same before and after this patch.

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.

1 participant