File tree Expand file tree Collapse file tree 2 files changed +79
-15
lines changed Expand file tree Collapse file tree 2 files changed +79
-15
lines changed Original file line number Diff line number Diff line change @@ -38,15 +38,46 @@ const config: ElmPagesInit = {
38
38
} )
39
39
console . log ( 'Added event listener for mousemove homepage' )
40
40
}
41
-
42
- app . ports . highlightJS . subscribe ( function ( message ) {
43
- console . log ( 'Highlighting code blocks' )
44
- hljs . highlightAll ( ) ;
45
- } ) ;
46
41
} ,
47
42
flags : function ( ) {
48
43
return "You can decode this in Shared.elm using Json.Decode.string!" ;
49
44
} ,
50
45
} ;
51
46
47
+ // Highlight.js editing the DOM doesn't work well with Elm
48
+ // custom elements work better since it is not managed by Elm
49
+ // I tried using web components (custom elements with Shadow DOM)
50
+ // but it was too tricky getting the shadow DOM to import the highlight.js CSS from the main DOM
51
+ customElements . define ( 'highlightjs-code' ,
52
+ class extends HTMLElement {
53
+ codeElem : HTMLElement ;
54
+
55
+ constructor ( ) {
56
+ super ( ) ;
57
+ console . log ( 'highlightjs-code created' , this . codeElem )
58
+ }
59
+ connectedCallback ( ) { this . setTextContent ( ) ; }
60
+ attributeChangedCallback ( ) { this . setTextContent ( ) ; }
61
+ static get observedAttributes ( ) { return [ 'lang' , 'code' ] ; }
62
+
63
+ // Our function to set the textContent based on attributes.
64
+ setTextContent ( ) {
65
+ const lang = this . getAttribute ( 'lang' ) || 'plaintext' ;
66
+ const code = this . getAttribute ( 'code' ) || '' ;
67
+ const hljsVal = hljs . highlight ( code , { language : lang } ) ;
68
+ console . log ( hljsVal )
69
+ this . innerHTML = `
70
+ <style>
71
+ code {
72
+ border-radius: 0.5em;
73
+ font-size: 0.75em;
74
+ }
75
+ </style>
76
+ <pre><code class="hljs language-${ lang } ">${ hljsVal . value } </code></pre>
77
+ `
78
+ }
79
+ }
80
+ ) ;
81
+
82
+
52
83
export default config ;
Original file line number Diff line number Diff line change 1
1
module MarkdownRenderer exposing (renderer )
2
2
3
3
import Css exposing (..)
4
+ import Html.Attributes
4
5
import Html.Styled as Html
5
6
import Html.Styled.Attributes as Attr exposing (css )
6
7
import Markdown.Block as Block
@@ -242,16 +243,48 @@ codeBlock details =
242
243
let
243
244
lang =
244
245
details. language
245
- |> Maybe . map ( \ l -> " lang-" ++ String . toLower l)
246
246
|> Maybe . withDefault " nohighlight"
247
247
in
248
- Html . pre []
249
- [ Html . code
250
- [ Attr . class lang
251
- , css
252
- [ borderRadius ( em 0.5 )
253
- , fontSize ( em 0.75 )
254
- ]
255
- ]
256
- [ Html . text details. body ]
248
+ Html . node
249
+ " highlightjs-code"
250
+ [ Attr . attribute " lang" lang
251
+ , Attr . attribute " code" details. body
257
252
]
253
+ []
254
+
255
+
256
+
257
+ -- Html.pre
258
+ -- []
259
+ -- [ Html.code
260
+ -- [ Attr.class lang
261
+ -- , css
262
+ -- [ borderRadius (em 0.5)
263
+ -- , fontSize (em 0.75)
264
+ -- ]
265
+ -- ]
266
+ -- [ Html.node
267
+ -- "highlightjs-code"
268
+ -- [ Attr.attribute "lang" lang
269
+ -- , Attr.attribute "code" details.body
270
+ -- ]
271
+ -- []
272
+ -- ]
273
+ -- ]
274
+ -- Html.node
275
+ -- "highlightjs-code"
276
+ -- [ Attr.attribute "lang" lang
277
+ -- , Attr.attribute "code" details.body
278
+ -- ]
279
+ -- []
280
+ -- Html.pre
281
+ -- []
282
+ -- [ Html.code
283
+ -- [ Attr.class lang
284
+ -- , css
285
+ -- [ borderRadius (em 0.5)
286
+ -- , fontSize (em 0.75)
287
+ -- ]
288
+ -- ]
289
+ -- [ Html.text details.body ]
290
+ -- ]
You can’t perform that action at this time.
0 commit comments