Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntaxhighlighter can't find any brushes after ajax request

I'm using Syntaxhighlighter on a web page, I have made a javascript function to load all brushes using SyntaxHighlighter.autoloader(...) and apply SyntaxHighlighter.all()

I run this function once when the page gets loaded, result: the syntax highlighting gets applied correctly.

Afterwards, I load some new content using ajax and run this same function to highlight the new content as well.

However this time Syntaxhighlighter seems to have forgotten about all the loaded brushes, I get an alert saying the brush is not loaded.

I have no idea what is causing this although I have looked around and found 2 possible causes:

An issue on the bitbucket repository

This looks like the solution but when I use the unpacked shCore.js from the repository my IDE indicates a syntax error and I get javascript errors when I try to run it anyway.

Another solution I've found on a few answers on other similar Stack Overflow posts is to use SyntaxHighlighter.highlight() instead of .all() after the page has been loaded. This doesn't work however.

The function I'm using:

function loadSyntaxHighLighter() {
    SyntaxHighlighter.autoloader(
            'ahk ' + app.assets + 'js/syntaxhighlighter/brushes/shBrushAhk.js',
            'aps ' + app.assets + 'js/syntaxhighlighter/brushes/shBrushAppleScript.js'
            //...
    );

    SyntaxHighlighter.defaults['toolbar'] = false;

    if (SyntaxHighlighter != 'undefined') {
        SyntaxHighlighter.highlight();
    } else {
        SyntaxHighlighter.all();
    }
}

Does anyone have any idea on how to fix this? (Or can someone point me out how I can make the change suggested on bitbucket)

Thanks

like image 750
Geoffrey De Vylder Avatar asked Nov 05 '22 14:11

Geoffrey De Vylder


1 Answers

I had the same issue. I've just solved it similar to bitbucket's suggestions.

Add following code just before: loadSyntaxHighLighter() call:

SyntaxHighlighter.vars.discoveredBrushes=null;

SyntaxHighlighter will be forced to rediscover brushes on your page and load appopriate ones.

Regards Tom

like image 108
Tomasz Rozmus Avatar answered Nov 15 '22 09:11

Tomasz Rozmus