Highlight haskell file content using Prism.js

315 Views Asked by At

I'm trying to highlight the Haskell code piece using Prism.js

Below is the sample HTML code which properly highlighting code

<pre><code id="codecontent1" class="language-haskell">import Data.List
import Data.List.Split (chunksOf)

area h w xs = 2 * h * w + count xs + count (rotate xs)
  where rotate = reverse . transpose
        count = sum . map diff
        diff ys = sum $ zipWith ((abs .) . (-)) (0 : ys) (ys ++ [0])


solve :: [Int] -> Int
solve (h:w:xs) = area h w (chunksOf w $ xs)

main = interact $ show . solve . map read . words
</code></pre>

Output: enter image description here

But when try to load the code dynamically (from github repo) it is not properly getting highlighted. HTML Code:

<pre><code id="codecontent" class="language-haskell">loading..</code></pre>

JS code to load content:

function load_code (probname) {
    var url = 'https://rawgit.com/tuxian-root/hackerrank-solutions/master/' + probname + '.hs';
    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", url, true);
    txtFile.onreadystatechange = function() {
        if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) {  // Makes sure it's found the file.
                allText = txtFile.responseText;
                //lines = txtFile.responseText.split("\n"); // Will separate each line into an array
                document.getElementById("codecontent").textContent = txtFile.responseText;
                document.getElementById("codecontent").setAttribute("class", "language-haskell language-markup");
            }
        }
    }
    txtFile.send(null);
    //document.getElementById("codecontent").setAttribute("class", "language-haskell");
}

calling part:

<body style="overflow: hidden;" onload="load_code('algorithms/implementation/3d-surface-area.hs')"></body>

output: enter image description here

I've also tried below, but didn't worked.

<pre data-src="https://github.com/tuxian-root/hackerrank-solutions/blob/master/algorithms/implementation/3d-surface-area.hs"></pre>

Can someone please help me find what I'm missing?

1

There are 1 best solutions below

0
tuxian On BEST ANSWER

I found out the issue that while downloading prism.css/prism.js i missed file highlight option

Ref: https://stackoverflow.com/a/54246906/2173020