From 4f28cbfda72a6f123d3bbc01fe9db061e9003715 Mon Sep 17 00:00:00 2001 From: tristan Date: Mon, 1 Jan 2024 20:27:05 +0000 Subject: [PATCH] add checkboxes --- README.md | 16 +++++++++------ nixite/html.nix | 3 ++- nixite/md.nix | 24 ++++++++++++++++------ testing/html.test.nix | 5 +++++ testing/md.test.nix | 47 +++++++++++++++++++++++-------------------- 5 files changed, 60 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 4a959c7..ebc8974 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ nix build .#raw - [X] global styles - [X] custom components with styles - [X] component extend / inheritance +- [ ] templating ### markdown @@ -46,12 +47,15 @@ nix build .#raw - [X] paragraphs - [ ] tables - [X] lists -- [ ] checkboxes +- [ ] embedded lists +- [X] checkboxes - [ ] images - [ ] links -- [X] codeblocks -- [ ] subscript -- [X] highlight -- [X] italics -- [X] bold +- [X] `codeblocks` +- [X] ~superscript~ +- [X] ^subscript^ +- [X] ==highlight== +- [X] *italics* +- [X] **bold** +- [X] ~~strikethrough~~ diff --git a/nixite/html.nix b/nixite/html.nix index 0f4a025..9534c4c 100644 --- a/nixite/html.nix +++ b/nixite/html.nix @@ -3,8 +3,9 @@ let assert builtins.isString key; if builtins.isAttrs value then builtins.trace "Skipping ${key} as it is a set" "" - else if value == "" || value == [ ] then + else if value == "" || value == [ ] || value == false then "" + else if value == true then key else ''${key}="${toString value}"''; in rec { diff --git a/nixite/md.nix b/nixite/md.nix index 818021f..a197ea7 100644 --- a/nixite/md.nix +++ b/nixite/md.nix @@ -69,13 +69,25 @@ in rec { matchThen "(```)(.*)(```)" block (m: elems.code (builtins.elemAt m 1)); list = block: - matchThen (let item = "- .+"; - in '' - (${item} - )*(${item} + matchThen ( + '' + (- .+ + )*(- .+ ?)'') block (m: - elems.List (builtins.filter (s: builtins.isString s && s != "") - (builtins.split "[:blank:]*- " block))); + elems.List ( splitList block )); + + splitList = block: map (listItem) (builtins.filter + (s: builtins.isString s && s != "") + (builtins.split "\n" block)); + + listItem = str: + let + li = builtins.match "- (.*)" str; + checkbox = builtins.match "- \\[(.)] (.*)" str; + checked = builtins.elemAt checkbox 0; + content = builtins.elemAt checkbox 1; + in if checkbox == null then li else + [(elems.input {type = "checkbox"; checked = checked != " "; disabled = true;}) content]; replace = matcher: apply: block: (let diff --git a/testing/html.test.nix b/testing/html.test.nix index fe0297b..b291e24 100644 --- a/testing/html.test.nix +++ b/testing/html.test.nix @@ -132,4 +132,9 @@ in with html; [ removeDunders = true; })) + (it "renders on / off attrs" ({ + actual = toString (tag "p" {on = true; off = false;}); + expected = "

"; + })) + ] diff --git a/testing/md.test.nix b/testing/md.test.nix index f16979f..c6d53c6 100644 --- a/testing/md.test.nix +++ b/testing/md.test.nix @@ -34,13 +34,12 @@ in with md; [ ''; expected = { matched = true; - block = elems.List ['' - something - '']; + block = elems.List ["something"]; }; + asJSON = true; })) - (it "matches a list of many elements" ({ + (it "makes a list of many elements" ({ actual = list '' - something - something else @@ -48,14 +47,28 @@ in with md; [ expected = { matched = true; block = elems.List [ - '' - something - '' - '' - something else - '' + "something" + "something else" ]; }; + asJSON = true; + })) + + (it "makes a list of many checkboxes" ({ + actual = list '' + - [ ] something + - [X] something else + ''; + expected = { + matched = true; + block = elems.List [ + [(elems.input {type = "checkbox"; disabled = true; checked = false; } "") + "something"] + [(elems.input {type = "checkbox"; disabled = true; checked = true; } "") + "something else"] + ]; + }; + asJSON = true; })) (it "matches a list with no whitespace around" ({ @@ -65,12 +78,11 @@ in with md; [ expected = { matched = true; block = elems.List [ - '' - something - '' + "something" "something else" ]; }; + asJSON = true; })) (it "doesnt match not a list" (let str = "blah blah"; @@ -82,15 +94,6 @@ in with md; [ }; })) - (it "makes a list" ({ - actual = mdBlock '' - - something - ''; - expected = elems.List ['' - something - '']; - })) - (it "finds surrounded parts" ({ actual = replace (wrap "\\*\\*") elems.strong '' this text **may** contain **bold** words inside it.