I use highlight.js in my site hosted on github pages. The markdown engine redcarpet2 generates HTML tags from markdown code by mark ```language. Since I manually mark the language of all code blocks respectively, I don't really need auto language detection.
In fact, auto language detection produces problems. Without rendering, the markdown correctly translate the code block to
<code class="r language-r" data-lang="r"></code>
which should later be rendered as R code. But when I load highlight.js and run
hljs.initHighlightingOnLoad();
the auto detect also works and makes the code tags mixed like
<code class="r language-r hljs php" data-lang="r"></code>
where php should be eliminated from the class.
Using tag selector and iteratively render all code blocks will solve the problem. But is it better to allow us to disable language auto detection in the config to make it?
I prefer a new method that works like initHighlightingOnLoad() to render all code blocks except that it does not detect language but take the prespecified language as given.
Could you give a test case, because I've try to replicate this and the auto detection never runs. The only way I can see this happening is when highlight.js can't match a language described in an html class name.
I can probably guess that the package that @renkun-ken uses doesn't contain R language definition. In which case we don't recognize neither "r" nor "language-r" class names as languages and proceed with auto detection with available set of languages. I kinda inclined to think that the proper way of fixing it would be getting a package with all the languages that are used on the site. @renkun-ken will that work for you?
Oh, I only just realized that we _do_ have a setting to completely disable auto detection on the page: hljs.configure({languages: []}). So, this looks solved then?
Thanks anyway, @isagalaev, maybe I should speak more clearly.
Previously, I use the default theme of highlight.js (the latest version hosted on CDNJS. I choose markdown (redcarpet2) to generate HTML code. The syntax is Markdown GFM. The problem was: In the markdown file I write r to start a code block and to end the code block. The HTML code generated by markdown is correct:
<code class="r language-r" data-lang="r"></code>
However, when I load highlight.js and call initHighlightingOnLoad(), the code blocks are detected again and becomes:
<code class="r language-r hljs php" data-lang="r"></code>
which is not desirable because I have already specified the language R and auto detection is not necessary at all and makes mistakes in this case. It happened in my blog pages like this.
Later, I changed the theme of highlight.js to Google Code, and the problem no longer exists and the syntax highlighting works perfectly, just rendering the language I specify. But I don't know why.
hljs.configure({languages: []}) won't help because it blocks all languages I want to render.
If you're using a CDN version then it doesn't contain R language definition at all. So you won't get your snippet highlighted as R with or without auto detection. In your case highlight.js simply doesn't recognize the language in the class name and goes ahead with auto detection.
If you specify a _known_ language, e.g.:
<code class="javascript language-javascript" data-lang="javascript"></code>
It will be highlighted as JavaScript and highlight.js won't try to do any detection.
I suggested hljs.configure({languages: []}) as a way to disable highlighting for those languages that highlight.js doesn't recognize. It only blocks auto detection, but if you specify known language it will be highlighted as usual.
And changing theme certainly has nothing to do with the highlighting itself, it probably just was something coincidental.
It's strange that when I use cdnjs version of highlight.js, it almost looks right since some R keywords are highlighted and no further language detection seems to happen. When I switch back to original version <script src="http://yandex.st/highlightjs/8.0/highlight.min.js"></script> R code cannot be recognized and auto detection happens. The original version seems not to include R by default?
Another question is: How can I write markdown to tell highlight.js the code block is a pure text and no need to render? I tried ``no-highlight or something and markdown cannot generate code due to syntax error of-`
The original version seems not to include R by default?
I was actually going to ask if you used to different builds on you site and locally. And yes, the yandex CDN uses only the common languages in it's build. And looking into CDNJS, it seems to include all languages, which R is apart of.
How can I write markdown to tell highlight.js the code block is a pure text
Only way is with no-highlight.
Thanks, @sourrust, you are right. CDNJS includes all languages.
For no-highlight question, I raise another issue #429, because no-highlight seems incompatible with Markdown syntax.
Alright, I'll close this issue now since it is resolved and get to the new one you opened.
btw, the version on cdnjs with all the languages is, frankly, a mistake. It was supposed to be exactly like the one hosted on Yandex. And it probably will be like this starting from 8.1.
Most helpful comment
Oh, I only just realized that we _do_ have a setting to completely disable auto detection on the page:
hljs.configure({languages: []}). So, this looks solved then?