【问题标题】:Highlight haskell file content using Prism.js使用 Prism.js 突出显示 haskell 文件内容
【发布时间】:2020-04-29 10:27:56
【问题描述】:

我正在尝试使用 Prism.js 突出显示 Haskell 代码段

以下是正确突出显示代码的示例 HTML 代码

<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>

输出:

但是当尝试动态加载代码时(来自 github repo),它没有正确突出显示。 HTML 代码:

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

加载内容的JS代码:

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");
}

调用部分:

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

输出:

我也在下面尝试过,但没有成功。

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

有人可以帮我找到我缺少的东西吗?

【问题讨论】:

    标签: javascript html prism.js


    【解决方案1】:

    我发现在下载 prism.css/prism.js 时我错过了文件突出显示选项的问题

    参考:https://stackoverflow.com/a/54246906/2173020

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-07
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      相关资源
      最近更新 更多