diff --git a/src/links.rs b/src/links.rs index 106e72b..11eb916 100644 --- a/src/links.rs +++ b/src/links.rs @@ -24,6 +24,17 @@ pub fn replace_links<'a>(ast: &'a Node<'a, RefCell>, from: &'a str, to: &'a }); } +pub fn remove_link<'a>(ast: &'a Node<'a, RefCell>, url: &'a str) { + utils::iter_nodes(ast, &|node| { + if let NodeValue::Link(ref mut l) = &mut node.data.borrow_mut().value { + if l.url == url { + node.children().for_each(|n| node.insert_before(n)); + node.detach(); + } + } + }) +} + pub fn get_links<'a>(ast: &'a Node<'a, RefCell>) -> Vec { let links: RefCell> = RefCell::new(vec![]); iterate_links(ast, |l| links.borrow_mut().push(l.url.clone())); diff --git a/src/main.rs b/src/main.rs index 149514d..276078b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,6 +68,10 @@ enum LinksCommands { #[clap(num_args = 2, value_names = ["FROM", "TO"], required = true)] replace: Vec, }, + Remove { + #[clap(num_args = 1, value_names = ["URL"], required = true)] + links: Vec, + }, } #[derive(Debug, Subcommand)] @@ -119,6 +123,10 @@ fn main() { .for_each(|p| links::replace_links(ast, &p[0], &p[1])); cli::ResultType::Markdown(ast) } + LinksCommands::Remove { links } => { + links.iter().for_each(|l| links::remove_link(ast, l)); + cli::ResultType::Markdown(ast) + } }, Commands::Frontmatter { command } => { if let None = ast.children().find(|c| {