This repository has been archived on 2025-10-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mdparser/src/links.rs
2024-05-02 11:36:58 -03:00

81 lines
2.2 KiB
Rust

use std::cell::RefCell;
use comrak::arena_tree::Node;
use comrak::nodes::{Ast, NodeLink, NodeValue};
use crate::utils;
pub fn iterate_links<'a, F>(ast: &'a Node<'a, RefCell<Ast>>, iterator: F)
where
F: Fn(&mut NodeLink),
{
utils::iter_nodes(ast, &|node| {
if let NodeValue::Link(ref mut l) = &mut node.data.borrow_mut().value {
iterator(l);
};
});
}
pub fn replace_links<'a>(ast: &'a Node<'a, RefCell<Ast>>, from: &'a str, to: &'a str) {
iterate_links(ast, |l| {
if l.url == from {
l.url = String::from(to)
}
});
}
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()));
let r = links.borrow().to_vec();
r
}
pub fn iterate_images<'a, F>(ast: &'a Node<'a, RefCell<Ast>>, iterator: F)
where
F: Fn(&mut NodeLink),
{
utils::iter_nodes(ast, &|node| {
if let NodeValue::Image(ref mut l) = &mut node.data.borrow_mut().value {
iterator(l);
};
});
}
pub fn replace_images<'a>(ast: &'a Node<'a, RefCell<Ast>>, from: &'a str, to: &'a str) {
iterate_images(ast, |l| {
if l.url == from {
l.url = String::from(to)
}
});
}
pub fn remove_image<'a>(ast: &'a Node<'a, RefCell<Ast>>, url: &'a str) {
utils::iter_nodes(ast, &|node| {
if let NodeValue::Image(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_images<'a>(ast: &'a Node<'a, RefCell<Ast>>) -> Vec<String> {
let images: RefCell<Vec<String>> = RefCell::new(vec![]);
iterate_images(ast, |l| images.borrow_mut().push(l.url.clone()));
let r = images.borrow().to_vec();
r
}