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

Request: Support for line numbers #28

Closed w3suli closed 6 years ago

w3suli commented 6 years ago

This project is great but there is missing one option.

Code lines can not be numbered.

There is a javascript plugin for this: https://github.com/wcoder/highlightjs-line-numbers.js/

In the present case, it would also be an option to return an array with rows. This could later be shaped by everyone's own needs to have tables, listings, or div elements for numbering.

allejo commented 6 years ago

Lack of line numbering support is intended, see https://github.com/scrivo/highlight.php/issues/19.

Same way the linked repository is a separate project, this would have to be a separate project from highlight.php as well.

You could split up the highlighted code on each new line and wrap each line with whatever elements you'd like to use. The concept would be like this:

$highlightedCode = '...';
$lines = preg_split('/\R/', $highlightedCode);
$output = '<span class="code-line">' . implode("</span>\n<span class=\"code-line\">", $lines) . '</span>';

Edit: Code snippet now actually works

allejo commented 6 years ago

Since this is not the first time this feature has been asked for, I've added a demo showing why adding this to the core is not necessary: demo/line-numbers.php

Styling with line numbers can be so opinionated so it's left up to the user to style it however they'd like. highlight.php's goal remains the same: highlighting code 😄