feat(links): remove link command
This commit is contained in:
11
src/links.rs
11
src/links.rs
@@ -24,6 +24,17 @@ pub fn replace_links<'a>(ast: &'a Node<'a, RefCell<Ast>>, from: &'a str, to: &'a
|
||||
});
|
||||
}
|
||||
|
||||
pub fn remove_link<'a>(ast: &'a Node<'a, RefCell<Ast>>, 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<Ast>>) -> Vec<String> {
|
||||
let links: RefCell<Vec<String>> = RefCell::new(vec![]);
|
||||
iterate_links(ast, |l| links.borrow_mut().push(l.url.clone()));
|
||||
|
||||
@@ -68,6 +68,10 @@ enum LinksCommands {
|
||||
#[clap(num_args = 2, value_names = ["FROM", "TO"], required = true)]
|
||||
replace: Vec<String>,
|
||||
},
|
||||
Remove {
|
||||
#[clap(num_args = 1, value_names = ["URL"], required = true)]
|
||||
links: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[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| {
|
||||
|
||||
Reference in New Issue
Block a user