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.

Cannot read property 'replace' of undefined(..) when calling highlightBlock() or highlightAuto()

See original GitHub issue

When I call hljs.highlightBlock(); or hljs.highlightAuto(); I get this error: Cannot read property 'replace' of undefined(..) img_2015 09 14_12h40m34s_001_

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
isagalaevcommented, Oct 28, 2015

Ah… This doesn’t work because hljs.initHighlighting only works once. To highlight anything dynamically you call hljs.highlightBlock on specific code blocks. It’s okay to let hljs.initHighlighting work on $(document).read but on pjax:end you should first figure out which blocks arent’ highlighted yet:

$(document).on('pjax:end', function() {
  $('pre code:not(.hljs)').each(function(i, block) {
    hljs.highlightBlock(block);
  });
});

I haven’t tested the code, it’s just to show the idea.

And your original code didn’t work because hljs.highlightAuto is a lower level function that accepts code as a simple string, not a DOM node.

0reactions
isagalaevcommented, Dec 2, 2015

I think it’s safe to close by now, as it’s obviously not a bug with either of the methods mentioned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[SOLVED] Cannot Read Property 'replace' of Undefined in JS
This error occurs when you attempt to call the replace() method on a variable that has a value of undefined . const str...
Read more >
TypeError: Cannot read property 'replace' of Undefined in JS
The "Cannot read property 'replace' of undefined" error occurs when calling the replace() method on a variable that stores an undefined value.
Read more >
How to use highlight.js
The bare minimum to auto-detect the language and highlight some code. // load the library and ALL languages hljs = require('highlight.js'); html =...
Read more >
TypeError: Cannot read property 'replace' of undefined
I have been trying to make my first express application and I have been getting a error with range.replace() ...
Read more >
Core API — highlight.js 11.7.0 documentation - Read the Docs
highlightAuto (code, languageSubset). Highlighting with language detection. Accepts a string with the code to highlight and an optional array of language ...
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