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

refactor: Improve code highlighting #188

Merged
merged 4 commits into from
Jun 7, 2020
Merged

refactor: Improve code highlighting #188

merged 4 commits into from
Jun 7, 2020

Conversation

palant
Copy link
Collaborator

@palant palant commented Jun 4, 2020

This has two goals:

  • Do not use JavaScript for code highlighting. Not only will it be problematic when JavaScript is disabled, it produces awkward visual effects when loading a page in dark mode (initial code highlighting is meant light theme).
  • Make dark and light themes consistent. Switching between themes will currently produce wildly inconsistent highlighting - not merely colors change but also bold and italic text, also which parts of the code have different colors to stand out.

My initial approach was putting highlighting styles directly into the light-theme/dark-theme mixins. This unfortunately increases the CSS file size by 10kb because both of these mixins are used twice.

So instead I added the basic styles to _highlight.css and merely define the ten color variables in the mixins. This way the CSS file size even decreases slightly.

The downside is that changing/overriding styles is no longer merely a matter of running hugo gen chromastyles. On the other hand, people can now override specific variables fairly easily.

The other downside is that not all styles are usable in this way. I used the dracula style as a template, but simply taking color values from rainbow_dash for the light mode didn't work - this style uses color values that are very close to each other, relying heavily on bold text as distinctive factor. I decided to use manni style instead which is similar but has sufficiently distinctive colors.

Here is what light/dark mode highlighting used to look like in comparison:

Light mode code snippet 1
Dark mode code snippet 1
Light mode code snippet 2
Dark mode code snippet 2
Light mode code snippet 3
Dark mode code snippet 3

Here is what it looks like with this change (only light mode changed):

Light mode code snippet 1
Dark mode code snippet 1
Light mode code snippet 2
Dark mode code snippet 2
Light mode code snippet 3
Dark mode code snippet 3

@palant
Copy link
Collaborator Author

palant commented Jun 5, 2020

I added one more commit here which isn't really related, merely to avoid a merge conflict later. I noticed a bug introduced by #169 - the brightness reduction in dark mode will only work if the user explicitly selected the them (data-theme attribute present), not when it was chosen implicitly. So now that the last rule not related to the theme switcher was moved away from assets/scss/components/_dark-mode.scss, maybe rename it into _theme-switcher.scss?

@reuixiy
Copy link
Owner

reuixiy commented Jun 7, 2020

Nice approach!

Maybe rename it into _theme-switcher.scss?

Agreed. And we can delete the related code in partials/utils/content.html now.

Also, I think we should add a "BREAKING CHANGE" note for this, since the way to customize the style of code highlighting has changed.

…used code

BREAKING CHANGE: Highlighting customization now works by overriding CSS variables, no longer by overriding theme-specific files
@palant
Copy link
Collaborator Author

palant commented Jun 7, 2020

All done.

@reuixiy
Copy link
Owner

reuixiy commented Jun 7, 2020

Yep, except that we didn't update the annoying resources folder with example config.toml for themes.gohugo.io.

@palant
Copy link
Collaborator Author

palant commented Jun 7, 2020

Done.

@reuixiy
Copy link
Owner

reuixiy commented Jun 7, 2020

Thanks!

@palant palant merged commit cf603f3 into master Jun 7, 2020
@palant palant deleted the better-highlight branch June 7, 2020 11:19
ulmefors pushed a commit to ulmefors/hugo-theme-meme that referenced this pull request Nov 22, 2020
* refactor: Do not rely on JavaScript for code highlighting, make dark and light themes consistent

* fix: Image brightness reduction in dark mode should work with implicitly selected theme (regression from reuixiy#169)

* chore: Rename dark-theme style file into them-switcher and removed unused code

* chore: Update resources folder

BREAKING CHANGE: Highlighting customization now works by overriding CSS variables, no longer by overriding theme-specific files
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.

2 participants