用Elm突出显示语法

时间:2016-07-01 15:59:11

标签: jquery regex syntax-highlighting elm

我正在编写一个简单的应用程序,它会放置一个突出显示语法的代码块。现在我正在使用highlightjs对我的语法高亮显示。

为什么我的highlightBlock来电不在这里工作?

对于细心的读者:我选择ModelMsg是完全荒谬的,因为我现在并没有真正使用它们。

import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)

main =
  Html.program
  { init = init
  , view = view
  , update = update
  , subscriptions = subscriptions
  }

-- Model

type alias Model = { state: Int }

init : (Model, Cmd Msg)
init = (Model 1, Cmd.none)

-- view

view : Model -> Html Msg
view model = div [ ] [ codesample ]

codesample : Html Msg
codesample = pre [ myStyle ] [ code [] [ Html.text thisCode ] ]

myStyle : Html.Attribute Msg
myStyle = Html.Attributes.style [("background-color", "#F0F0F0"), ("width", "500px")]

thisCode : String
thisCode = """
import Html exposing (..)
import Html.App as Html
import Html.Events exposing (..)
import Random
import Svg exposing (..)

main =
  Html.program
  { init = init
  , view = view
  , update = update
  , subscriptions = subscriptions
  }
"""

-- update


type Msg
  = Roll  | NewFace Int

update : Msg -> Model -> (Model, Cmd Msg)
update msg model = ( model , Cmd.none )

-- subscriptions

subscriptions : Model -> Sub Msg
subscriptions model = Sub.none

我打电话给highlightjs后,我已经超出了纯榆树的范围。我下载了Elm语法高亮包并按如下方式调用库但没有...我怀疑端口是必要的(如Slack所述)

<html>
<head>
<link rel="stylesheet" href="styles/solarized-dark.css">
<script src="highlight.pack.js"></script>
</head>
<body>

<div id="my-elm-block"></div>
<script src="block.js"></script>
<script>
  var node = document.getElementById("my-elm-block");
  var app  = Elm.Main.embed(node);
  hljs.highlightBlock(node);

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

出于某种原因,在调用embed之后直接调用Elm生成的DOM上的函数不起作用,但是如果你将它包装在一个很小的超时中,它就会这样做。将您的高亮线更改为:

setTimeout(function() { hljs.highlightBlock(node); }, 1);

我不得不多次使用这个黑客并且没有问题。