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

Possible to have code block with gray background, no syntax highlighting, and not run with rustc? #179

Closed
carols10cents opened this issue Nov 8, 2016 · 5 comments

Comments

@carols10cents
Copy link
Member

What I'm trying to do is get a code block that has the gray background that comes with:

.hljs {
   background: #f1f1f1;
}

(and the rest of the .hljs css too, probably) but not have any syntax highlighting applied, and also not have mdbook try and run the code block with rustdoc --test.

My use case is code blocks like this that are showing command line output:

$ cargo run
   Compiling panic v0.1.0 (file:///projects/panic)
    Finished debug [unoptimized + debuginfo] target(s) in 0.27 secs
     Running `target/debug/panic`
thread 'main' panicked at 'index out of bounds: the len is 3 but the index is
100', ../src/libcollections/vec.rs:1265
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: Process didn't exit successfully: `target/debug/panic` (exit code: 101)

I've tried:

  • Setting the language as bash, which uses the gray background, but since it's meant for bash scripts not bash command line interactions, it confusingly will highlight arbitrary words like "in".
  • Setting the language as text, which turns off highlighting, but that also means that block doesn't get the hljs class since it technically didn't go through highlight.js.
  • Setting the language as something nonexistent like blah, which has the same effect as text.
  • Not setting a language at all, which does get me the grey background and lack of syntax highlighting, but mdbook test tries to run the contents as rust :(
  • Setting the language to just ignore, which makes mdbook test ignore it as desired but has the same display without the grey background as text, blah, etc

Do you know of any other workarounds that would result in the behavior I'm looking for?

Alternatively, would you accept a PR that adds this (or similar) to highlight.css, that matches the words in highlight.js's regex to not do syntax highlighting?

.language-no-highlight, .language-nohighlight, .language-plain, .language-text {
  display: block;
  overflow-x: auto;
  background: #f1f1f1;
  color: #6e6b5e;
  padding: 0.5em;
-webkit-text-size-adjust: none;
}
@azerupi
Copy link
Contributor

azerupi commented Nov 8, 2016

I am not sure there is a workaround without touching mdBook's code.
There are actually two issues being reported here:

1. Code blocks that don't go through highlight.js do not have any background color

Modifying the highlight.css file directly is probably not that great. It is likely that it will get updated or replaced in the future. The best place to make this change would probably be in mdBook's theme files. There we can add a rule for codeblocks and a variable for every theme to change.

However, for consistency between hljs and non-hljs codeblocks, it would probably be better to override the background color that is set by hljs as well.

A downside of this solution is that highlight.css themes don't control the background for codeblocks anymore.

2. mdbook test tries to run snippets that don't have a language specified

This is more problematic & trickier, as the test command just invokes rustdoc on the chapters. I am not sure if there is a setting in rustdoc to be more conservative and only run the snippets explicitly tagged as Rust?

@carols10cents
Copy link
Member Author

carols10cents commented Nov 10, 2016

You're totally right that this is two separate issues, I was sort of trying to get at what I wanted from multiple directions :)

There's actually one instance of .hljs in the theme files already, does that make the solution proposed in 1) better or worse? ;)

Re: 2), I looked into rustdoc a little bit but didn't see any useful settings. I did get another workaround idea, though it doesn't work either: if I don't specify a language but then add hidden lines to start and end a block comment around the code, then rustdoc is able to compile it since it's treating unannotated code blocks as rust. mdbook only hides hidden lines in the resulting markup when the language is specifically marked as rust, and with the rust syntax highlighting of the block comment, the HTML generated by the code-hiding JS hides all the code since the <span class="hidden">s end the <span class="hljs-comment"> instead. That is, if I try to do:

# /*
$ curl https://sh.rustup.rs -sSf | sh
# */

lines ends up being:

[
  "<span class="hidden"> <span class="hljs-comment">/*
</span>",
  "$ curl https://sh.rustup.rs -sSf | sh",
  "<span class="hidden">
 */</span></span>"
]

Ahahaa hahah ha 😂😂😂😂😂 Foiled again!!!

@azerupi
Copy link
Contributor

azerupi commented Nov 11, 2016

There's actually one instance of .hljs in the theme files already, does that make the solution proposed in

Indeed, it doesn't change the color though.
To be honest, I don't think setting the color in the theme files is a horrible solution, it's probably the easiest one. I am just a little worried that down the line we will get issues reported because the hljs themes can't change the background color..

I will try to see if I can come with another idea, otherwise this fix is fine for me :)

I looked into rustdoc a little bit but didn't see any useful settings.

I guess it would be a useful setting to have in rustdoc. I may or may not explore the possibility to make a PR if I find some time 😄

@carols10cents
Copy link
Member Author

I will try to see if I can come with another idea, otherwise this fix is fine for me :)

OOH I just had one more idea: what if book.js adds the hljs class to all code blocks after running highlightBlock?

@azerupi
Copy link
Contributor

azerupi commented Nov 12, 2016

That is a good idea. It think it should work and it is probably a better solution than hard-coding the color in the themes. :)

carols10cents added a commit to integer32llc/mdBook that referenced this issue Nov 14, 2016
Fixes rust-lang#179.

Highlight.js does not apply syntax highlighting to code blocks marked
no-highlight, nohighlight, plain, or text. When it finds blocks of those
languages, it does not add the `hljs` class to those code blocks either.

highlight.css and tomorrow-night.css use the `hljs` class to give code
blocks their backrgound color and text color, and we want that to apply
even if the code doesn't get syntax highlighting markup.

This is a somewhat hacky solution to get just that behavior! After this
commit, code blocks with no-highlight, nohighlight, plain, or text
language set on them will indeed get the hljs colors.
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

2 participants