add md code block support (basic)

This commit is contained in:
tristan 2024-01-01 14:06:54 +00:00
parent 3ceb580768
commit fafff120f9
3 changed files with 30 additions and 11 deletions

View file

@ -49,7 +49,7 @@ nix build .#raw
- [ ] checkboxes
- [ ] images
- [ ] links
- [ ] codeblocks
- [X] codeblocks
- [ ] subscript
- [ ] highlight
- [ ] italics

View file

@ -46,9 +46,22 @@ in rec {
mdBlock = block:
let
m = heading block;
h = if m == null then 0 else builtins.stringLength (builtins.elemAt m 0);
in if m == null then elems.p { } block else H h (builtins.elemAt m 1);
h = heading block;
c = code block;
in (if h.matched then h.block else
if c.matched then c.block else
elems.p block);
heading = block: builtins.match "(#+) (.*)" block;
heading = block: let
m = builtins.match "(#+) (.*)" block;
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); };
code = block: let
m = builtins.match "(```)(.*)(```)" block;
in
if m == null then { matched = false; inherit block; }
else { matched = true; block = elems.code (builtins.elemAt m 1); };
}

View file

@ -5,23 +5,29 @@ let
in with md; [
(it "gets md heading" {
actual = mdBlock ''
# title of the page'';
actual = mdBlock "# title of the page";
expected = elems.h1 "title of the page";
})
(it "gets md heading 2" {
actual = mdBlock ''
## a subheading'';
actual = mdBlock "## a subheading";
expected = elems.h2 "a subheading";
})
(it "limits to 6 #" {
actual = mdBlock ''
######## super ultra tiny heading'';
actual = mdBlock "######## super ultra tiny heading";
expected = elems.h6 "super ultra tiny heading";
})
(it "makes a code block" (
let code = ''
this is my code
''; in {
actual = mdBlock ''
```${code}```'';
expected = elems.code code;
}))
(it "processes md block" {
actual = readMd ''
# foo bar