feat(links): remove link command

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-05-01 17:02:17 -03:00
parent 400ec56531
commit 69c6e6b17e
2 changed files with 19 additions and 0 deletions

View File

@@ -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()));

View File

@@ -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| {