package html_of_jsx

  1. Overview
  2. Docs
Render HTML writing JSX

Install

Dune Dependency

Authors

Maintainers

Sources

html_of_jsx-0.0.1.tbz
sha256=d1a36abcbebd23adfaffe9eed27e69045c1e112da17bfebc385355d2a64f1fb7
sha512=bee674151f2ab6fba41acc1234118b5eca5379aedab2907da517bdeb604ddd02807519b9b29b4eea09595ced9936300b9a6ebf16c9edd92d959151daab4e7a96

doc/src/html_of_jsx.lib/Html_of_jsx.ml.html

Source file Html_of_jsx.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(* TODO: Use buffer instead of Printf and String.concat *)
let render element =
  let rec render_element element =
    let open Jsx in
    match element with
    | Null -> ""
    | Fragment list | List list ->
        list |> List.map render_element |> String.concat ""
    | Component f -> render_element (f ())
    | Node { tag; attributes; _ } when Html.is_self_closing_tag tag ->
        Printf.sprintf "<%s%s />" tag (Jsx.Attribute.to_string attributes)
    | Node { tag; attributes; children } when tag == "html" ->
        Printf.sprintf "<!DOCTYPE html><%s%s>%s</%s>" tag
          (Jsx.Attribute.to_string attributes)
          (children |> List.map render_element |> String.concat "")
          tag
    | Node { tag; attributes; children } ->
        Printf.sprintf "<%s%s>%s</%s>" tag
          (Jsx.Attribute.to_string attributes)
          (children |> List.map render_element |> String.concat "")
          tag
    | Text text -> Html.encode text
  in
  render_element element
OCaml

Innovation. Community. Security.