diff --git a/src/renderer.rs b/src/renderer.rs index 71b88db..925124c 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -26,7 +26,7 @@ pub enum RenderOutput { pub struct Frontmatter { layout: Option, title: Option, - published: bool + published: Option } impl Frontmatter { @@ -39,10 +39,10 @@ impl Frontmatter { } else { content }; - if self.published { - Ok(RenderOutput::Rendered(filename.with_extension("html"), wrapped)) - } else { + if let Some(false) = self.published { Ok(RenderOutput::Hidden(filename.with_extension("html"), wrapped)) + } else { + Ok(RenderOutput::Rendered(filename.with_extension("html"), wrapped)) } } } @@ -57,6 +57,19 @@ pub enum RenderError { FrontmatterParseError(toml::de::Error, PathBuf) } +/// Return the path for the default thing to render for the test server, which is either index.html +/// (if it exists) or index.md. Note that this does not guarantee that the file actually exists, it +/// just chooses that because those are the files which can become index.html in the output and +/// index.html is what a reasonable webserver will pick as the default file. +pub fn default_path(root: impl Into) -> PathBuf { + let root: PathBuf = root.into(); + if let Ok(true) = fs::exists(root.join("/index.html")) { + "index.html".into() + } else { + "index.md".into() + } +} + /// Take a source file path (relative to the root) and the root path, and return a RenderOutput for it pub fn render(source: PathBuf, root: PathBuf) -> Result { if skipped_path(source.clone()) { @@ -176,6 +189,9 @@ mod tests { let RenderOutput::Hidden(_, contents) = contents else { unreachable!() }; assert!(contents.starts_with("")); // Uses the layout assert!(contents.matches("This file should render to hidden").next().is_some()); + + let contents = render_file("not_hidden.md"); + assert!(matches!(contents, RenderOutput::Rendered(_, _))); // If we don't specify, it's published by default } #[test] diff --git a/testdata/not_hidden.md b/testdata/not_hidden.md new file mode 100644 index 0000000..9b5e784 --- /dev/null +++ b/testdata/not_hidden.md @@ -0,0 +1,4 @@ ++++ +layout = "_layouts/post.html" ++++ +I'm not hidden because the default for a missing 'published' key is that it's published. \ No newline at end of file