scrivo / highlight.php

A port of highlight.js by Ivan Sagalaev to PHP
BSD 3-Clause "New" or "Revised" License
695 stars 45 forks source link

is line numbers supported? #19

Closed qwqoffice closed 6 years ago

allejo commented 6 years ago

Nope.

This project is a direct and loyal port of the JS project. Since the JS project doesn’t support line numbers by choice, this project doesn’t support them as well.

Line numbers would have to be accomplished by manually with your own code or a separate project.

qwqoffice commented 6 years ago

OK, I will try it

paxperscientiam commented 6 years ago

@qwqoffice I managed to add line numbers in the following way.

Wrap each line of code in <span class='enumerate'></span> and then apply the following style.

pre.hljs:before {
  counter-reset: listing;
}

pre.hljs span.enumerate {
  counter-increment: listing;
}

pre.hljs span.enumerate::before {
  content: counter(listing) "  ";
  display: inline-block;
}

Good luck.

allejo commented 6 years ago

I had to handle line numbers in another project, this is how I solved with it just CSS: https://codepen.io/allejo/pen/eKzydQ

scrivo commented 6 years ago

Hi All,

That's the way to do it. It is very easy to wrap the lines returned by highlight.php in markup that renders the line numbers. Adding the line numbers with CSS gives the benefit that you still can copy/paste the code without getting the line numbers as well.

If you use an

    to start the code and wrap the lines in an
  1. you even can start first line number at an offset if you like and adding a named anchor to each line also gives you the possibility to jump to a line:

    1. <?php
    2. // Initialize your autoloader (this example is using composer)
    3. ....etc Some might argue that using an
        is semantically incorrect, I personally think it's perfect (code fragment = an ordered list of lines). But one of the reasons to leave line numbers out in highlight.php is to omit discussions like these. Perhaps the possibility to add an line-wrap-callback-function is an idea. This way we can facilitate line numbers in a correct and flexible manner. Cheers, Geert On Thu, Jun 21, 2018 at 6:12 AM, Vladimir Jimenez wrote: > I had to handle line numbers in another project, this is how I solved with > it just CSS: https://codepen.io/allejo/pen/eKzydQ > > — > You are receiving this because you are subscribed to this thread. > Reply to this email directly, view it on GitHub > , > or mute the thread > > . >
allejo commented 6 years ago

Since #28 asked for this same feature, I've added demo/line-numbers.php showing why this functionality isn't necessary as part of the core and how you can achieve it yourself.