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

code blocks lose background color when no-highlight is set #530

Closed
thinlines opened this issue Jul 8, 2018 · 7 comments
Closed

code blocks lose background color when no-highlight is set #530

thinlines opened this issue Jul 8, 2018 · 7 comments

Comments

@thinlines
Copy link

This may actually be the intended result, but it doesn't make sense to me, at least. I want to keep the element styling of code blocks without any of the syntax highlighting.

I tried explicitly adding the background color to the elements in the inline CSS included with the reference implementation from the README, but that didn't work.

A markdown example:

```no-highlight
Some stuff I want to __write____ on a whiteboard
                      throw
                      catch
```

Expected result: no keywords from any programming language are highlighted, but background color is kept.

Actual result: background color is removed along with all syntax highlighting.

Any suggestions?

@utdrmac
Copy link

utdrmac commented Jul 8, 2018

Ditto. I like having my code blocks with a "background box". I just have to force it by using ```sql everywhere, even though I don't want the highlighting. Sometimes I just have to try different highlight languages until I find one that "works". Crappy workaround indeed.

@thinlines
Copy link
Author

thinlines commented Jul 9, 2018

I wrote something resembling a solution using javascript:

var elements = document.getElementsByClassName('no-highlight');
for(var i=0; i < elements.length; i++) {
    elements[i].className += " hljs-github hljs"; // note the space
}

I understand that getElementsByClassName has some browser limitations, but is overall widely supported, and I reckon that if that won't work, then remark.js won't work either. At any rate, it just adds hljs-github and hljs to any element with class no-highlight, which adds the styling back. It's still manual, so if I want to change the theme, then I'll also have to change this part of the template, too, but it works for now.

Maybe someone can do something similar in remark's code so that the styling doesn't disappear by default?

@scarolan
Copy link

scarolan commented Aug 6, 2018

I came here for this issue. I'm building a slide deck that has both commands, and shell output. I want to show the shell output so it also looks like it's in a terminal. Losing the background color is 👎

@mbrgm
Copy link

mbrgm commented Aug 7, 2018

As a workaround, I added this to the javascript in my html file:

var slideshow = remark.create({
  // ...
});

function repairBackgroundForNoHighlightCodeblocks() {
  var elements = document.getElementsByClassName('remark-code');

  for(var i=0; i < elements.length; i++) {
    const hljsClass = "hljs";
    let el = elements[i];

    if (!el.classList.contains(hljsClass)) {
      el.classList.add(hljsClass);
    }
  }
}

slideshow.on('showSlide', function (slide) {
  repairBackgroundForNoHighlightCodeblocks();
});

@utdrmac
Copy link

utdrmac commented Dec 11, 2018

I just did this and seems to accomplish the goal without needing extra JS

.remark-code.hljs[```
 <content>
```]

@beruic
Copy link

beruic commented Nov 12, 2019

The solution from @mbrgm works. I would however like to add that the function repairBackgroundForNoHighlightCodeblocks() should be called in the end to make it work for refreshes too.

@abelards
Copy link
Collaborator

I think @utdrmac solution is the best one, it requires only a CSS directive, no JS, and no change in remark's core.
I have added this to the wiki: https://github.com/gnab/remark/wiki/HTML-CSS-JS-samples
Thanks to all!

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

No branches or pull requests

6 participants