added sitemap header

This commit is contained in:
Daniel Fichtinger 2025-07-02 15:01:13 -04:00
parent 0e3e9d243d
commit 89e33e92c4
6 changed files with 27 additions and 7 deletions

View file

@ -89,7 +89,9 @@ class ZonaBuilder:
reverse=True,
)
templater = Templater(
template_dir=self.layout.templates, post_list=post_list
config=self.config,
template_dir=self.layout.templates,
post_list=post_list,
)
self.item_map = {
item.source.resolve(): item for item in self.items

View file

@ -14,6 +14,9 @@ def find_config(start: Path | None = None) -> Path | None:
return None
SitemapConfig = dict[str, str]
@dataclass
class BlogConfig:
dir: str = "blog"
@ -53,6 +56,8 @@ class ZonaConfig:
title: str = "Zona Blog"
base_url: str = "https://example.com"
language: str = "en"
# dictionary where key is name, value is url
sitemap: SitemapConfig = field(default_factory=lambda: {"Home": "/"})
# list of globs relative to content that should be ignored
ignore: list[str] = field(default_factory=lambda: IGNORELIST)
markdown: MarkdownConfig = field(default_factory=MarkdownConfig)

View file

@ -1 +1 @@
The footer content.
My Markdown footer!

View file

@ -0,0 +1,5 @@
<ul>
{% for name, url in site_map.items() %}
<li><a href="{{ url }}">{{ name }}</a></li>
{% endfor %}
</ul>

View file

@ -1,2 +0,0 @@
- One
- Two

View file

@ -1,6 +1,7 @@
from pathlib import Path
from typing import Literal
from jinja2 import Environment, FileSystemLoader, select_autoescape
from zona.config import ZonaConfig
from zona.models import Item
from zona.markdown import md_to_html
@ -26,16 +27,25 @@ def get_footer(template_dir: Path) -> str | None:
# TODO: add next/prev post button logic to posts
# TODO: add a recent posts element that can be included elsewhere?
class Templater:
def __init__(self, template_dir: Path, post_list: list[Item]):
def __init__(
self,
config: ZonaConfig,
template_dir: Path,
post_list: list[Item],
):
self.env: Environment = Environment(
loader=FileSystemLoader(template_dir),
autoescape=select_autoescape(["html", "xml"]),
)
self.config: ZonaConfig = config
self.template_dir: Path = template_dir
self.header: str | None = get_header(template_dir)
self.footer: str | None = get_footer(template_dir)
self.post_list: list[Item] = post_list
def render_header(self):
template = self.env.get_template("header.html")
return template.render(site_map=self.config.sitemap)
def render_item(self, item: Item, content: str) -> str:
env = self.env
meta = item.metadata
@ -46,7 +56,7 @@ class Templater:
else meta.template + ".html"
)
header: str | Literal[False] = (
self.header if self.header and meta.header else False
self.render_header() if meta.header else False
)
footer: str | Literal[False] = (
self.footer if self.footer and meta.footer else False