Skip to content

fix: use lookbehind in highlightSpans to fix adjacent spans and start-of-line#18

Merged
dpecos merged 1 commit into
masterfrom
fix/issue-3-highlight-spans-trailing-paren
Apr 16, 2026
Merged

fix: use lookbehind in highlightSpans to fix adjacent spans and start-of-line#18
dpecos merged 1 commit into
masterfrom
fix/issue-3-highlight-spans-trailing-paren

Conversation

@dpecos
Copy link
Copy Markdown
Contributor

@dpecos dpecos commented Apr 16, 2026

Summary

Root cause (both bugs share the same origin)

The old pattern ([^\])`([^\`]+?)` consumed one character before the opening backtick as group 1 (e`). This caused:

Issue #2 – no preceding character at start of line → no match at all.

Issue #3 – with adjacent spans like `foo`(`bar`):

  • The engine couldn't match the first span's opening backtick (nothing before it, or backtick before it)
  • It found o (last char of foo) as e, then treated foo's closing backtick as the opening of a new span
  • Content [^\]+?then matched(`, which was "highlighted" — the bug

Fix

// before — consumes preceding char, breaks at start-of-line and adjacent spans
pattern = /([^`])`([^`]+?)`/g;

// after — lookbehind: opening backtick must not be preceded by ` or \
pattern = /(?<![`\\])`([^`]+?)`/g;

The lookbehind also naturally handles the escape case: \` is no longer treated as an opening delimiter, so the separate if (e === '\\') return m.slice(1) branch is no longer needed.

Closes #2
Closes #3

…-of-line

The old pattern ([^`])`([^`]+?)` required consuming a preceding
character as group 1, which caused two bugs:

- Issue #2: spans at the start of a line were never matched because there
  was no preceding character to consume.
- Issue #3: adjacent spans like `foo`(`bar`) were broken because the
  pattern consumed the last char of the first span ('o' from 'foo') and
  then treated that span's closing backtick as the opening of a new span,
  capturing '(' as the highlighted content.

Replace the consuming group with a negative lookbehind (?<!['`'\\]) so
the opening backtick is matched without consuming any preceding character.
This fixes both bugs and also subsumes escape handling: a backtick
preceded by \ is no longer treated as an opening delimiter.

Closes #2
Closes #3
@dpecos dpecos force-pushed the fix/issue-3-highlight-spans-trailing-paren branch from c7b6995 to d5088bc Compare April 16, 2026 11:46
@dpecos dpecos merged commit 8f5aa07 into master Apr 16, 2026
4 checks passed
@dpecos dpecos deleted the fix/issue-3-highlight-spans-trailing-paren branch April 16, 2026 12:39
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.

Bug: highlightSpans incorrectly captures trailing parenthesis Bug: highlightSpans fails when backtick is the first character on a line

1 participant