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

WIP: Add auto light/dark syntax highlight theme switch with chroma #143

Closed

Conversation

ataias
Copy link

@ataias ataias commented Dec 19, 2020

Hi,

I use syntax highlighting in my personal website and I wanted to discuss about having light/dark syntax highlight switching. Is this something other people also want or care about?

From what I looked, hugo-PaperMod does not currently offer that and the css variable --hljs-bg: #2e2e33; that defines the background is hard-coded the same for light and dark. I am not sure why the background is set in the hugo paperMod sheet anyway... maybe a highlightjs limitation? I didn't look into it.

I was already using chroma, which is the built-in syntax highlighter from hugo and I find it fantastic as I don't need to care about which languages are supported in the highlightjs bundle or its size. Chroma runs when hugo builds the site and it works by either

  1. embedding the styles in the code block or,
  2. adding classes to it.

I was using the first approach, but I couldn't figure out a way of making it work if I wanted to switch light and dark. Maybe I could add some JS that would later toggle the things? It seemed to be a pain.

I tried approach 2 then, which is this PR. It has the following:

  • Two CSS sheets for light and dark mode:
    • light: hugo gen chromastyles --style=emacs
    • dark: hugo gen chromastyles --style=dracula and then search/replace .chroma to .dark .chroma
  • Workaround: remove highlights (had some conflicts, maybe I could have just removed the background css var or something? IDK)

I tested this against my website locally, making sure config had noClasses = false set so that hugo chroma uses the classes approach.

[markup]
  [markup.highlight]
    noClasses = false

The result can be seen in the quick video below where I toggle the style.

Screen.Recording.2020-12-19.at.04.40.26.mov

Questions

  • What are your opinions on this? Is it promising?
  • What would be the next steps to get this ready for a merge? IMO this would be the following:
    • Create properties "light" and "dark" with reasonable defaults that can be overriden by user to select the chroma classes
    • Generate the css automatically based on such values (maybe hugo gen chromastyles with sed for necessary changes and then saving the css, or some hugo templating thing)
    • decide on what to do with highlightjs: either make it togglable instead of a requirement like now, or remove it

Highlightjs javascript and css in this theme conflicts with hugo's
chroma setup.
@sonarcloud
Copy link

sonarcloud bot commented Dec 19, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 37 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@ataias ataias changed the title WIP: Change highlightjs to chroma and add auto light/dark syntax highlight switch WIP: Add auto light/dark syntax highlight theme switch with chroma Dec 19, 2020
@michaeloliverx
Copy link

+1 I am interested in this feature!

@adityatelange
Copy link
Owner

@ataias Sorry for delay, we'll look into this after few weeks as I am busy, so new features for theme are on hold

@ataias
Copy link
Author

ataias commented Dec 27, 2020

@adityatelange No problem! I have been busy too, so I may take a couple weeks to do some updates and remove the WIP.

adityatelange added a commit that referenced this pull request Dec 29, 2020
usage =>

in site config =>
    params:
        assets:
            disableHLJS: true

per-page in front-matter =>
   disableHLJS: true
@stale
Copy link

stale bot commented Jan 3, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the no-activity No activity since long time label Jan 3, 2021
@stale stale bot closed this Jan 6, 2021
Repository owner deleted a comment from reorx Apr 14, 2022
kylethedeveloper pushed a commit to kylethedeveloper/hugo-PaperMod that referenced this pull request Feb 21, 2023
usage =>

in site config =>
    params:
        assets:
            disableHLJS: true

per-page in front-matter =>
   disableHLJS: true
@adityatelange adityatelange removed the no-activity No activity since long time label Apr 20, 2024
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.

None yet

3 participants