Skip to content

Commit 075c77b

Browse files
authored
Edit markdown (#91)
* Use custom elements for highlight.js * Remove extraneous highlight call
1 parent 37cfe0a commit 075c77b

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

index.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,46 @@ const config: ElmPagesInit = {
3838
})
3939
console.log('Added event listener for mousemove homepage')
4040
}
41-
42-
app.ports.highlightJS.subscribe(function (message) {
43-
console.log('Highlighting code blocks')
44-
hljs.highlightAll();
45-
});
4641
},
4742
flags: function () {
4843
return "You can decode this in Shared.elm using Json.Decode.string!";
4944
},
5045
};
5146

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+
5283
export default config;

src/MarkdownRenderer.elm

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module MarkdownRenderer exposing (renderer)
22

33
import Css exposing (..)
4+
import Html.Attributes
45
import Html.Styled as Html
56
import Html.Styled.Attributes as Attr exposing (css)
67
import Markdown.Block as Block
@@ -242,16 +243,48 @@ codeBlock details =
242243
let
243244
lang =
244245
details.language
245-
|> Maybe.map (\l -> "lang-" ++ String.toLower l)
246246
|> Maybe.withDefault "nohighlight"
247247
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
257252
]
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+
-- ]

0 commit comments

Comments
 (0)