Commit
+48 -34 +/-2 browse
1 | diff --git a/lib/note.ml b/lib/note.ml |
2 | index f5e53d7..ccd22d6 100644 |
3 | --- a/lib/note.ml |
4 | +++ b/lib/note.ml |
5 | @@ -58,37 +58,31 @@ tags: [] |
6 | # This is a Note! |
7 | |} |
8 | |
9 | - let rec extract_structured_data (accm : Ezjsonm.value list) (doc : Omd.doc) : |
10 | - Ezjsonm.value list = |
11 | - match doc with |
12 | - | [] -> accm |
13 | - | hd :: tl -> ( |
14 | - match hd.bl_desc with |
15 | - | Code_block (kind, content) -> ( |
16 | - match kind with |
17 | - | "json" -> |
18 | - let accm = accm @ [ Ezjsonm.from_string content ] in |
19 | - extract_structured_data accm tl |
20 | - | "yaml" -> |
21 | - let accm = accm @ [ Ezjsonm.wrap (Yaml.of_string_exn content) ] in |
22 | - extract_structured_data accm tl |
23 | - | _ -> extract_structured_data accm tl) |
24 | - | _ -> extract_structured_data accm tl) |
25 | + let extract_structured_data content = |
26 | + let of_codeblock kind content = |
27 | + match kind with |
28 | + | "json" -> [ content |> Ezjsonm.from_string ] |
29 | + | "yaml" -> [ Ezjsonm.wrap (content |> Yaml.of_string_exn) ] |
30 | + | _ -> [] |
31 | + in |
32 | + let get_data ~values doc = |
33 | + match doc with |
34 | + | Omd.Code_block (_, kind, content) -> of_codeblock kind content @ values |
35 | + | _ -> values |
36 | + in |
37 | + let doc = content |> Omd.of_string in |
38 | + doc |> List.concat_map ~f:(fun doc -> doc |> get_data ~values:[]) |
39 | |
40 | let to_json note = |
41 | - let data = |
42 | - note.content |> Omd.of_string |> extract_structured_data [] |
43 | - |> Ezjsonm.list (fun value -> value) |
44 | - in |
45 | Ezjsonm.dict |
46 | [ |
47 | ("frontmatter", Frontmatter.to_json note.frontmatter); |
48 | ("content", Ezjsonm.string note.content); |
49 | - ("data", data); |
50 | + ( "data", |
51 | + note.content |> extract_structured_data |> Ezjsonm.list (fun a -> a) ); |
52 | ] |
53 | |
54 | - let to_html note = |
55 | - note.content |> Omd.of_string |> Omd.to_html |
56 | + let to_html note = note.content |> Omd.of_string |> Omd.to_html |
57 | |
58 | let to_string note = |
59 | let yaml = Yaml.to_string_exn (Frontmatter.to_json note.frontmatter) in |
60 | @@ -129,12 +123,14 @@ module Tree = struct |
61 | |
62 | let note_to_json = to_json |
63 | |
64 | - let rec to_json tree = |
65 | + let rec to_json tree = |
66 | let (Tree (root, others)) = tree in |
67 | - Ezjsonm.dict [ |
68 | - ("note", (root |> note_to_json)) ; |
69 | - ("descendants", others |> List.map ~f:to_json |> Ezjsonm.list (fun a -> a)) |
70 | - ] |
71 | + Ezjsonm.dict |
72 | + [ |
73 | + ("note", root |> note_to_json); |
74 | + ( "descendants", |
75 | + others |> List.map ~f:to_json |> Ezjsonm.list (fun a -> a) ); |
76 | + ] |
77 | |
78 | let rec resolve_manifest ~path manifest = |
79 | match manifest |> Manifest.list ~path with |
80 | diff --git a/test/note_test.ml b/test/note_test.ml |
81 | index 6d25e88..3a9c76a 100644 |
82 | --- a/test/note_test.ml |
83 | +++ b/test/note_test.ml |
84 | @@ -29,14 +29,10 @@ let adapter () = |
85 | } |
86 | in |
87 | let tree = options |> Note.load ~path:"/" in |
88 | - Alcotest.(check int) |
89 | - "initialized" 1 |
90 | - (tree |> Note.Tree.flatten |> List.length); |
91 | + Alcotest.(check int) "initialized" 1 (tree |> Note.Tree.flatten |> List.length); |
92 | options |> Note.create ~content:(Some "bar") ~path:"/fuu"; |
93 | let tree = options |> Note.load ~path:"/" in |
94 | - Alcotest.(check int) |
95 | - "note added" 2 |
96 | - (tree |> Note.Tree.flatten |> List.length); |
97 | + Alcotest.(check int) "note added" 2 (tree |> Note.Tree.flatten |> List.length); |
98 | options |> Note.remove ~path:"/fuu"; |
99 | let tree = options |> Note.load ~path:"/" in |
100 | Alcotest.(check int) |
101 | @@ -76,6 +72,26 @@ let suggest_tags () = |
102 | let result = options |> Note.Completion.suggest_tags ~hint:"a" in |
103 | Alcotest.(check string) "tag aa" "aa" (List.nth_exn result 0) |
104 | |
105 | + let structured_data () = |
106 | + let note = Note.of_string {| |
107 | + # Some Data |
108 | + ```json |
109 | + {"a": "b"} |
110 | + ``` |
111 | + |} in |
112 | + let result = |
113 | + note |> Note.to_json |> Ezjsonm.wrap |> Ezjsonm.to_string |
114 | + |> Ezjsonm.from_string |
115 | + in |
116 | + let result = Ezjsonm.get_list (fun a -> a) result in |
117 | + let result = List.nth_exn result 0 in |
118 | + let result = |
119 | + Ezjsonm.find result [ "data" ] |> Ezjsonm.get_list (fun a -> a) |
120 | + in |
121 | + let result = List.nth_exn result 0 in |
122 | + let result = Ezjsonm.find result [ "a" ] |> Ezjsonm.get_string in |
123 | + Alcotest.(check string) "data" "b" result |
124 | + |
125 | let () = |
126 | Alcotest.run "Note" |
127 | [ |
128 | @@ -85,4 +101,6 @@ let () = |
129 | [ Alcotest.test_case "suggest path" `Quick suggest_path ] ); |
130 | ( "tag_suggestion", |
131 | [ Alcotest.test_case "suggest tags" `Quick suggest_tags ] ); |
132 | + ( "structured", |
133 | + [ Alcotest.test_case "structured data" `Quick structured_data ] ); |
134 | ] |