How to use highlight.js outside the browser?
See original GitHub issueI’m considering pre-highlighting code on the server-side to avoid making users download the highlight.js library, but I’m not exactly sure how to do it.
Say I have a README file in Markdown, and I use marked
to parse it into HTML:
require("marked").parse(require("fs").readFileSync("./README.md").toString())
Now I have this HTML:
<h1 id="jiggle">jiggle</h1>
<p>Jiggle arrays to slightly rearrange their values</p>
<h2 id="installation">Installation</h2>
<pre><code class="lang-sh">npm install jiggle --save
</code></pre>
<h2 id="usage">Usage</h2>
<pre><code class="lang-js">var jiggle = require("jiggle")
// The jiggle() function takes an array as input and returns an array
jiggle([0,1,2,3,4,5,6,7,8,9])
// [ 1, 2, 0, 4, 3, 5, 6, 7, 9, 8 ]
// It can also jiggle strings
jiggle("jabberwocky")
// jbbeawockyr
// It always returns a new object without altering the original
var orig = ["a","b","c","d","e","f"]
var dupe = jiggle(orig)
// orig: ['a','b','c','d','e','f']
// dupe: ['a','b','d','e','c','f']
// And if you like jiggling you can keep on doing it
jiggle(jiggle(jiggle("supercalifragilisticexpialidocious")))
// supcaerligralitiiscpefaldioxciosiu
</code></pre>
<h2 id="tests">Tests</h2>
<pre><code class="lang-sh">npm install
npm test
</code></pre>
How would I use highlight.js to apply syntax highlighting markup to this HTML string? Please forgive me if I missed the documentation somewhere that explains this. 😃
Issue Analytics
- State:
- Created 9 years ago
- Comments:13 (3 by maintainers)
Top Results From Across the Web
How to use highlight.js
// first, find all the div.code blocks document.querySelectorAll('div.code').forEach(el => { // then highlight each hljs.highlightElement(el); });.
Read more >Highlight.js - mikesir87's blog
Highlight.js is a syntax highlighter written in JavaScript. It works in the browser as well as on the server. It works with pretty...
Read more >Prism.js
If you want to opt-out of highlighting but still use plugins like Show Invisibles, add use language-plain class instead. Manual highlighting. If you...
Read more >How can I turn off auto language detection in highlight.js and ...
It's possible to disable highlight.js auto language detection with hljs.configure({languages:[]}) ...
Read more >How do I add highlight.js to my website? - Quora
You can get highlight.js as a hosted, or custom-build, browser script or as a server module. Right out of the box the browser...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There is documentation in marked for this. The example only uses auto highlighting when it is possible to specify a language like the Pygments example.
Give a compatible version: