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

Add support for highlighting specific lines #20

Merged
merged 27 commits into from
Apr 22, 2020
Merged

Add support for highlighting specific lines #20

merged 27 commits into from
Apr 22, 2020

Conversation

allejo
Copy link
Collaborator

@allejo allejo commented Nov 23, 2019

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

@westonruter
Copy link
Owner

FYI: I'm not going to have a chance to look at this until mid-December.

@allejo
Copy link
Collaborator Author

allejo commented Nov 28, 2019

No worries! I'll leave any major further work on this PR until then

@allejo
Copy link
Collaborator Author

allejo commented Dec 31, 2019

Gonna gently nudge you on this one, @westonruter

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
@@ -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'] ) ) {
Copy link
Owner

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?

Copy link
Collaborator Author

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
Comment on lines 18 to 20
.code-block-bg .loc.highlighted {
background: rgba(86, 213, 255, 0.2);
}
Copy link
Owner

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?

Copy link
Collaborator Author

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.

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
block-styles.css Outdated Show resolved Hide resolved
@allejo
Copy link
Collaborator Author

allejo commented Feb 17, 2020

Just as an update, I'll be getting back to this in about a week or so

@allejo
Copy link
Collaborator Author

allejo commented Feb 23, 2020

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?

@westonruter
Copy link
Owner

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 background-color and color, and somehow computing a highlight color? At very least, could there be one default highlight color for dark scheme and another for a light scheme? Perhaps a light yellow for a light style and a dark yellow for a dark style? A Customizer setting to override this could also be provided, but there should be some good default.

@allejo
Copy link
Collaborator Author

allejo commented Feb 27, 2020

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 background-color and color, and somehow computing a highlight color?

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?

@westonruter
Copy link
Owner

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.

@westonruter
Copy link
Owner

westonruter commented Feb 29, 2020

I was curious actually how browsers handle highlighting with the mark element.

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:

image

And in a dark color scheme, it looks like:

image

Nevertheless, would this work for adding highlights to syntax-highlighted text which has variable colors set for the text? I suspect not.

@allejo
Copy link
Collaborator Author

allejo commented Mar 1, 2020

I think the default styles for <mark> works for <mark> because all of the text is a solid color. I would agree with you that I don't think this would work for themes where some keywords could be yellow-ish. I think we could have the following behavior:

  • For a light theme, we can be a nice pastel color: yellow or blue
  • For a dark theme, take the background color and lighten it up; similar to how an IDE dark mode looks for a selected line

What are your thoughts?

@westonruter
Copy link
Owner

Yes, that sounds good to me.

@allejo
Copy link
Collaborator Author

allejo commented Mar 3, 2020

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?

@westonruter
Copy link
Owner

I just tried testing this but I got an error:

PHP Notice:  Undefined index: theme_name in /app/public/content/plugins/syntax-highlighting-code-block/syntax-highlighting-code-block.php on line 132
PHP Fatal error:  Uncaught DomainException: There is no stylesheet by the name of '' in /app/public/content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight.php/HighlightUtilities/_themeColors.php:475
Stack trace:
#0 /app/public/content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight.php/HighlightUtilities/functions.php(91): _getThemeBackgroundColor(NULL)
#1 /app/public/content/plugins/syntax-highlighting-code-block/syntax-highlighting-code-block.php(110): HighlightUtilities\getThemeBackgroundColor(NULL)
#2 /app/public/content/plugins/syntax-highlighting-code-block/syntax-highlighting-code-block.php(132): Syntax_Highlighting_Code_Block\get_default_line_bg_color(NULL)
#3 /app/public/content/plugins/syntax-highlighting-code-block/syntax-highlighting-code-block.php(146): Syntax_Highlighting_Code_Block\get_options()
#4 /app/public/content/plugins/syntax-highlighting-code-block/syntax-highlighting-code-block.php(268): Syntax_Highlighting_Code_Block\get_option('theme_name')
#5 /app/public/core-dev/src/wp in /app/public/content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight.php/HighlightUtilities/_themeColors.php on line 475

@allejo
Copy link
Collaborator Author

allejo commented Mar 4, 2020

Welp, sorry about that. I hadn't tested my changes without any plugin options in the database. Time for round 2!

@westonruter
Copy link
Owner

I'm noticing that the highlight lines aren't appearing to line up properly in the editor, at least in the Twenty Twenty theme:

image

src/index.js Outdated Show resolved Hide resolved
@allejo
Copy link
Collaborator Author

allejo commented Apr 19, 2020

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.

image
image

@westonruter
Copy link
Owner

westonruter commented Apr 20, 2020

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.)

image

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):

image

@allejo
Copy link
Collaborator Author

allejo commented Apr 20, 2020

I believe I've largely fixed this in 4d381e9.

Thanks for the catch!


For the line-height issue, can you confirm that your code DOM node has a class of selected-lines and that class has display: table? I made a change to line-numbers.css in 7b02b94 that made code blocks with either line numbers or selected lines to be a display: table.

When I make that code block display: block (the default style), this is what I get. It looks like your screenshots, but I'm not sure if it's the same cause.

image

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.

@westonruter
Copy link
Owner

The class is present on code:

image

However, the display is block not table:

image

That seems to reveal the issue, namely that this rule is contained in line-numbers.css which is not loaded when selected lines are only supplied:

.hljs.line-numbers,
.hljs.selected-lines {
	display: table;
	width: 100%;
}

So do we need a separate selected-lines.css file that we add independently?

@allejo
Copy link
Collaborator Author

allejo commented Apr 21, 2020

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 🤦

So do we need a separate selected-lines.css file that we add independently?

For the very little CSS this plugin is injecting from line-numbers.css, I don't think it's worth having a separate file (585 bytes currently vs 121 bytes with a separate file). I would lean more towards just modifying the logic to include that CSS file if either line numbers are used or lines are highlighted.

@westonruter
Copy link
Owner

That sounds good to me.

Copy link
Owner

@westonruter westonruter left a 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.

@allejo
Copy link
Collaborator Author

allejo commented Apr 21, 2020

Your changes worked fine for me! 👍

Here are a few minor changes that I had overlooked.

The gap between line numbers and code with highlights is gone now

image

Empty new lines can now be selected. Previously, there were not being rendered.

image

@westonruter
Copy link
Owner

Empty new lines can now be selected. Previously, there were not being rendered.

I'm not seeing this issue… or any difference between 0b57cc5 and 5ccac85.

I'm trying with this code, having selected lines 3,5:

function foo() {
   var first = 1;

   var second = 2;
      
   var third = 3;
}

Editor:

image

Frontend:

image

@westonruter
Copy link
Owner

Oh, I'm also noticing a discrepancy in the highlighted lines between the editor and the frontend (using selected lines 2-4,5):

image

@allejo
Copy link
Collaborator Author

allejo commented Apr 21, 2020

Oh, I'm also noticing a discrepancy in the highlighted lines between the editor and the frontend (using selected lines 2-4,5):

image

Fixed in 51076ef

@allejo
Copy link
Collaborator Author

allejo commented Apr 21, 2020

Empty new lines can now be selected. Previously, there were not being rendered.

I'm not seeing this issue… or any difference between 0b57cc5 and 5ccac85.

I'm trying with this code, having selected lines 3,5:

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.

@westonruter
Copy link
Owner

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.

@allejo
Copy link
Collaborator Author

allejo commented Apr 22, 2020

Sure! Here's my code snippet

<?php

echo "hello world";

Screenshots of running 0b57cc5:

Gutenberg

image

Line number options

image

Rendering
image

@westonruter
Copy link
Owner

Thank you. I was able to reproduce the problem and the fix 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to highlight one or more lines
2 participants