add md lists support

This commit is contained in:
tristan 2024-01-01 14:51:59 +00:00
parent fafff120f9
commit 496d3667de
3 changed files with 64 additions and 12 deletions

View file

@ -45,7 +45,7 @@ nix build .#raw
- [X] headers - [X] headers
- [X] paragraphs - [X] paragraphs
- [ ] tables - [ ] tables
- [ ] lists - [X] lists
- [ ] checkboxes - [ ] checkboxes
- [ ] images - [ ] images
- [ ] links - [ ] links

View file

@ -45,23 +45,33 @@ in rec {
elems.Doc { } [ [ (elems.title { } "markdown file") ] (readMd md) ]; elems.Doc { } [ [ (elems.title { } "markdown file") ] (readMd md) ];
mdBlock = block: mdBlock = block:
let (let
h = heading block; h = heading block;
c = code block; c = code block;
ul = list block;
in (if h.matched then h.block else in (if h.matched then h.block else
if c.matched then c.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 heading = block: matchThen "(#+) (.*)" block (m:
m = builtins.match "(#+) (.*)" block; let
l = builtins.stringLength (builtins.elemAt m 0); l = builtins.stringLength (builtins.elemAt m 0);
in in H l (builtins.elemAt m 1));
if m == null then { matched = false; inherit block; }
else { matched = true; block = H l (builtins.elemAt m 1); };
code = block: let code = block: matchThen "(```)(.*)(```)" block (m:
m = builtins.match "(```)(.*)(```)" block; 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 in
if m == null then { matched = false; inherit block; } if m == null then dontMatch block
else { matched = true; block = elems.code (builtins.elemAt m 1); }; else { matched = true; block = func m; };
dontMatch = block: { matched = false; inherit block; };
} }

View file

@ -28,6 +28,48 @@ in with md; [
expected = elems.code code; 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" { (it "processes md block" {
actual = readMd '' actual = readMd ''
# foo bar # foo bar