From fafff120f955243db300615d4425a4c8fa612b49 Mon Sep 17 00:00:00 2001 From: tristan Date: Mon, 1 Jan 2024 14:06:54 +0000 Subject: [PATCH] add md code block support (basic) --- README.md | 2 +- nixite/md.nix | 21 +++++++++++++++++---- testing/md.test.nix | 18 ++++++++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b8c512c..1f073b2 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ nix build .#raw - [ ] checkboxes - [ ] images - [ ] links -- [ ] codeblocks +- [X] codeblocks - [ ] subscript - [ ] highlight - [ ] italics diff --git a/nixite/md.nix b/nixite/md.nix index 415f215..e093e18 100644 --- a/nixite/md.nix +++ b/nixite/md.nix @@ -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); }; } diff --git a/testing/md.test.nix b/testing/md.test.nix index adb072f..66eb6bc 100644 --- a/testing/md.test.nix +++ b/testing/md.test.nix @@ -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