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] paragraphs
- [ ] tables
- [ ] lists
- [X] lists
- [ ] checkboxes
- [ ] images
- [ ] links

View file

@ -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; };
}

View file

@ -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