question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add initHighlight callable multiple times.

See original GitHub issue

Add a variant of initHighlight function that can be called multiple times. Useful for single-page applications with lazy-loaded content, that don’t wish to specifically target new code blocks and highlight them manually.

Currently this can be accomplished as follows:

hljs.initHighlighting.called = false;
hljs.initHighlighting();

But that seems bad practice as it’s more complicated than necessary, and encourages manipulation of an internal variable.

We could do:

hljs.initHighlighting(true);

But some might not like the boolean trap. In which case, we could do:

hljs.initHighlighting({force: true});

or:

hljs.initHighlighting({once: false});

Thoughts? I can submit a pull request.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
isagalaevcommented, Aug 17, 2015

In the context of single-page applications (SPAs) or AJAX sites, would you not expect the desire to conveniently re-run to be even more common than the run-once behavior?

No, not really. While the desire to highlight new code blocks is pretty straight-forward the details would differ: people use millions of different frameworks with different tools for reacting at new code blocks. Resorting to some global function that does or does not what you want is far less convenient than just calling highlightBlock or highlightAuto on new content. This also takes care of filtering out already highlighted elements.

And if you do want such a function in your particular case it’s a simple two-liner, really:

var blocks = document.querySelectorAll('pre code:not(.hljs)');
Array.prototype.forEach.call(blocks, highlightBlock);

So I’d say highlightBlock is an adequate API and adding wrappers around it is a slippery slope of chasing all the slightly different variants that people would want.

0reactions
speigecommented, Jul 23, 2018

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advice on how to make a javascript function callable multiple ...
Basically, I want to try to call several instances of this slider in my HTML document. I have tried wrapping the function inside...
Read more >
Diff - gerrit - Google Git
If a merge +is necessary yet we already have this many threads running, the incoming thread +(that is calling add/updateDocument) will block until...
Read more >
Collaborative JavaScript Debugging - JS Bin
A live pastebin for HTML, CSS & JavaScript and a range of processors, including SCSS, CoffeeScript, Jade and more...
Read more >
PEP 677 – Callable Type Syntax
We can add types to this example to detect the runtime error: ... There are a few usability challenges with Callable we can...
Read more >
File: elpy.el - Debian Sources
Elpy creates RPC buffers over time, depending on python interpreters and ... Elpy can automatically insert parentheses after completing callable objects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found