Sync removal too;
This commit is contained in:
parent
c92bdb8e17
commit
7f7cf3a436
37
src/main.rs
37
src/main.rs
@ -72,6 +72,11 @@ impl Context {
|
|||||||
self.tags = Some(tags);
|
self.tags = Some(tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sanitize_title(title: &str) -> String {
|
||||||
|
// TODO do a better sanitize of the title.
|
||||||
|
title.replace(" ", "_")
|
||||||
|
}
|
||||||
|
|
||||||
fn publish_public(&self, notes: &NotesMap) -> Result<(), io::Error> {
|
fn publish_public(&self, notes: &NotesMap) -> Result<(), io::Error> {
|
||||||
let public_tag_id = if let Some(id) = &self.public_tag_id {
|
let public_tag_id = if let Some(id) = &self.public_tag_id {
|
||||||
id
|
id
|
||||||
@ -117,8 +122,7 @@ impl Context {
|
|||||||
+ &comrak::markdown_to_html(&md_content, &options)
|
+ &comrak::markdown_to_html(&md_content, &options)
|
||||||
+ TEMPLATE_FOOTER;
|
+ TEMPLATE_FOOTER;
|
||||||
|
|
||||||
// TODO do a better sanitize of the title.
|
let filename = Context::sanitize_title(title);
|
||||||
let filename = title.replace(" ", "_");
|
|
||||||
let path = self.config.path_out.clone() + &format!("/{}.html", filename);
|
let path = self.config.path_out.clone() + &format!("/{}.html", filename);
|
||||||
|
|
||||||
println!("Publishing {}", path);
|
println!("Publishing {}", path);
|
||||||
@ -132,6 +136,22 @@ impl Context {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn remove_public(&mut self, note: &NoteMap) {
|
||||||
|
// TODO this will only remove the file if the note has been removed, not if the public tag
|
||||||
|
// has been removed. Handle this too.
|
||||||
|
let title = if let Some(content) = note.get("markdown_content") {
|
||||||
|
content.split("\n").next().unwrap()
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let filename = Context::sanitize_title(&title);
|
||||||
|
let path = self.config.path_out.clone() + &format!("/{}.html", filename);
|
||||||
|
|
||||||
|
// Missing files or errors on deletion are fine.
|
||||||
|
let _ = fs::remove_file(&path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_tags(notes: &NotesMap) -> TagMap {
|
fn collect_tags(notes: &NotesMap) -> TagMap {
|
||||||
@ -245,21 +265,20 @@ fn main() -> Result<(), io::Error> {
|
|||||||
match rx.recv() {
|
match rx.recv() {
|
||||||
Ok(evt) => {
|
Ok(evt) => {
|
||||||
use DebouncedEvent::*;
|
use DebouncedEvent::*;
|
||||||
let should_run_hooks = match &evt {
|
match &evt {
|
||||||
Create(path) | Write(path) => {
|
Create(path) | Write(path) => {
|
||||||
files.insert(path.clone(), read_file(path)?);
|
files.insert(path.clone(), read_file(path)?);
|
||||||
true
|
run_hooks(&mut context, &files)?;
|
||||||
}
|
}
|
||||||
Remove(path) => {
|
Remove(path) => {
|
||||||
|
if let Some(note) = files.get(path) {
|
||||||
|
context.remove_public(note);
|
||||||
|
}
|
||||||
files.remove(path);
|
files.remove(path);
|
||||||
true
|
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => {}
|
||||||
};
|
};
|
||||||
println!("{:?}", evt);
|
println!("{:?}", evt);
|
||||||
if should_run_hooks {
|
|
||||||
run_hooks(&mut context, &files)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(e) => println!("watch error: {:?}", e),
|
Err(e) => println!("watch error: {:?}", e),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user