Function tpnote_lib::workflow::synchronize_filename
source · Expand description
Open the note file path on disk and read its YAML front matter.
Then calculate from the front matter how the filename should be to
be in sync. If it is different, rename the note on disk.
Returns the note’s new or existing filename.
Example with TemplateKind::SyncFilename
use tpnote_lib::content::ContentString;
use tpnote_lib::workflow::synchronize_filename;
use std::env::temp_dir;
use std::fs;
use std::path::Path;
// Prepare test: create existing note.
let raw = r#"
---
title: "My day"
subtitle: "Note"
---
Body text
"#;
let notefile = temp_dir().join("20221030-hello.md");
fs::write(¬efile, raw.as_bytes()).unwrap();
let expected = temp_dir().join("20221030-My day--Note.md");
let _ = fs::remove_file(&expected);
// Start test.
// You can plug in your own type (must impl. `Content`).
let n = synchronize_filename::<ContentString>(¬efile).unwrap();
// Check result
assert_eq!(n, expected);
assert!(n.is_file());