Skip to content

Commit 236b3e2

Browse files
pablofdezrclaude
andcommitted
fix(core): detect showLineNumbers anywhere in the meta string
`showLineNumbers` was detected with a reversed-string regex: /srebmuNeniLwohs(?!(.*)(\/))/.test(reverseString(meta)) The negative lookahead was meant to ignore `/showLineNumbers/` used as a highlight word, but it actually rejected `showLineNumbers` whenever any `/` appeared before it in the meta. Since highlight tokens such as `/age/#v` contain slashes, `showLineNumbers` placed after them was silently ignored — so it only worked at the start of the meta string. Replace the reverse-string hack with a token-boundary match that is position-independent and captures the optional start-at number directly: meta.match(/(?:^|\s)showLineNumbers(?:\{(\d+)\})?(?=\s|$)/) Bounding on whitespace/string-ends still excludes `/showLineNumbers/` (slash-delimited), so the highlight-word cases keep working. The `reverseString` import is now unused in this module and removed. Added a fixture covering `showLineNumbers` (and `showLineNumbers{N}`) after highlight tokens; all existing snapshots are unchanged. Closes #204 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ad648c5 commit 236b3e2

3 files changed

Lines changed: 111 additions & 12 deletions

File tree

packages/core/src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import rangeParser from 'parse-numeric-range';
1111
import { unified, type Transformer } from 'unified';
1212
import rehypeParse from 'rehype-parse';
1313
import { charsHighlighter } from './chars/charsHighlighter';
14-
import { reverseString } from './chars/utils';
1514
import {
1615
isElement,
1716
isText,
@@ -427,24 +426,24 @@ export function rehypePrettyCode(
427426
counterMap: new Map<string, number>(),
428427
};
429428

429+
// Detect `showLineNumbers` (optionally `showLineNumbers{N}`) as a
430+
// standalone meta token, wherever it appears in the meta string.
431+
// Bounding on whitespace/string-ends avoids matching a
432+
// `/showLineNumbers/` highlight word, which is slash-delimited.
433+
const showLineNumbersMatch = meta.match(
434+
/(?:^|\s)showLineNumbers(?:\{(\d+)\})?(?=\s|$)/,
435+
);
436+
430437
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
431438
visit(codeTree, 'element', (element) => {
432-
if (
433-
element.tagName === 'code' &&
434-
/srebmuNeniLwohs(?!(.*)(\/))/.test(reverseString(meta))
435-
) {
439+
if (element.tagName === 'code' && showLineNumbersMatch) {
436440
if (element.properties) {
437441
element.properties['data-line-numbers'] = '';
438442
}
439443

440-
const lineNumbersStartAtMatch = reverseString(meta).match(
441-
/(?:\}(\d+){)?srebmuNeniLwohs(?!(.*)(\/))/,
442-
);
443-
const startNumberString = lineNumbersStartAtMatch?.[1];
444+
const startNumberString = showLineNumbersMatch[1];
444445
if (startNumberString) {
445-
const startAt = startNumberString
446-
? Number(reverseString(startNumberString)) - 1
447-
: 0;
446+
const startAt = Number(startNumberString) - 1;
448447
lineNumbersMaxDigits = startAt;
449448
if (element.properties) {
450449
element.properties.style = `counter-set: line ${startAt};`;

packages/core/test/fixtures/showLineNumbersAfterHighlight.md

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/test/results/showLineNumbersAfterHighlight.html

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)