fix(plugin-shiki): highlight lines not working with code block options (close #577) #578
+90
−39
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before submitting the PR, please make sure you do the following
close #123).What is the purpose of this pull request?
Description
Highlighted lines (
{1,2}) fail when combined with other code block options like:no-collapsed-linesor:collapsed-lines=5. The regex inattrsToLinesincorrectly captures hyphens from option names instead of line numbers. This bug occurs regardless of the order in which the syntax elements appear.Examples that fail before this fix:
Root cause:
The bug occurs due to how the code processes markdown code fence syntax through two stages:
Preprocessing by
highlightLinePlugin(inhighlightLinesPlugin.ts):java {1} :no-collapsed-lines{1}pattern →"java :no-collapsed-lines"(note: double space where{1}was){1}→"1""java :no-collapsed-lines 1"attrsToLinesRegex matching in
attrsToLines(inutils.ts):/^(?:\[.*?\])?.*?([\d,-]+).*/.*?matches as few characters as possible before finding[\d,-]+"java :no-collapsed-lines 1":.*?matches"java :no"(minimal match, stops as soon as possible)[\d,-]+then finds the first matching character: the-in:no-collapsed-linesat position 9'c'doesn't match[\d,-], it captures just"-"and stops"1"at the end because it already found a match earlierThis happens in both orderings:
java :no-collapsed-lines {1}→ preprocessed to"java :no-collapsed-lines 1"→ regex captures-java {1} :no-collapsed-lines→ preprocessed to"java :no-collapsed-lines 1"→ regex captures-Why the non-greedy
.*?causes the problem:The non-greedy quantifier makes the regex stop at the FIRST character matching
[\d,-]+, not look for the best or last match. Since-appears before1in the string (within:no-collapsed-lines), it gets captured instead of the intended line number1at the end.Fix:
Changed regex to
/\s+([\d,-]+)\s*$/— anchors to the end of string and matches line numbers after the final space, correctly extracting them regardless of option ordering.Changes:
attrsToLines()regex insrc/node/utils.tsScreenshots
N/A - No UI changes