Skip to content

Commit

Permalink
Prevent generating folder & index.html for a specific page
Browse files Browse the repository at this point in the history
  • Loading branch information
endesigner authored and Keats committed Jun 20, 2024
1 parent c5991fc commit cb2a4b0
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions components/content/src/front_matter/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down Expand Up @@ -151,6 +155,7 @@ impl Default for PageFrontMatter {
datetime: None,
datetime_tuple: None,
draft: false,
render: true,
slug: None,
path: None,
taxonomies: HashMap::new(),
Expand Down
8 changes: 5 additions & 3 deletions components/content/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions components/content/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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());
}
}
3 changes: 3 additions & 0 deletions components/content/src/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions components/site/src/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
7 changes: 5 additions & 2 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"));
Expand Down
3 changes: 3 additions & 0 deletions docs/content/documentation/content/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
8 changes: 8 additions & 0 deletions test_site/content/posts/render.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cb2a4b0

Please sign in to comment.