summaryrefslogtreecommitdiff
path: root/editor/actions/of_markdown.ml
blob: 580c6c4af294b10e5da35d20bc29c7dff8562eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
open StdLabels
module Js = Js_of_ocaml.Js
module PM = Prosemirror

type node_t = PM.Model.node Js.t

module FromMarkdown = struct

  type t = PM.t * Omd.doc

  (** Add the given mark in the mark list.
      The attributes, if any, are added to the mark properties. *)
  let add_attribute
    : string -> PM.Model.schema Js.t -> PM.Model.mark Js.t list -> Jv.prop PM.O.t Js.opt -> PM.Model.mark Js.t list
    = fun name schema marks attributes ->
      match PM.O.get schema##.marks name with
      | None -> marks
      | Some mark_type ->
        let m = schema##mark_fromType mark_type attributes in
        m::marks

  (** The function [parse_inline] will tranform all the inline markup to a
      Prosemirror node.

      This apply to element like bold, links and so one. *)
  let rec parse_inline_content
    : Prosemirror.View.editor_view Js.t
      -> PM.t
      -> PM.Model.mark Js.t list
      -> Omd.attributes Omd.inline
      -> node_t Js.js_array Js.t
    = fun view pm marks -> function
      | Omd.Concat (attrs, els) ->
        ignore attrs;

        let nodes =
          List.to_seq els
          |> Seq.map (fun e -> parse_inline_content view pm marks e)
          |> Array.of_seq
          |> Js.array in

        (* Flatten each array returned *)
        nodes##reduce_init
          (Js.wrap_callback
           @@ fun (init: node_t Js.js_array Js.t) (elems: node_t Js.js_array Js.t) _ _ ->
           init##concat elems)
          (new%js Js.array_empty)

      | Omd.Strong (attrs, content) ->
        (* Strong (or Emph) elements just add the coresponding mark and
           process the content further *)
        ignore attrs;
        let marks = add_attribute
            "strong"
            view##.state##.schema
            marks
            Js.null in
        parse_inline_content view pm marks content

      | Omd.Emph (attrs, content) ->
        ignore attrs;
        let marks = add_attribute
            "em"
            view##.state##.schema
            marks
            Js.null in
        parse_inline_content view pm marks content

      | Omd.Text (attrs, text) ->
        ignore attrs;
        (* Convert the marks as js array *)
        let js_marks = Js.array @@ Array.of_list marks in
        Js.array
          [| view##.state##.schema##text
               (Jstr.of_string text)
               (Js.some js_marks)
          |]

      | Omd.Code (attrs, content) ->
        ignore attrs;
        let marks = add_attribute
            "code"
            view##.state##.schema
            marks
            Js.null in
        let js_marks = Js.array @@ Array.of_list marks in
        Js.array
          [| view##.state##.schema##text
               (Jstr.of_string content)
               (Js.some js_marks)
          |]

      | Omd.Link (attrs, link_attrs) ->
        ignore attrs;
        let attrs' = PM.O.init
            [| "href", link_attrs.destination
            (* TODO Handle title *)
            |] in
        let marks = add_attribute
            "link"
            view##.state##.schema
            marks
            (Js.some attrs') in
        parse_inline_content view pm marks link_attrs.label

      | _ ->
        (* TODO Handle Break *)
        Brr.Console.(log [Jstr.v "Other"]);
        new%js Js.array_empty

  let rec parse_block
    : Prosemirror.View.editor_view Js.t -> PM.t -> Omd.attributes Omd.block -> node_t option
    = fun view pm -> function

      | Omd.Paragraph (attrs, elements) ->
        ignore attrs;
        let marks = [] in
        (* Transform each node inside the markdown document and add them into the
           paragraph node *)
        let nodes = parse_inline_content view pm marks elements in
        let fragment = PM.Model.Fragment.from_array pm nodes in
        let node = view##.state##.schema##node
            (Jstr.v "paragraph")
            (Js.null)
            (Js.some fragment)
            (Js.null) in
        Some node

      | Omd.Heading (attrs, level, elements) ->
        ignore attrs;
        let marks = [] in
        (* Heading is like a paragraph, but with an attribute (the level) *)
        let attributes = object%js val level = level end
        and nodes = parse_inline_content view pm marks elements in
        let fragment = PM.Model.Fragment.from_array pm nodes in
        let node = view##.state##.schema##node
            (Jstr.v "heading")
            (Js.some attributes)
            (Js.some fragment)
            (Js.null) in
        Some node

      | Omd.List (attrs, type_, spacing, elements) ->
        ignore attrs;
        ignore spacing;

        let type_list = match type_ with
          | Omd.Ordered _ -> "ordered_list"
          | Omd.Bullet _ -> "bullet_list" in

        (* The whole list node is declared as ordered or bullet depending of
           the type given by the markdown.

           Each element insiide the list is transformed as a list_item.

           The list_item node can itself contains other blocks (recursively) *)
        let nodes = List.map elements
            ~f:(fun list_entry ->
                let nodes = (List.filter_map list_entry
                               ~f:(fun e -> parse_block view pm e))
                            |> Array.of_list
                            |> Js.array in
                let fragment = PM.Model.Fragment.from_array pm nodes in
                view##.state##.schema##node
                  (Jstr.v "list_item")
                  (Js.null)
                  (Js.some fragment)
                  (Js.null)
              ) in
        let nodes_array= nodes
                         |> Array.of_list
                         |> Js.array in

        let fragment = PM.Model.Fragment.from_array pm nodes_array in
        let node = view##.state##.schema##node
            (Jstr.v type_list)
            (Js.null)
            (Js.some fragment)
            (Js.null) in
        Some node

      | Omd.Thematic_break attrs ->
        ignore attrs;
        let node = view##.state##.schema##node
            (Jstr.v "horizontal_rule")
            (Js.null)
            (Js.null)
            (Js.null) in
        Some node

      | Omd.Blockquote(attrs, elements) ->
        ignore attrs;
        let nodes =
          List.filter_map elements
            ~f:(fun e -> parse_block view pm e)
          |> Array.of_list
          |> Js.array in
        let fragment = PM.Model.Fragment.from_array pm nodes in
        let node = view##.state##.schema##node
            (Jstr.v "blockquote")
            (Js.null)
            (Js.some fragment)
            (Js.null) in
        Some node

      | _ ->
        Brr.Console.(log [Jstr.v "Other block"]);
        None

  let parse
    : Prosemirror.View.editor_view Js.t -> PM.t -> Omd.doc -> unit
    = fun view pm doc ->
      Brr.Console.( log [ doc ]);
      (* Transform each node inside the markdown document and add them into the
         root node *)
      let nodes =
        doc
        |> List.filter_map ~f:(fun b -> parse_block view pm b)
        |> Array.of_list
        |> Js.array
      in
      let fragment = PM.Model.Fragment.from_array pm nodes in
      let document = view##.state##.schema##node
          (Jstr.v "doc")
          (Js.null)
          (Js.some fragment)
          (Js.null) in
      Brr.Console.(log [ document ])

  let update
    : t -> State.t -> State.t
    = fun (pm, doc) state ->
      let () = parse state.State.view pm doc in
      state
end