-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
355674f
commit 0303312
Showing
4 changed files
with
112 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
let is_text_empty text = String.(equal empty (trim text)) | ||
let text_or_none text = | ||
if is_text_empty text then None else Some text | ||
|
||
let title_of_doc doc = | ||
let open Omd in | ||
let rec finder = function | ||
| Text (_, s) | Code (_, s) -> text_or_none s | ||
| Link (_, link) -> finder link.label | ||
| Strong (_, inline) | Emph (_, inline) -> finder inline | ||
| Concat (_, xs) -> | ||
List.filter_map finder xs | ||
|> String.concat "" | ||
|> text_or_none | ||
| _ -> None | ||
in | ||
Omd_ext.inline_find_map ~concat:false ~f:finder doc | ||
|
||
(* split a string, extracting a list of Omd.Link and Omd.Text *) | ||
let split_links attr (tag, r) text = | ||
Str.full_split r text | ||
|> List.map (function | ||
| Str.Delim (s) -> | ||
let label = Omd.Text (attr, s) in | ||
let title = Some ("Search " ^ tag ^ " " ^ s) in | ||
Omd.Link ( | ||
[("class", "issue-" ^ tag)], | ||
{ Omd.title; label; destination = "#" }) | ||
| Str.Text (s) -> Omd.Text (attr, s)) | ||
|
||
let mention_regexp = Str.regexp {|@\([A-Za-z0-9]+\)|} | ||
let hashtag_regexp = Str.regexp {|#\([A-Za-z0-9]+\)|} | ||
|
||
let extract_links attr text = | ||
split_links attr ("mention", mention_regexp) text | ||
|> List.concat_map ( | ||
function | ||
| Omd.Text (attr, s) -> split_links attr ("hashtag", hashtag_regexp) s | ||
| x -> [x]) | ||
|
||
let replace_text_with_links = Omd_ext.inline_map ~concat:true ~f:(function | ||
| Omd.Text (attr, s) as t -> | ||
let links = extract_links attr s in | ||
if List.is_empty links | ||
then t | ||
else Omd.Concat (attr, links) | ||
| x -> x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
val title_of_doc : 'a Omd.block list -> string option | ||
|
||
val extract_links | ||
: (string * string) list | ||
-> string | ||
-> (string * string) list Omd.inline list | ||
|
||
val replace_text_with_links | ||
: (string * string) list Omd.block list | ||
-> (string * string) list Omd.block list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
open Issue_lib;; | ||
|
||
let%test_unit "title_of_doc various inlines" = | ||
let tester test = | ||
Printf.eprintf "\twith %s\n" test; | ||
assert ( | ||
Omd.of_string test | ||
|> Omd_util.title_of_doc | ||
|> Option.fold ~none:false ~some:(String.equal "title")) | ||
in | ||
List.iter tester | ||
[ {|title|} | ||
; {| title |} | ||
; {|# title|} | ||
; {|[title](https://example.com)|} | ||
; {|`title`|} | ||
; {|_title_|} | ||
; {|__title__|} | ||
; {|[_title_](https://example.com)|} | ||
; {|[__title__](https://example.com)|} | ||
; {|[`title`](https://example.com)|} | ||
] | ||
|
||
let%test "title_of_doc handles empty links" = | ||
Omd.of_string {|[](https://example.com)|} | ||
|> Omd_util.title_of_doc | ||
|> Option.is_none | ||
|
||
let%test "title_of_doc handles empty headings" = | ||
Omd.of_string {|# |} | ||
|> Omd_util.title_of_doc | ||
|> Option.is_none | ||
|
||
let%test "title_of_doc handles empty code" = | ||
Omd.of_string {|``|} | ||
|> Omd_util.title_of_doc | ||
(* will return ``, i'm ok with this case since the markdown parser | ||
seems to think its just plain text. *) | ||
|> Option.fold ~none:false ~some:(String.equal "``") | ||
|
||
let%test "extract_links extracts @ from text" = | ||
match Omd_util.extract_links [] "hello @joe" with | ||
| [Omd.Text (_, text); Omd.Link (_, { label = Omd.Text (_, link); _ })] -> | ||
String.equal "hello " text | ||
&& String.equal "@joe" link | ||
| _ -> false | ||
|
||
let%test "extract_links extracts # from text" = | ||
match Omd_util.extract_links [] "hello #mike" with | ||
| [Omd.Text (_, text); Omd.Link (_, { label = Omd.Text (_, link); _ })] -> | ||
String.equal "hello " text | ||
&& String.equal "#mike" link | ||
| _ -> false |