Skip to content

Commit

Permalink
Line Highlight: Ignore ranges outside of actual lines (#3475)
Browse files Browse the repository at this point in the history
  • Loading branch information
lijyze authored Jun 16, 2022
1 parent 6464271 commit 9a4e725
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion plugins/line-highlight/prism-line-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var LINE_NUMBERS_CLASS = 'line-numbers';
var LINKABLE_LINE_NUMBERS_CLASS = 'linkable-line-numbers';
var NEW_LINE_EXP = /\n(?!$)/g;

/**
* @param {string} selector
Expand Down Expand Up @@ -136,7 +137,8 @@
var codeElement = pre.querySelector('code');
var parentElement = hasLineNumbers ? pre : codeElement || pre;
var mutateActions = /** @type {(() => void)[]} */ ([]);

var lineBreakMatch = codeElement.textContent.match(NEW_LINE_EXP);
var numberOfLines = lineBreakMatch ? lineBreakMatch.length + 1 : 1;
/**
* The top offset between the content box of the <code> element and the content box of the parent element of
* the line highlight element (either `<pre>` or `<code>`).
Expand All @@ -154,6 +156,11 @@

var start = +range[0];
var end = +range[1] || start;
end = Math.min(numberOfLines, end);

if (end < start) {
return;
}

/** @type {HTMLElement} */
var line = pre.querySelector('.line-highlight[data-range="' + currentRange + '"]') || document.createElement('div');
Expand Down
2 changes: 1 addition & 1 deletion plugins/line-highlight/prism-line-highlight.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a4e725

Please sign in to comment.