recursive markdown rendering

This commit is contained in:
tristan 2023-12-31 10:33:05 +00:00
parent 5e8bb998db
commit 234c629e66
6 changed files with 90 additions and 15 deletions

View file

@ -20,10 +20,10 @@
"index.html" = with nixite.elems; (doc [ "index.html" = with nixite.elems; (doc [
[ [
(title "Nixite") (title "Nixite")
(nixite.html.tag "link" {rel = "shortcut icon"; type = "image/png"; href = ./testing/src/favicon.png;} "")
] ]
(main [ (main [
(link "/blog" "blog") (link "/blog" "blog")
(nixite.html.tag "img" {src = ./testing/src/favicon.png;} "")
(list [ (list [
"item 1" "item 1"
"item 2" "item 2"
@ -31,19 +31,7 @@
]) ])
]) ])
]); ]);
blog = { blog = nixite.md.readDir ./testing/blog;
"index.html" = with nixite.elems; (doc [
[
(title "A post")
]
(main [
(p ''
This is a post
'')
(link "/" "Home")
])
]);
};
}) })
); );

View file

@ -5,11 +5,46 @@ in rec {
if builtins.isPath md if builtins.isPath md
then processMd (builtins.readFile md) then processMd (builtins.readFile md)
else processMd md; else processMd md;
processMd = md: (map (c: processMd = md: (map (c:
if builtins.isString c if builtins.isString c
then mdBlock c then mdBlock c
else "") (builtins.split "\n\n" md)); else "") (builtins.split "\n\n" md));
recReadMd = root:
assert builtins.isPath root;
builtins.mapAttrs
(path: type:
if type == "directory"
then recReadMd (root + (/. + path))
else if type == "regular"
then mdToPage (root + (/. + path))
else throw "Cannot read ${path}, file type ${type}")
(builtins.readDir root);
recFixAppendix = site:
builtins.listToAttrs (
builtins.attrValues (
builtins.mapAttrs (name: value: {
name = fixAppendix name;
value =
if builtins.isAttrs value
then recFixAppendix value
else value;
})
site
)
);
fixAppendix = builtins.replaceStrings [".md"] [".html"];
readDir = root: recFixAppendix (recReadMd root);
mdToPage = md: elems.doc [
[(elems.title "markdown file")]
(readMd md)
];
mdBlock = block: let mdBlock = block: let
m = heading block; m = heading block;
h = h =

View file

@ -69,7 +69,7 @@ in rec {
mkdir -p ${prefix}/${name} mkdir -p ${prefix}/${name}
${copyTo "${prefix}/${name}" content} ${copyTo "${prefix}/${name}" content}
'' ''
else throw "Site page must be string, path or attrset" else throw "Site page must be string, path or attrset, but got ${builtins.typeOf content}: [${toString content}]"
) )
site site
) )

View file

@ -0,0 +1,3 @@
# this is a file
wowo

9
testing/blog/index.md Normal file
View file

@ -0,0 +1,9 @@
# yeee
ye
&
<a href="dir">dir</a>
<a href="/">home</a>

View file

@ -24,4 +24,44 @@ in
(elems.p {} "lorem ipsum\n") (elems.p {} "lorem ipsum\n")
]; ];
}) })
(it "can fix file appendixes" {
actual = fixAppendix "index.md";
expected = "index.html";
})
(it "recursively reads dir" {
actual = recReadMd ./blog;
expected = {
"index.md" = mdToPage ./blog/index.md;
"dir" = {
"index.md" = mdToPage ./blog/dir/index.md;
};
};
})
(it "recursively fixes filename" {
actual = recFixAppendix {
"index.md" = "something";
dir = {
"index.md" = "something else";
};
};
expected = {
"index.html" = "something";
dir = {
"index.html" = "something else";
};
};
})
(it "recursively translates md to html" {
actual = builtins.toJSON ( readDir ./blog );
expected = builtins.toJSON {
"index.html" = mdToPage ./blog/index.md;
"dir" = {
"index.html" = mdToPage ./blog/dir/index.md;
};
};
})
] ]