Skip to content

Commit

Permalink
extract fn, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Sep 26, 2024
1 parent e7c9958 commit 5cbfa56
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions crates/utils/src/utils/markdown/image_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ pub fn markdown_rewrite_image_links(mut src: String) -> (String, Vec<Url>) {
let mut links = vec![];
// Go through the collected links in reverse order
for (start, end) in links_offsets.into_iter().rev() {
let content = src.get(start..end).unwrap_or_default();
// necessary for custom emojis which look like `![name](url "title")`
let (url, extra) = if content.contains(' ') {
let split = content.split_once(' ').expect("split is valid");
(split.0, Some(split.1))
} else {
(content, None)
};
let (url, extra) = markdown_handle_title(&src, start, end);
match Url::parse(url) {
Ok(parsed) => {
links.push(parsed.clone());
Expand Down Expand Up @@ -46,7 +39,19 @@ pub fn markdown_rewrite_image_links(mut src: String) -> (String, Vec<Url>) {
(src, links)
}

pub fn find_links(src: &str) -> Vec<(usize, usize)> {
pub fn markdown_handle_title(src: &String, start: usize, end: usize) -> (&str, Option<&str>) {
let content = src.get(start..end).unwrap_or_default();
// necessary for custom emojis which look like `![name](url "title")`
let (url, extra) = if content.contains(' ') {
let split = content.split_once(' ').expect("split is valid");
(split.0, Some(split.1))
} else {
(content, None)
};
(url, extra)
}

pub fn markdown_find_links(src: &str) -> Vec<(usize, usize)> {
find_urls::<Link>(src)
}

Expand Down Expand Up @@ -98,7 +103,7 @@ mod tests {

#[test]
fn test_find_links() {
let links = find_links("[test](https://example.com)");
let links = markdown_find_links("[test](https://example.com)");
assert_eq!(vec![(7, 26)], links);

let links = find_urls::<Image>("![test](https://example.com)");
Expand Down

0 comments on commit 5cbfa56

Please sign in to comment.