1+ import contour
12import gleam/list
3+ import gleam/option . { type Option , Some }
24import lustre/attribute . { attribute }
35import lustre/element . { type Element }
46import lustre/element/html
7+ import pearl
58import website/component/footer
69import website/component/header
710
8- pub fn head ( page : String ) -> Element ( a ) {
11+ pub fn head ( page : String ) -> Element ( _ ) {
912 html . head ( [ ] , [
1013 html . meta ( [ attribute ( "charset" , "UTF-8" ) ] ) ,
1114 html . title ( [ ] , page <> " | Gears" ) ,
@@ -38,7 +41,7 @@ pub type Section(a) {
3841 Section ( content : List ( Element ( a) ) )
3942}
4043
41- pub fn page ( name : String , sections : List ( Section ( a) ) ) -> Element ( a ) {
44+ pub fn page ( name : String , sections : List ( Section ( a) ) ) -> Element ( _ ) {
4245 html . html ( [ attribute ( "lang" , "en" ) ] , [
4346 head ( name ) ,
4447 html . body ( [ attribute . class ( "min-h-screen bg-slate-800 text-white" ) ] , [
@@ -64,6 +67,33 @@ pub fn text_page(header: String, content: List(Element(a))) -> Section(a) {
6467 ] )
6568}
6669
67- pub fn dangerous_html ( html : String ) -> Element ( a ) {
70+ pub fn dangerous_html ( html : String ) -> Element ( _ ) {
6871 element . unsafe_raw_html ( "" , "span" , [ ] , html )
6972}
73+
74+ pub fn code_block ( language : Option ( String ) , code_text : String ) -> Element ( _) {
75+ let code = case language {
76+ Some ( "erlang" ) | Some ( "erl" ) ->
77+ dangerous_html ( pearl . highlight_html ( code_text ) )
78+ Some ( "gleam" ) -> dangerous_html ( contour . to_html ( code_text ) )
79+ _ -> html . text ( code_text )
80+ }
81+
82+ html . div ( [ attribute . class ( "codeblock" ) ] , [
83+ html . button (
84+ [
85+ attribute . class ( "copy-button" ) ,
86+ attribute (
87+ "onclick" ,
88+ "navigator.clipboard.writeText(`" <> code_text <> "`)" ,
89+ ) ,
90+ ] ,
91+ [ html . text ( "copy" ) ] ,
92+ ) ,
93+ html . pre ( [ ] , [
94+ html . code ( [ attribute ( "data-lang" , option . unwrap ( language , "text" ) ) ] , [
95+ code ,
96+ ] ) ,
97+ ] ) ,
98+ ] )
99+ }
0 commit comments