fix(feed): derive URLs from content paths
Replace hardcoded "/blog/{slug}.html" URL pattern with dynamic
path derivation using Content.output_path(). This ensures feed
URLs work correctly for any content location, not just blog posts.
This commit is contained in:
@@ -2,9 +2,10 @@
|
|||||||
|
|
||||||
use crate::config::SiteConfig;
|
use crate::config::SiteConfig;
|
||||||
use crate::content::Content;
|
use crate::content::Content;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
/// Generate an Atom 1.0 feed from blog posts.
|
/// Generate an Atom 1.0 feed from blog posts.
|
||||||
pub fn generate_atom_feed(posts: &[Content], config: &SiteConfig) -> String {
|
pub fn generate_atom_feed(posts: &[Content], config: &SiteConfig, content_root: &Path) -> String {
|
||||||
let base_url = config.base_url.trim_end_matches('/');
|
let base_url = config.base_url.trim_end_matches('/');
|
||||||
|
|
||||||
// Use the most recent post date as feed updated time, or fallback
|
// Use the most recent post date as feed updated time, or fallback
|
||||||
@@ -16,7 +17,9 @@ pub fn generate_atom_feed(posts: &[Content], config: &SiteConfig) -> String {
|
|||||||
|
|
||||||
let mut entries = String::new();
|
let mut entries = String::new();
|
||||||
for post in posts {
|
for post in posts {
|
||||||
let post_url = format!("{}/blog/{}.html", base_url, post.slug);
|
// Derive URL from output path (e.g., blog/foo.html → /blog/foo.html)
|
||||||
|
let relative_path = post.output_path(content_root);
|
||||||
|
let post_url = format!("{}/{}", base_url, relative_path.display());
|
||||||
let post_date = post
|
let post_date = post
|
||||||
.frontmatter
|
.frontmatter
|
||||||
.date
|
.date
|
||||||
|
|||||||
11
src/main.rs
11
src/main.rs
@@ -52,7 +52,7 @@ fn run() -> Result<()> {
|
|||||||
generate_blog_index(output_dir, &posts, &config, &nav)?;
|
generate_blog_index(output_dir, &posts, &config, &nav)?;
|
||||||
|
|
||||||
// 2b. Generate Atom feed
|
// 2b. Generate Atom feed
|
||||||
generate_feed(output_dir, &posts, &config)?;
|
generate_feed(output_dir, &posts, &config, content_dir)?;
|
||||||
|
|
||||||
// 3. Process standalone pages (discovered dynamically)
|
// 3. Process standalone pages (discovered dynamically)
|
||||||
process_pages(content_dir, output_dir, &config, &nav)?;
|
process_pages(content_dir, output_dir, &config, &nav)?;
|
||||||
@@ -135,11 +135,16 @@ fn generate_blog_index(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Generate the Atom feed
|
/// Generate the Atom feed
|
||||||
fn generate_feed(output_dir: &Path, posts: &[Content], config: &config::SiteConfig) -> Result<()> {
|
fn generate_feed(
|
||||||
|
output_dir: &Path,
|
||||||
|
posts: &[Content],
|
||||||
|
config: &config::SiteConfig,
|
||||||
|
content_dir: &Path,
|
||||||
|
) -> Result<()> {
|
||||||
let out_path = output_dir.join("feed.xml");
|
let out_path = output_dir.join("feed.xml");
|
||||||
eprintln!("generating: {}", out_path.display());
|
eprintln!("generating: {}", out_path.display());
|
||||||
|
|
||||||
let feed_xml = feed::generate_atom_feed(posts, config);
|
let feed_xml = feed::generate_atom_feed(posts, config, content_dir);
|
||||||
|
|
||||||
fs::write(&out_path, feed_xml).map_err(|e| Error::WriteFile {
|
fs::write(&out_path, feed_xml).map_err(|e| Error::WriteFile {
|
||||||
path: out_path.clone(),
|
path: out_path.clone(),
|
||||||
|
|||||||
Reference in New Issue
Block a user