Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I turn off auto language detection in highlight.js and render all blocks?

I use redcarpet2 markdown in my site hosted by Github pages. I switch the syntax highlighter from pygments to highlight.js.

I specified the language for each code block by

```r
(some r code)
```

So I don't need the auto language detection by highlight.js, actually if I don't specify the language it will always mis-recognize my R code to php or perl code.

The problem is: the HTML tag for <code> is somehow mixed.

Without rendering, the markdown correctly translate the code block with <code class="r language-r" data-lang="r"></code> which should be rendered as R code. But when I load highlight.js and run <script>hljs.initHighlightingOnLoad();</script>, 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.

How can I fix the problem?

like image 259
Kun Ren Avatar asked Feb 16 '14 07:02

Kun Ren


People also ask

How do you highlight in Javascript?

js code to highlight the text. There are many built-in functions in mark. js but we are using two functions for our requirement that is mark() and unmark() function respectively. Here mark() is used to highlight the search text and unmark() is used to remove highlighting the text that is highlighted before.

What is the use of highlight js?

Highlight. js tries to automatically detect the language of a code fragment. The heuristics is essentially simple: it tries to highlight a fragment with all the language definitions and the one that yields most specific modes and keywords wins.

How do you highlight syntax in HTML?

When creating or editing posts, syntax highlighting is assigned to the preview based on question's tags as soon as you stop typing for 5 seconds. Stack Exchange does not have its own syntax highlighting engine. It uses highlight. js, and may not necessarily be using the latest release of that library.


1 Answers

Probably your highlight.pack.js does not include R language, you can download it here just tick the appropriate checkboxes. When HighlightJS sees unknown class for code tag it tries to guess automatically right language.

like image 182
Alex Titarenko Avatar answered Sep 28 '22 23:09

Alex Titarenko