Skip to content

Commit

Permalink
Add option to add publish date as prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitozgumus committed May 19, 2024
1 parent a3ad139 commit 7b1632a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Config {
pub sanitize_frontmatter: bool,
pub auto_add_cover_img: bool,
pub remove_draft_on_stage: bool,
pub add_date_prefix: bool,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -36,6 +37,7 @@ impl fmt::Display for Config {
writeln!(f, " yaml_asset_prefix: {}", self.yaml_asset_prefix)?;
writeln!(f, " sanitize_frontmatter: {}", self.sanitize_frontmatter)?;
writeln!(f, " auto_add_cover_img: {}", self.auto_add_cover_img)?;
writeln!(f, " add_date_prefix: {}", self.add_date_prefix)?;
writeln!(
f,
" remove_draft_on_stage: {}",
Expand Down Expand Up @@ -93,6 +95,7 @@ pub fn validate_config() -> ConfigResult<Config> {
sanitize_frontmatter: false,
auto_add_cover_img: false,
remove_draft_on_stage: false,
add_date_prefix: false
};

// Serialize the updated JSON structure
Expand Down
16 changes: 14 additions & 2 deletions src/writing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
io::{self, Write},
path::{Path, PathBuf},
};
use std::fmt::format;

use crate::{
asset::Asset,
Expand Down Expand Up @@ -110,9 +111,20 @@ pub fn update_writing_content_and_transfer(
caps.get(0).unwrap().as_str().to_string()
}
});
let writing_name = Path::new(&writing.path)

let mut writing_name = Path::new(&writing.path)
.file_name()
.expect("Could not parse writing name");
.expect("Could not parse writing name")
.to_str()
.expect("Parsed writing name shouldn't be empty").to_string();

let publish_date = frontmatter["publishDate"].as_str().unwrap_or("");

if config.add_date_prefix && !publish_date.is_empty() {
let concatenation = format!("{}-{}", &publish_date, &writing_name).to_string();
writing_name = concatenation;
}

let target_file_name = Path::new(&config.target_dir).join(writing_name);
let merged_content = format!(
"---\n{}\n{}",
Expand Down

0 comments on commit 7b1632a

Please sign in to comment.