Skip to content

Commit 9a4e725

Browse files
authored
Line Highlight: Ignore ranges outside of actual lines (#3475)
1 parent 6464271 commit 9a4e725

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

plugins/line-highlight/prism-line-highlight.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

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

1011
/**
1112
* @param {string} selector
@@ -136,7 +137,8 @@
136137
var codeElement = pre.querySelector('code');
137138
var parentElement = hasLineNumbers ? pre : codeElement || pre;
138139
var mutateActions = /** @type {(() => void)[]} */ ([]);
139-
140+
var lineBreakMatch = codeElement.textContent.match(NEW_LINE_EXP);
141+
var numberOfLines = lineBreakMatch ? lineBreakMatch.length + 1 : 1;
140142
/**
141143
* The top offset between the content box of the <code> element and the content box of the parent element of
142144
* the line highlight element (either `<pre>` or `<code>`).
@@ -154,6 +156,11 @@
154156

155157
var start = +range[0];
156158
var end = +range[1] || start;
159+
end = Math.min(numberOfLines, end);
160+
161+
if (end < start) {
162+
return;
163+
}
157164

158165
/** @type {HTMLElement} */
159166
var line = pre.querySelector('.line-highlight[data-range="' + currentRange + '"]') || document.createElement('div');

plugins/line-highlight/prism-line-highlight.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)