diff --git a/README.md b/README.md index 1f073b2..ead6413 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ nix build .#raw - [X] headers - [X] paragraphs - [ ] tables -- [ ] lists +- [X] lists - [ ] checkboxes - [ ] images - [ ] links diff --git a/nixite/md.nix b/nixite/md.nix index e093e18..8278cc2 100644 --- a/nixite/md.nix +++ b/nixite/md.nix @@ -45,23 +45,33 @@ in rec { elems.Doc { } [ [ (elems.title { } "markdown file") ] (readMd md) ]; mdBlock = block: - let + (let h = heading block; c = code block; + ul = list block; in (if h.matched then h.block else if c.matched then c.block else - elems.p block); + if ul.matched then ul.block else + elems.p block)); - heading = block: let - m = builtins.match "(#+) (.*)" block; + heading = block: matchThen "(#+) (.*)" block (m: + let l = builtins.stringLength (builtins.elemAt m 0); - in - if m == null then { matched = false; inherit block; } - else { matched = true; block = H l (builtins.elemAt m 1); }; + in H l (builtins.elemAt m 1)); - code = block: let - m = builtins.match "(```)(.*)(```)" block; + code = block: matchThen "(```)(.*)(```)" block (m: + elems.code (builtins.elemAt m 1)); + + list = block: matchThen (let item = "- .+"; in "(${item}\n)*(${item}\n?)") block (m: + elems.List (builtins.filter ( s: builtins.isString s && s!="" ) (builtins.split "[:blank:]*- " block))); + + matchThen = matcher: block: func: + let + m = builtins.match matcher block; in - if m == null then { matched = false; inherit block; } - else { matched = true; block = elems.code (builtins.elemAt m 1); }; + if m == null then dontMatch block + else { matched = true; block = func m; }; + + dontMatch = block: { matched = false; inherit block; }; + } diff --git a/testing/md.test.nix b/testing/md.test.nix index 66eb6bc..6086f03 100644 --- a/testing/md.test.nix +++ b/testing/md.test.nix @@ -28,6 +28,48 @@ in with md; [ expected = elems.code code; })) + (it "matches a list of one element" ( + { + actual = list '' + - something + ''; + expected = { matched = true; block = elems.List ["something\n"];}; + })) + + (it "matches a list of many elements" ( + { + actual = list '' + - something + - something else + ''; + expected = { matched = true; block = elems.List ["something\n" "something else\n"];}; + })) + + (it "matches a list with no whitespace around" ( + { + actual = list + "- something\n- something else"; + expected = { matched = true; block = elems.List ["something\n" "something else"];}; + })) + + + (it "doesnt match not a list" ( + let + str = "blah blah"; + in + { + actual = list str; + expected = { matched = false; block = str;}; + })) + + (it "makes a list" ( + { + actual = mdBlock '' + - something + ''; + expected = elems.List ["something\n"]; + })) + (it "processes md block" { actual = readMd '' # foo bar