-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add ability to show code with line numbers #14
Add ability to show code with line numbers #14
Conversation
Thank you!
I didn't have a specific solution in mind.
Yes, perhaps using GitHub's strategy? But…
I'm not a huge fan of using tables for this. Seems like a hack. I wonder if lines could be styled with numbers via
I think it's good to be per-block basis. |
👍 for not using tables for this. I tried giving Currently, I'm using a CSS counter for the line numbers, but I can get the number generation happening on the PHP side and use Thoughts? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For counter()
, what happens if you have two code blocks one after the other? Does it reset?
// We need to wrap the line of code twice in order to let out `white-space: pre` CSS setting to be respected | ||
// by our `table-row`. | ||
foreach ( $lines as $line ) { | ||
$code .= sprintf( '<div class="loc"><span>%s</span></div>%s', $line, PHP_EOL ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future it will be useful to include the loc
even if line numbers aren't being displayed. This will make it easy to highlight lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you okay with adding the extra overhead on the initial render of splitting up code into lines even if it's not showing line numbers or highlighting lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. How much overhead is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's nothing too crazy:
In splitCodeIntoArray()
from highlight.php:
- It'll parse the highlighted HTML into a
\DOMDocument
- Loop through all of the spans and see if there are any new lines
- If new lines exist, modify the lines of code to add closing and opening tags
- Split the modified code into an array.
In this plug-in:
- Loop through all of the lines of code and wrap them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder. Why is splitCodeIntoArray
even needing to load the string into a DOMDocument
to do this? Why can't it just split the string by lines given that it is inside of a pre
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing it as a DOM is necessary, take this snippet as an example:
/**
* Hello World
*
* @api
* @since 1.0.0
* @param string $str Some string parameter
*/
The way highlight.js works (and in turn, highlight.php, since it's a strict port), you would get this HTML:
<span class="hljs-comment">/**
* Hello World
*
* <span class="hljs-doctag">@api</span>
* <span class="hljs-doctag">@since</span> 1.0.0
* <span class="hljs-doctag">@param</span> string $str Some string parameter
*/</span>
If you split up by newlines, you'd break <span>
s that have new lines in them. You'd get something like:
<div class="loc"><span><span class="hljs-comment">/**</span></div>
<div class="loc"><span> * Hello World</span></div>
<div class="loc"><span> *</span></div>
<div class="loc"><span> * <span class="hljs-doctag">@api</span></span></div>
<div class="loc"><span> * <span class="hljs-doctag">@since</span> 1.0.0</span></div>
<div class="loc"><span> * <span class="hljs-doctag">@param</span> string $str Some string parameter</span></div>
<div class="loc"><span> */</span></span></div>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's a better and robust solution to splitting into lines without parsing it, I'm all ears! 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good observation. I don't have any better recommendation!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work on this!
wp_enqueue_style( FRONTEND_HIGHLIGHT_STYLE_HANDLE ); | ||
wp_add_inline_style( | ||
FRONTEND_HIGHLIGHT_STYLE_HANDLE, | ||
file_get_contents( plugins_url( 'index.css', __FILE__ ) ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good to know for future reference. Thanks for fixing it!
I haven't developed for WordPress in years, I'm hoping I'm not too far off with this PR.
How do you want to handle line numbers on the front-end?
Closes #11
Edit: Looks like the build failed. Should
.editorconfig
enforce tabs for indentation? I was hoping my IDE would pick up the correct indentation but it doesn't look like it did.