~mort/coffeepaste

fbcfca9d4f17c750ce7b0f359d41207309703e15 — Martin Dørum 2 years ago e81cfa8
guess highlighting if there's an error
1 files changed, 24 insertions(+), 4 deletions(-)

M web/view.html
M web/view.html => web/view.html +24 -4
@@ 178,7 178,6 @@ function onLoad(evt) {

    if (window.hljs) {
        doHighlight(xhr.responseText)
        highlighted = true;
    } else {
        code.innerText = xhr.responseText;
    }


@@ 190,14 189,35 @@ function onLoad(evt) {
function doHighlight(text) {
    try {
        if (ext && ext != "auto") {
            code.innerHTML = hljs.highlight(ext, text).value;
        } else {
            code.innerHTML = hljs.highlightAuto(text).value;
            let res = hljs.highlight(ext, text);
            if (res.illegal) {
                console.error("Illegal matches found for " + ext + ":", res.illegalBy.msg);
                console.error("Context:\n" + res.illegalBy.context);
            } else {
                code.innerHTML = res.value;
                highlighted = true;
            }
        }

        if (!highlighted) {
            let res = hljs.highlightAuto(text);
            console.info("Guessed language", res.language);
            if (res.illegal) {
                console.error("Illegal matches found for guessed language:", res.illegalBy.msg);
                console.error("Context:");
                console.error(res.illegalBy.context);
                code.innerText = text;
            } else {
                code.innerHTML = res.value;
                highlighted = true;
            }
        }
    } catch (err) {
        console.error("Failed to highlight for some reason:", err);
        code.innerText = text;
    }

    highlighted = true;
}

function onLineNumClick(evt) {