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.

File Highlight and dynamically loaded content

See original GitHub issue

Hey,

first of all: great work here!

I got a documentation page where I load some views via AJAX. I know that I must call Prism.highlightAll(); when I want to get Prism working on dynamically loaded content. But somehow this doesn’t work with all the additional plugins like File Highlight or Line Highlight. They just don’t apply.

Is this on purpose, a bug or what can I do to get them also running on dynamically loaded content? 😃

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
hiulitcommented, Nov 25, 2014

Hi there! I’m no JavaScript ninja but I did find a solution. I don’t know if it’s the best one, but it worked for me. So here it is:

// File Highlight Plugin - start

var fileHighlight = (function fileHighlight(){  // Reference to fileHighlight function.
                                                // This is a "feature" that doesn't
                                                // come with the default plugin.

    if (!self.Prism || !self.document || !document.querySelector) {
        return;
    }

    var Extensions = {
        'js': 'javascript',
        'html': 'markup',
        'svg': 'markup',
        'xml': 'markup',
        'py': 'python'
    };

    Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function(pre) {
        var src = pre.getAttribute('data-src');
        var extension = (src.match(/\.(\w+)$/) || [,''])[1];
        var language = Extensions[extension] || extension;

        var code = document.createElement('code');
        code.className = 'language-' + language;

        pre.textContent = '';

        code.textContent = 'Loading…';

        pre.appendChild(code);

        var xhr = new XMLHttpRequest();

        xhr.open('GET', src, true);

        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {

                if (xhr.status < 400 && xhr.responseText) {
                    code.textContent = xhr.responseText;

                    Prism.highlightElement(code);
                }
                else if (xhr.status >= 400) {
                    code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
                }
                else {
                    code.textContent = '✖ Error: File does not exist or is empty';
                }
            }
        };

        xhr.send(null);
    });

    return fileHighlight;   // Reference to fileHighlight function.
                            // This is a "feature" that doesn't
                            // come with the default plugin.

})();;

Basically what I did was turn the self-executing anonymous function into one that you can actually call, called fileHighlight(). Now you just have to call it whenever you need it 😉

I know it’s bad to rewrite third-party code, but I don’t know how to extend it properly.

0reactions
Golmotecommented, Jul 3, 2016

I consider this issue was fixed by #514.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Highlight (change background) / of the content in a web ...
Is there any way to change the background-color of text in a web page dynamically, while select ...
Read more >
Solved: JavaScript and dynamically loaded elements
My script works just the way I want it in the full calendar event view because I can simply use: $(document).ready(function() and document....
Read more >
Loading Script Files Dynamically
For loading a script file dynamically using JavaScript, the basic steps are: Create the script element; Set the src attribute on the script...
Read more >
Editing dynamic content in ContentCreator
Editing dynamic content in ContentCreator ... ContentCreator detects editable content on the basis of editor identifiers. The editing functions (EasyEdit and ...
Read more >
How to SCRAPE DYNAMIC websites with Selenium - YouTube
How I use Selenium and Python to automate a browser to scrape data from dynamic websites. These sites load the content through JS...
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