Use the deep path as the post id.

This commit is contained in:
Tom Alexander 2024-10-19 16:55:38 -04:00
parent 8ab69e480e
commit 379850fe3d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 17 additions and 9 deletions

View File

@ -103,10 +103,15 @@ async fn get_post_directories(config: &Config) -> Result<Vec<PathBuf>, CustomErr
async fn load_blog_posts(config: &Config) -> Result<Vec<BlogPost>, CustomError> {
let root_directory = config.get_root_directory().to_owned();
let posts_directory = config.get_posts_directory();
let post_directories = get_post_directories(config).await?;
let load_jobs = post_directories
.into_iter()
.map(|path| tokio::spawn(BlogPost::load_blog_post(root_directory.clone(), path)));
let load_jobs = post_directories.into_iter().map(|path| {
tokio::spawn(BlogPost::load_blog_post(
root_directory.clone(),
posts_directory.clone(),
path,
))
});
let mut blog_posts = Vec::new();
for job in load_jobs {
blog_posts.push(job.await??);

View File

@ -20,14 +20,17 @@ pub(crate) struct BlogPost {
}
impl BlogPost {
pub(crate) async fn load_blog_post<P: AsRef<Path>, R: AsRef<Path>>(
pub(crate) async fn load_blog_post<P: AsRef<Path>, R: AsRef<Path>, S: AsRef<Path>>(
root_dir: R,
posts_dir: S,
post_dir: P,
) -> Result<BlogPost, CustomError> {
async fn inner(_root_dir: &Path, post_dir: &Path) -> Result<BlogPost, CustomError> {
let post_id = post_dir
.file_name()
.expect("The post directory should have a name.");
async fn inner(
_root_dir: &Path,
posts_dir: &Path,
post_dir: &Path,
) -> Result<BlogPost, CustomError> {
let post_id = post_dir.strip_prefix(posts_dir)?.as_os_str();
let org_files = {
let mut ret = Vec::new();
@ -80,7 +83,7 @@ impl BlogPost {
pages,
})
}
inner(root_dir.as_ref(), post_dir.as_ref()).await
inner(root_dir.as_ref(), posts_dir.as_ref(), post_dir.as_ref()).await
}
/// Get the date for a blog post.