From 63ecdc5765aca7c781b934793ded9faaead8a2e4 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Fri, 23 Jan 2026 12:46:03 +0100 Subject: [PATCH] refactor: allow custom metadata in navigation templates (#291) Signed-off-by: squidfunk --- crates/zensical/src/structure/nav.rs | 10 ++-- crates/zensical/src/structure/nav/item.rs | 4 +- crates/zensical/src/structure/nav/meta.rs | 61 ----------------------- 3 files changed, 7 insertions(+), 68 deletions(-) delete mode 100644 crates/zensical/src/structure/nav/meta.rs diff --git a/crates/zensical/src/structure/nav.rs b/crates/zensical/src/structure/nav.rs index 464d474..a1304b5 100644 --- a/crates/zensical/src/structure/nav.rs +++ b/crates/zensical/src/structure/nav.rs @@ -25,6 +25,7 @@ //! Navigation. +use std::collections::BTreeMap; use std::hash::{DefaultHasher, Hash, Hasher}; use ahash::HashMap; @@ -40,7 +41,6 @@ use super::page::Page; mod item; mod iter; -mod meta; pub use item::NavigationItem; use iter::Iter; @@ -124,7 +124,7 @@ impl Navigation { } // Extract page metadata for selected keys - item.meta = Some(page.meta.clone().into()); + item.meta = page.meta.clone(); } } @@ -144,7 +144,7 @@ impl Navigation { title: Some(page.title.clone()), url: Some(page.url.clone()), canonical_url: page.canonical_url.clone(), - meta: Some(page.meta.clone().into()), + meta: page.meta.clone(), children: Vec::new(), is_index: true, active: false, @@ -334,7 +334,7 @@ impl From> for Navigation { title: Some(title), url: None, canonical_url: None, - meta: None, + meta: BTreeMap::default(), children: Vec::new(), is_index: false, active: false, @@ -351,7 +351,7 @@ impl From> for Navigation { title: Some(page.data.title), url: Some(page.data.url), canonical_url: page.data.canonical_url, - meta: Some(page.data.meta.into()), + meta: page.data.meta.clone(), children: Vec::new(), is_index: is_index(&file), active: false, diff --git a/crates/zensical/src/structure/nav/item.rs b/crates/zensical/src/structure/nav/item.rs index c1c898c..b9cf8d2 100644 --- a/crates/zensical/src/structure/nav/item.rs +++ b/crates/zensical/src/structure/nav/item.rs @@ -28,7 +28,7 @@ use pyo3::FromPyObject; use serde::Serialize; -use super::meta::NavigationMeta; +use crate::structure::page::PageMeta; // ---------------------------------------------------------------------------- // Structs @@ -45,7 +45,7 @@ pub struct NavigationItem { /// Item canonical URL. pub canonical_url: Option, /// Item metadata. - pub meta: Option, + pub meta: PageMeta, /// Item children. pub children: Vec, /// Whether this item is an index page. diff --git a/crates/zensical/src/structure/nav/meta.rs b/crates/zensical/src/structure/nav/meta.rs deleted file mode 100644 index c90039c..0000000 --- a/crates/zensical/src/structure/nav/meta.rs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2025-2026 Zensical and contributors - -// SPDX-License-Identifier: MIT -// All contributions are certified under the DCO - -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: - -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. - -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. - -// ---------------------------------------------------------------------------- - -//! Navigation item. - -use pyo3::FromPyObject; -use serde::Serialize; - -use crate::structure::page::PageMeta; - -// ---------------------------------------------------------------------------- -// Structs -// ---------------------------------------------------------------------------- - -/// Navigation item metadata. -#[derive(Clone, Debug, PartialEq, Hash, Eq, FromPyObject, Serialize)] -#[pyo3(from_item_all)] -pub struct NavigationMeta { - /// Page icon. - pub icon: Option, - /// Page status. - pub status: Option, -} - -// ---------------------------------------------------------------------------- -// Trait implementations -// ---------------------------------------------------------------------------- - -impl From for NavigationMeta { - /// Extract navigation metadata from a page. - fn from(meta: PageMeta) -> Self { - let icon = meta.get("icon").cloned(); - let status = meta.get("status").cloned(); - NavigationMeta { - icon: icon.map(|meta| meta.to_string()), - status: status.map(|meta| meta.to_string()), - } - } -}