Commit
+26 -64 +/-1 browse
1 | diff --git a/lib/note.ml b/lib/note.ml |
2 | index 5b0896a..7ca26a1 100644 |
3 | --- a/lib/note.ml |
4 | +++ b/lib/note.ml |
5 | @@ -1,68 +1,39 @@ |
6 | open Core |
7 | |
8 | type t = { |
9 | - frontmatter : Ezjsonm.t option; |
10 | + frontmatter : Ezjsonm.t; |
11 | content : string; |
12 | markdown : Omd.doc; |
13 | slug : Slug.t; |
14 | + parent : Slug.t option; |
15 | } |
16 | |
17 | - let build ?(tags = []) ?(content = "") ~title slug = |
18 | + let build ?(description = "") ?(tags = []) ?(content = "") ~title slug = |
19 | let frontmatter = |
20 | - Some |
21 | - (Ezjsonm.dict |
22 | - [ ("title", Ezjsonm.string title); ("tags", Ezjsonm.strings tags) ]) |
23 | + Ezjsonm.dict |
24 | + [ |
25 | + ("title", Ezjsonm.string title); |
26 | + ("description", Ezjsonm.string description); |
27 | + ("tags", Ezjsonm.strings tags); |
28 | + ] |
29 | in |
30 | let markdown = Omd.of_string content in |
31 | - { frontmatter; content; markdown; slug } |
32 | - |
33 | - let rec title_of_markdown (blocks : Omd.block list) : string = |
34 | - match blocks with |
35 | - | [] -> "" |
36 | - | hd :: tl -> ( |
37 | - match hd.bl_desc with |
38 | - | Heading (_, content) -> ( |
39 | - match content.il_desc with |
40 | - | Text text -> text |
41 | - | _ -> title_of_markdown tl) |
42 | - | Paragraph content -> ( |
43 | - match content.il_desc with |
44 | - | Text text -> text |
45 | - | _ -> title_of_markdown tl) |
46 | - | _ -> "??") |
47 | + { frontmatter; content; markdown; slug; parent = None } |
48 | |
49 | let get_title t = |
50 | - let title = |
51 | - match t.frontmatter with |
52 | - | Some fm -> ( |
53 | - match Ezjsonm.find_opt (Ezjsonm.value fm) [ "title" ] with |
54 | - | Some v -> Some (Ezjsonm.get_string v) |
55 | - | None -> None) |
56 | - | None -> None |
57 | - in |
58 | - match title with |
59 | - | Some title -> title |
60 | - (* Since we couldn't determine the title from frontmatter now we will |
61 | - infer the title by looking at the markdown *) |
62 | - | None -> title_of_markdown t.markdown |
63 | + (* if title is specified use that, otherwise fall back to slug *) |
64 | + match Ezjsonm.find_opt (Ezjsonm.value t.frontmatter) [ "title" ] with |
65 | + | Some title -> Ezjsonm.get_string title |
66 | + | None -> Slug.to_string t.slug |
67 | |
68 | let get_description t = |
69 | - let description = |
70 | - match t.frontmatter with |
71 | - | Some fm -> ( |
72 | - match Ezjsonm.find_opt (Ezjsonm.value fm) [ "description" ] with |
73 | - | Some v -> Some (Ezjsonm.get_string v) |
74 | - | None -> None) |
75 | - | None -> None |
76 | - in |
77 | - match description with Some description -> description | None -> "" |
78 | + match Ezjsonm.find_opt (Ezjsonm.value t.frontmatter) [ "description" ] with |
79 | + | Some description -> Ezjsonm.get_string description |
80 | + | None -> "" |
81 | |
82 | let get_tags t = |
83 | - match t.frontmatter with |
84 | - | Some fm -> ( |
85 | - match Ezjsonm.find_opt (Ezjsonm.value fm) [ "tags" ] with |
86 | - | Some v -> Ezjsonm.get_strings v |
87 | - | None -> []) |
88 | + match Ezjsonm.find_opt (Ezjsonm.value t.frontmatter) [ "tags" ] with |
89 | + | Some tags -> Ezjsonm.get_strings tags |
90 | | None -> [] |
91 | |
92 | let get_path t = Slug.get_path t.slug |
93 | @@ -89,25 +60,16 @@ let get_data t = |
94 | Ezjsonm.list (fun value -> value) data |
95 | |
96 | let to_json t = |
97 | - let frontmatter = |
98 | - match t.frontmatter with |
99 | - | Some fm -> Ezjsonm.value fm |
100 | - | None -> Ezjsonm.unit () |
101 | - in |
102 | Ezjsonm.dict |
103 | [ |
104 | - ("frontmatter", frontmatter); |
105 | + ("frontmatter", Ezjsonm.value t.frontmatter); |
106 | ("content", Ezjsonm.string t.content); |
107 | ("data", get_data t); |
108 | ] |
109 | |
110 | let to_string t = |
111 | - match t.frontmatter with |
112 | - | Some fm -> |
113 | - let front_matter = Yaml.to_string_exn (Ezjsonm.value fm) in |
114 | - String.concat ~sep:"\n" |
115 | - [ "---"; front_matter; "---"; Omd.to_html t.markdown ] |
116 | - | None -> Omd.to_html t.markdown |
117 | + let yaml = Yaml.to_string_exn (Ezjsonm.value t.frontmatter) in |
118 | + "\n---" ^ yaml ^ "\n---\n" ^ t.content |
119 | |
120 | let of_string ~content slug = |
121 | let indexes = |
122 | @@ -132,12 +94,11 @@ let of_string ~content slug = |
123 | (List.nth_exn indexes 1 + 3) |
124 | (String.length content)) |
125 | in |
126 | - let frontmatter = Some frontmatter in |
127 | - { frontmatter; content; markdown; slug } |
128 | + { frontmatter; content; markdown; slug; parent = None } |
129 | else |
130 | - let frontmatter = None in |
131 | + let frontmatter = Ezjsonm.dict [] in |
132 | let markdown = Omd.of_string content in |
133 | - { frontmatter; content; markdown; slug } |
134 | + { frontmatter; content; markdown; slug; parent = None } |
135 | |
136 | module Util = struct |
137 | let split_words str = |
138 | @@ -155,6 +116,7 @@ module Util = struct |
139 | match doc with |
140 | | [] -> accm |
141 | | hd :: tl -> ( |
142 | + (* TODO: account for headings, lists, etc *) |
143 | match hd.bl_desc with |
144 | | Paragraph inline -> |
145 | let accm = accm @ split_words inline.il_desc in |