diff --git a/CHANGELOG.md b/CHANGELOG.md index bde1892e..bf5a5254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Fix resizing for images with EXIF orientation - Add MIME type to get_image_metadata - Fix hot loading for config.toml in some cases +- Add `render = false` capability to pages ## 0.18.0 (2023-12-18) diff --git a/components/content/src/front_matter/page.rs b/components/content/src/front_matter/page.rs index 4d2ed5eb..b4bacd30 100644 --- a/components/content/src/front_matter/page.rs +++ b/components/content/src/front_matter/page.rs @@ -39,6 +39,10 @@ pub struct PageFrontMatter { pub datetime_tuple: Option<(i32, u8, u8)>, /// Whether this page is a draft pub draft: bool, + /// Prevent generation of a folder for current page + /// Defaults to `true` + #[serde(skip_serializing)] + pub render: bool, /// The page slug. Will be used instead of the filename if present /// Can't be an empty string if present pub slug: Option, @@ -151,6 +155,7 @@ impl Default for PageFrontMatter { datetime: None, datetime_tuple: None, draft: false, + render: true, slug: None, path: None, taxonomies: HashMap::new(), diff --git a/components/content/src/library.rs b/components/content/src/library.rs index 2ab6fdbf..c2adb941 100644 --- a/components/content/src/library.rs +++ b/components/content/src/library.rs @@ -82,9 +82,11 @@ impl Library { pub fn insert_page(&mut self, page: Page) { let file_path = page.file.path.clone(); - let mut entries = vec![page.path.clone()]; - entries.extend(page.meta.aliases.to_vec()); - self.insert_reverse_aliases(&file_path, entries); + if page.meta.render { + let mut entries = vec![page.path.clone()]; + entries.extend(page.meta.aliases.to_vec()); + self.insert_reverse_aliases(&file_path, entries); + } for (taxa_name, terms) in &page.meta.taxonomies { for term in terms { diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 94672b2b..e0ea0f9b 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -115,6 +115,10 @@ impl Page { page.word_count = Some(word_count); page.reading_time = Some(reading_time); + if !page.meta.render { + return Ok(page); + } + let mut slug_from_dated_filename = None; let file_path_for_slug = if page.file.name == "index" { @@ -923,4 +927,25 @@ Bonjour le monde"# assert_eq!(page.slug, "hello"); assert_eq!(page.permalink, "http://a-website.com/bonjour/"); } + + #[test] + fn page_without_physical_path() { + let config = Config::default(); + let content = r#" ++++ +render = false ++++ +Hello world +"# + .to_string(); + + let res = Page::parse(Path::new("hello.md"), &content, &config, &PathBuf::new()); + assert!(res.is_ok()); + let page = res.unwrap(); + println!("{:#?}", page); + assert_eq!(page.slug, ""); + assert_eq!(page.path, ""); + assert_eq!(page.permalink, ""); + assert_eq!(page.raw_content, "Hello world\n".to_string()); + } } diff --git a/components/content/src/pagination.rs b/components/content/src/pagination.rs index 88a09462..c620bb3a 100644 --- a/components/content/src/pagination.rs +++ b/components/content/src/pagination.rs @@ -130,6 +130,9 @@ impl<'a> Paginator<'a> { for p in &*self.all_pages { let page = &library.pages[p]; + if !page.meta.render { + continue; + } current_page.push(SerializingPage::new(page, Some(library), false)); if current_page.len() == self.paginate_by { diff --git a/components/site/src/sitemap.rs b/components/site/src/sitemap.rs index 0f071767..2d996074 100644 --- a/components/site/src/sitemap.rs +++ b/components/site/src/sitemap.rs @@ -64,6 +64,9 @@ pub fn find_entries<'a>( let mut entries = HashSet::new(); for p in library.pages.values() { + if !p.meta.render { + continue; + } let mut entry = SitemapEntry::new( Cow::Borrowed(&p.permalink), if p.meta.updated.is_some() { &p.meta.updated } else { &p.meta.date }, diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index baca868e..ec8694a1 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -21,7 +21,7 @@ fn can_parse_site() { let library = site.library.read().unwrap(); // Correct number of pages (sections do not count as pages, draft are ignored) - assert_eq!(library.pages.len(), 35); + assert_eq!(library.pages.len(), 36); let posts_path = path.join("content").join("posts"); // Make sure the page with a url doesn't have any sections @@ -44,7 +44,7 @@ fn can_parse_site() { let posts_section = library.sections.get(&posts_path.join("_index.md")).unwrap(); assert_eq!(posts_section.subsections.len(), 2); - assert_eq!(posts_section.pages.len(), 10); // 11 with 1 draft == 10 + assert_eq!(posts_section.pages.len(), 11); // 12 with 1 draft == 11 assert_eq!(posts_section.ancestors, vec![index_section.file.relative.clone()]); // Make sure we remove all the pwd + content from the sections @@ -136,6 +136,9 @@ fn can_build_site_without_live_reload() { assert!(file_exists!(public, "posts/with-assets/index.html")); assert!(file_exists!(public, "posts/no-section/simple/index.html")); + // "render = false" should not generate an index.html + assert!(!file_exists!(public, "posts/render/index.html")); + // Sections assert!(file_exists!(public, "posts/index.html")); assert!(file_exists!(public, "posts/tutorials/index.html")); diff --git a/docs/content/documentation/content/page.md b/docs/content/documentation/content/page.md index 98197808..30f2af58 100644 --- a/docs/content/documentation/content/page.md +++ b/docs/content/documentation/content/page.md @@ -112,6 +112,9 @@ weight = 0 # A draft page is only loaded if the `--drafts` flag is passed to `zola build`, `zola serve` or `zola check`. draft = false +# When set to "false" Zola will not create a separate folder with index.html inside for this page. +render = false + # If set, this slug will be used instead of the filename to make the URL. # The section path will still be used. slug = "" diff --git a/test_site/content/posts/render.md b/test_site/content/posts/render.md new file mode 100644 index 00000000..63af6b75 --- /dev/null +++ b/test_site/content/posts/render.md @@ -0,0 +1,8 @@ ++++ +title = "Page with content but without generated folder" +description = "" +date = 2017-04-01 +render = false ++++ + +Don't generate a folder for this page