Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line numbers: Inheritance for the line-numbers class #1799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions plugins/line-numbers/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ <h2>Line Numbers</h2>
<h1>How to use</h1>

<p>Obviously, this is supposed to work only for code blocks (<code>&lt;pre>&lt;code></code>) and not for inline code.</p>
<p>Add the <strong>line-numbers</strong> class to your desired <code>&lt;pre></code>, and the line-numbers plugin will take care of the rest.</p>
<p>Add the <code>line-numbers</code> class to your desired <code>&lt;pre></code> or any of its ancestors, and the line-numbers plugin will take care of the rest. To given all code blocks line numbers, add the <code>line-numbers</code> class to the <code>&lt;body></code> of the page.</p>
<p>Optional: You can specify the <code>data-start</code> (Number) attribute on the <code>&lt;pre></code> element. It will shift the line counter.</p>
<p>Optional: To support multiline line numbers using soft wrap, apply the CSS <code>white-space: pre-line;</code> or <code>white-space: pre-wrap;</code> to your desired <code>&lt;pre></code>.</p>
</section>

<section>
<section class="line-numbers">
<h1>Examples</h1>

<h2>JavaScript</h2>
<pre class="line-numbers" data-src="plugins/line-numbers/prism-line-numbers.js"></pre>

<h2>CSS</h2>
<pre class="line-numbers" data-src="plugins/line-numbers/prism-line-numbers.css"></pre>
<p>Please note that this <code class="language-markup">&lt;pre></code> does not have the <code>line-numbers</code> class but its parent does.</p>
<pre data-src="plugins/line-numbers/prism-line-numbers.css"></pre>

<h2>HTML</h2>
<p>Please note the <code>data-start="-5"</code> in the code below.</p>
Expand Down
41 changes: 25 additions & 16 deletions plugins/line-numbers/prism-line-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,46 @@
return;
}

var code = env.element;
var pre = code.parentNode;

// works only for <code> wrapped inside <pre> (not inline)
var pre = env.element.parentNode;
var clsReg = /(?:^|\s)line-numbers(?:\s|$)/;
if (
!pre || !/pre/i.test(pre.nodeName) ||
// Abort only if nor the <pre> nor the <code> have the class
(!clsReg.test(pre.className) && !clsReg.test(env.element.className))
) {
if (!pre || !/pre/i.test(pre.nodeName)) {
return;
}

if (env.element.querySelector('.line-numbers-rows')) {
// Abort if line numbers already exists
// Abort if line numbers already exists
if (code.querySelector('.line-numbers-rows')) {
return;
}

if (clsReg.test(env.element.className)) {
// Remove the class 'line-numbers' from the <code>
env.element.className = env.element.className.replace(clsReg, ' ');
var addLineNumbers = false;
var lineNumbersRegex = /(?:^|\s)line-numbers(?:\s|$)/;

for (var element = code; element; element = element.parentNode) {
if (lineNumbersRegex.test(element.className)) {
addLineNumbers = true;
break;
}
}
if (!clsReg.test(pre.className)) {
// Add the class 'line-numbers' to the <pre>

// only add line numbers if <code> or one of its ancestors has the `line-numbers` class
if (!addLineNumbers) {
return;
}

// Remove the class 'line-numbers' from the <code>
code.className = code.className.replace(lineNumbersRegex, ' ');
// Add the class 'line-numbers' to the <pre>
if (!lineNumbersRegex.test(pre.className)) {
pre.className += ' line-numbers';
}

var match = env.code.match(NEW_LINE_EXP);
var linesNum = match ? match.length + 1 : 1;
var lineNumbersWrapper;

var lines = new Array(linesNum + 1);
lines = lines.join('<span></span>');
var lines = new Array(linesNum + 1).join('<span></span>');

lineNumbersWrapper = document.createElement('span');
lineNumbersWrapper.setAttribute('aria-hidden', 'true');
Expand Down
2 changes: 1 addition & 1 deletion plugins/line-numbers/prism-line-numbers.min.js

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