我是榆樹新手,我真的很喜歡它,但我遇到了一個問題,我似乎無法將我的頭圍繞。變換Html DOM
我有一個HTML DOM,例如
div []
[ h1 [] [text "Headline 1"]
, p [] [text "Some text"]
, h2 [] [text "Headline 2"]
]
我想增加一個鏈接各自爲H內[1-6]元件等將其轉換爲類似的信息(保持簡單)
div []
[ h1 [] [ text "Headline 1"
, [a [name "headline"] [text "#"]
]
, p [] [text "Some text"]
, h2 [] [text "Headline 2"
, [a [name "headline"] [text "#"]
]
]
這在概念上不是很難。查看DOM,如果元素是h [1-6],則添加一個a-link作爲子元素。然而,我對榆樹的理解並不足以讓它起作用。
這是我一直在嘗試到目前爲止。
transform : Html a -> Html a
transform node =
-- check if the tag is h1-h6
case node.tag of
-- add a-link to h1 children
"h1" -> { node | children = (a [name "headline"] [text "#") :: node.children }
"h2" -> { node | children = (a [name "headline"] [text "#") :: node.children }
-- do this for all nodes in the tree
_ -> { node | children = List.map transform node.children }
這是行不通的。
The type annotation for `transform` does not match its definition.
40| transform : Html a -> Html a
^^^^^^^^^^^^^^^^
The type annotation is saying:
VirtualDom.Node a -> VirtualDom.Node a
But I am inferring that the definition has this type:
{ b | tag : String, children : List (Html a) }
-> { b | children : List (Html a), tag : String }
我明白,我不能這樣做,因爲node.tag
泛型類型a
可能沒有那場。它不是安全的。例如,文本節點沒有標記字段,但仍是Html.Html a
的實例。
> text "Hello World"
{ type = "text", text = "Hello World" } : Html.Html a
我的問題是,我該怎麼做?我可以這樣做嗎?或者我不應該這樣做?
我懷疑產量的JavaScript。我的「Html msg」來自Markdown的輸出。toHtml',所以在從Markdown.toHtml返回'Html msg'之前,我沒有任何好的方法來處理它。太糟糕了,它不可能改變輸出。 –