-
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 support for highlighting specific lines #20
Add support for highlighting specific lines #20
Conversation
FYI: I'm not going to have a chance to look at this until mid-December. |
No worries! I'll leave any major further work on this PR until then |
Gonna gently nudge you on this one, @westonruter |
syntax-highlighting-code-block.php
Outdated
@@ -238,7 +238,7 @@ function render_block( $attributes, $content ) { | |||
$transient_key = 'syntax-highlighting-code-block-' . md5( $attributes['showLines'] . $attributes['language'] . $matches['code'] ) . '-v' . PLUGIN_VERSION; | |||
$highlighted = get_transient( $transient_key ); | |||
|
|||
if ( $highlighted && isset( $highlighted['code'] ) ) { | |||
if ( ! DEVELOPMENT_MODE && $highlighted && isset( $highlighted['code'] ) ) { |
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.
Why the ! DEVELOPMENT_MODE
check?
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 might have been doing something wrong but I believe I kept getting cached results while I was developing on the plugin. So I figured I'd disable transients in dev mode. Is there a better way of handling this?
block-styles.css
Outdated
.code-block-bg .loc.highlighted { | ||
background: rgba(86, 213, 255, 0.2); | ||
} |
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.
This would also need to get added to the frontend inline styles, correct?
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'm thinking of having separate styles for the frontend vs the editor. Reason being is the frontend handles highlighting lines differently from what the editor is doing. Where in the editor we need to "style" a textarea
, the frontend has a lot of dedicated divs we can style.
Just as an update, I'll be getting back to this in about a week or so |
How should we handle the color for highlighting lines? On the frontend, there can be light or dark themes, we can't just select an arbitrary color since it might conflict with the background of the selected theme. Should the color be an option in Customizer? |
Could a default highlight color be selected for each color scheme as part of highlight.php? Could this be automated perhaps by iterating over all of the styles, obtaining the |
This is definitely doable but I wouldn't want to modify the distributed CSS files directly because I'm not sure what would be the best way to introduce those classes since line numbers aren't officially part of the highlight.js scope. Do you have any suggestions? |
Oh yeah, I didn't mean necessarily that the existing CSS files should be modified. What about adding additional CSS files to contain the rules for highlighting? Either that, or a JSON file that contains what background color should be used for each? In either case, the WordPress plugin here could either add the CSS file as an inline style or it could read the JSON file to output the necessary CSS. |
I was curious actually how browsers handle highlighting with the It turns out, the Chrome's default stylesheet has this: mark {
background-color: yellow;
color: black;
} So in a light color scheme, it gets rendered as: And in a dark color scheme, it looks like: Nevertheless, would this work for adding highlights to syntax-highlighted text which has variable colors set for the text? I suspect not. |
I think the default styles for
What are your thoughts? |
Yes, that sounds good to me. |
I've still got some things to work out on the Customizer settings, like to update colors based on theme changes. Otherwise, this is my proposal for this feature. What are your thoughts? Too complicated? Too many color calculations? |
I just tried testing this but I got an error:
|
Welp, sorry about that. I hadn't tested my changes without any plugin options in the database. Time for round 2! |
No worries about the delay! What WP + highlight.js theme are you using to test? I can't replicate the lack of line highlighting or the increased line-height with the "Twenty" WP themes and highlight.js' default theme. I did, however, catch this bug in the Twenty Seventeen theme where blank lines don't seem to be respected when the plug-in is enabled. I'll take a look at the CSS for that. |
I'm testing specifically in Twenty Twenty. I think I see the problem. The highlights showed up as soon as I checked “Show Line Numbers”. (The line spacing issue also went away.) As soon as I re-unchecked the highlights went away. I believe I've largely fixed this in 4d381e9. However, the line height issue is still present on the frontend in Twenty Twenty (when the line numbers are turned off): |
Thanks for the catch! For the line-height issue, can you confirm that your When I make that code block The styles are being injected inline, so I don't think it'd be a caching problem. I'll set up a clean install of WP and the plugin tomorrow and try replicating it. Edit: The length of the highlighted lines are different in my screenshot than yours, so I don't think this is the same problem. |
The class is present on However, the That seems to reveal the issue, namely that this rule is contained in .hljs.line-numbers,
.hljs.selected-lines {
display: table;
width: 100%;
} So do we need a separate |
OH! I see why I couldn't replicate it! My test page has blocks of all types, including blocks with line numbers, which is why that CSS was being loaded 🤦
For the very little CSS this plugin is injecting from |
That sounds good to me. |
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.
This is all working now!
If my latest changes all work for you too, I'll merge.
Fixed in 51076ef |
Ah sorry, instead of JavaScript, try doing some PHP. It's whenever a code block has a sublanguage (PHP is a sub lang of XML according to highlight.js) that newlines aren't rendered correctly. |
Humm. Could you share the raw post content for that Code block so I can see what you're seeing? I tried selecting PHP but I didn't see anything amiss about selecting newlines. |
Sure! Here's my code snippet <?php
echo "hello world"; Screenshots of running 0b57cc5: Gutenberg Line number options |
Thank you. I was able to reproduce the problem and the fix 👍 |
Here's my proof of concept for highlighting specific lines. Do you have any thoughts on whether or not to proceed with something like this?
Closes #12