Search code examples
staticeditorace-editor

ACE static highlight without using PHP tags


This is an example of using ext-static-highlight.js of ACE Editor. How would I set the inline option to true, so I can avoid using PHP tags when highlighting the code?

<script>
var highlight = ace.require("ace/ext/static_highlight")
var dom = ace.require("ace/lib/dom")
function qsa(sel) {
    return Array.apply(null, document.querySelectorAll(sel));
}

qsa(".code").forEach(function (codeEl) {
    highlight(codeEl, {
        mode: codeEl.getAttribute("ace-mode"),
        theme: codeEl.getAttribute("ace-theme"),
        startLineNumber: 1,
        showGutter: codeEl.getAttribute("ace-gutter"),
        trim: true
    }, function (highlighted) {

    });
});
</script>

Solution

  • You need to pass an object to the highlight function as a mode {path: "ace/mode/php", inline: true}. so use something like this:

    <script>
    var highlight = ace.require("ace/ext/static_highlight")
    var dom = ace.require("ace/lib/dom")
    function qsa(sel) {
        return Array.apply(null, document.querySelectorAll(sel));
    }
    
    qsa(".code").forEach(function (codeEl) {
        var mode = codeEl.getAttribute("ace-mode");
        if (mode == "php-inline")
            mode = {path: "ace/mode/php", inline: true}
        highlight(codeEl, {
            mode: mode,
            theme: codeEl.getAttribute("ace-theme"),
            startLineNumber: 1,
            showGutter: codeEl.getAttribute("ace-gutter"),
            trim: true
        }, function (highlighted) {
    
        });
    });
    </script>