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

Fix line highlight offset when using line numbers #2235

Closed
wants to merge 1 commit into from
Closed

Fix line highlight offset when using line numbers #2235

wants to merge 1 commit into from

Conversation

siavashs
Copy link

The line-highlight plugin ignored the data-line-offset value when the line-numbers plugin is enabled.
This change fixes the above issue.

The `line-highlight` plugin ignored the `data-line-offset` value when
the `line-numbers` plugin is enabled.
This change fixes the above issue.
@RunDevelopment
Copy link
Member

Could you please explain the issue you're trying to fix and how your changes fix it?

Also, we now combine data-line-offset and Line number's data-start, so I don't see how this is going to work.

I modified the plugin page of Line numbers to use Line highlight, and everything worked, so how can I reproduce your issue?

I set it up, so that line nr. 7 is highlighted.

image

Even works with soft-wrap.

image

Changes:

 <!DOCTYPE html>
 <html lang="en">
 <head>
 
 <meta charset="utf-8" />
 <link rel="icon" href="favicon.png" />
 <title>Line Numbers ▲ Prism plugins</title>
 <base href="../.." />
 <link rel="stylesheet" href="style.css" />
 <link rel="stylesheet" href="themes/prism.css" data-noprefix />
 <link rel="stylesheet" href="plugins/line-numbers/prism-line-numbers.css" data-noprefix />
+<link rel="stylesheet" href="plugins/line-highlight/prism-line-highlight.css" data-noprefix />
 <script src="scripts/prefixfree.min.js"></script>
 
 <script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
 <script src="https://www.google-analytics.com/ga.js" async></script>
 </head>
 <body>
 
 <header data-plugin-header="line-numbers"></header>
 
 <section class="language-markup">
   <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 <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 give 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 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>
   <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>
+  <pre class="line-numbers" data-src="plugins/line-numbers/index.html" data-line="7" data-start="-5"></pre>
-  <pre class="line-numbers" data-src="plugins/line-numbers/index.html" data-start="-5"></pre>
 
   <h2>Unknown languages</h2>
   <pre class="language-none line-numbers"><code>This raw text
 is not highlighted
 but it still has
 line numbers</code></pre>
 
   <h2>Soft wrap support</h2>
   <p>Please note the <code>style="white-space:pre-wrap;"</code> in the code below.</p>
+  <pre class="line-numbers" data-src="plugins/line-numbers/index.html" data-line="7" data-start="-5" style="white-space:pre-wrap;"></pre>
-  <pre class="line-numbers" data-src="plugins/line-numbers/index.html" data-line="7" data-start="-5" style="white-space:pre-wrap;"></pre>
 
 </section>
 
 <footer data-src="templates/footer.html" data-type="text/html"></footer>
 
 <script src="prism.js"></script>
+<script src="plugins/line-highlight/prism-line-highlight.js"></script>
 <script src="plugins/line-numbers/prism-line-numbers.js"></script>
 <script src="scripts/utopia.js"></script>
 <script src="components.js"></script>
 <script src="scripts/code.js"></script>
 
 
 </body>
 </html>

@siavashs
Copy link
Author

siavashs commented Feb 29, 2020

We add a <div> and <hr> to the <pre> as a custom title, and as you can see it pushes the code down but the highlight stays in it's position, See siavashs/Prism#27

@RunDevelopment
Copy link
Member

Can reproduce.

The basic problem here is that the line numbers itself are inside the <code> element and thus they are aligned relative to it. But the line highlight is aligned relative to the <pre>.
In this line (and this one), we assume basically assume that there is nothing in between the <code> and <pre> element, so that's the cause of your issue.

The fix for this problem isn't quite as simple as it might seem but still doable. I'll make a pull request.

@siavashs
Copy link
Author

@RunDevelopment please mention me or the issue I shared here on your PR, thanks.

@siavashs siavashs deleted the fix-line-highlist-offset branch February 29, 2020 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants