Works for inline links

This commit is contained in:
Raphaël Duchaîne 2020-09-18 09:59:52 -04:00
parent a9c96314f7
commit 3ae35f405b
1 changed files with 56 additions and 39 deletions

View File

@ -2,7 +2,6 @@ use core::ops::Range;
use pulldown_cmark::{Event, LinkType, Parser, Tag}; use pulldown_cmark::{Event, LinkType, Parser, Tag};
use pulldown_cmark_to_cmark::cmark; use pulldown_cmark_to_cmark::cmark;
use regex::Regex; use regex::Regex;
use std::iter::{FromIterator, Iterator};
use std::path::Path; use std::path::Path;
use crate::errors::*; use crate::errors::*;
@ -60,36 +59,54 @@ fn replace_readme_in_string(content: &str) -> String {
) )
.unwrap(); .unwrap();
} }
let parser = pulldown_cmark::Parser::new(content).into_offset_iter(); let parser = pulldown_cmark::Parser::new(content);
let mut ranges_to_replace_in = vec![]; let mut buf = String::with_capacity(content.len());
let mut start_range: Option<Range<usize>> = None;
let mut end_range: Option<Range<usize>> = None; let mut events = vec![];
for (event, range) in parser { for event in parser {
match event { events.push(match event {
Event::Start(Tag::Link(link_type, dest, title)) => start_range = Some(range), Event::Start(Tag::Link(link_type, dest, title)) => Event::Start(Tag::Link(
Event::End(Tag::Link(link_type, dest, title)) => end_range = Some(range), link_type,
_ => (), pulldown_cmark::CowStr::from(RE.replace_all(&dest, "/index.md").into_owned()),
title,
)),
Event::End(Tag::Link(link_type, dest, title)) => Event::End(Tag::Link(
link_type,
pulldown_cmark::CowStr::from(RE.replace_all(&dest, "/index.md").into_owned()),
title,
)),
// Event::FootnoteReference(text) => Event::FootnoteReference(
// pulldown_cmark::CowStr::from(RE.replace_all(&text, "/index.md").into_owned()),
// ),
// Event::End(Tag::Link(link_type, dest, title)) => Event::End(Tag::Link(
// link_type,
// pulldown_cmark::CowStr::from(dbg!(RE.replace_all(&dest, "/index.md").into_owned())),
// title,
// )),
// pulldown_cmark::Event::Text(text)
// if (text.chars().nth(0).unwrap() == Char::from(":")) =>
// {
// pulldown_cmark::Event::Text(pulldown_cmark::CowStr::from(dbg!(RE
// .replace_all(&text, "/index.md")
// .into_owned())))
// },
v => dbg!(v),
});
} }
match end_range {
Some(_) => { // dbg!(
let range = Range::<usize> { // match
start: start_range.take().unwrap().start, cmark(events.iter(), &mut buf, None).unwrap() // .map(|_| buf)
end: end_range.take().unwrap().end, // {
}; // Ok(_) => ,
ranges_to_replace_in.push(range); // Err(_) => content.to_string(),
} // }
None => {} // )
} ;
} buf
let content: String = ranges_to_replace_in
.iter() // //TODO : change
.map(|range| { // content.to_string()
// let substring: &str = "[fdfsd]: sdfsdfsdf/dfsdf/readme.md";
let substring: &str = &content[range.clone()];
RE.replace_all(substring, "/index.md").into_owned()
})
.collect();
content
} }
fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) { fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) {
@ -183,16 +200,16 @@ mod tests {
replace_readme_tests! { replace_readme_tests! {
"[content](./bla/index.md) content: ( ./readme.md)", "[content](./bla/index.md) content: ( ./readme.md)",
replace_readme_only_in_link_cases_1:"[content](./bla/readme.md) content: ( ./readme.md)", replace_readme_only_in_link_cases_1:"[content](./bla/readme.md) content: ( ./readme.md)",
replace_readme_only_in_link_cases_2:"[content](./bla/readme.md) content: ( ./reADme.md)", replace_readme_only_in_link_cases_2:"[content](./bla/rEaDme.md) content: ( ./readme.md)",
replace_readme_only_in_link_cases_3:"[content](./bla/readme.md) content: ( ./readMe.md)", replace_readme_only_in_link_cases_3:"[content](./bla/README.MD) content: ( ./readme.md)",
replace_readme_only_in_link_cases_4:"[content](./bla/readme.md) content: ( ./readme.MD)", replace_readme_only_in_link_cases_4:"[content](./bla/reAdme.md) content: ( ./readme.md)",
replace_readme_only_in_link_cases_5:"[content](./bla/readme.md) content: ( ./readme.md)", replace_readme_only_in_link_cases_5:"[content](./bla/Readme.md) content: ( ./readme.md)",
replace_readme_only_in_link_cases_6:"[content](./bla/readme.md) content: ( ./REAdme.md)", replace_readme_only_in_link_cases_6:"[content](./bla/README.md) content: ( ./readme.md)",
replace_readme_only_in_link_cases_7:"[content](./bla/readme.md) content: ( ./README.MD)", replace_readme_only_in_link_cases_7:"[content](./bla/readme.MD) content: ( ./readme.md)",
} }
replace_readme_tests! { replace_readme_tests! {
"🤞🏼[content](./bla/index.md)🤞🏼 content: ( ./readme.md)", "🤞🏼[content](./bla/index.md)🤞🏼\n[content]: ./index.md",
replace_readme_only_in_link_cases_even_with_multibyte_chars:"🤞🏼[content](./bla/readme.md)🤞🏼 content: ( ./README.MD)", replace_readme_only_in_link_cases_even_with_multibyte_chars:"🤞🏼[content](./bla/readme.md)🤞🏼\n[content]: ./README.MD",
} }
replace_readme_tests! { replace_readme_tests! {
@ -207,7 +224,7 @@ mod tests {
replace_readme_in_reference_link_test_8:"[content]: ./bla/readme.MD ", replace_readme_in_reference_link_test_8:"[content]: ./bla/readme.MD ",
} }
replace_readme_tests! { replace_readme_tests! {
"[content]( ./bla/index.md)", "[content](./bla/index.md)",
replace_readme_in_inline_link_test_1:"[content]( ./bla/readme.md)", replace_readme_in_inline_link_test_1:"[content]( ./bla/readme.md)",
replace_readme_in_inline_link_test_2:"[content]( ./bla/ReAdme.md)", replace_readme_in_inline_link_test_2:"[content]( ./bla/ReAdme.md)",
replace_readme_in_inline_link_test_3:"[content]( ./bla/ReaDme.md)", replace_readme_in_inline_link_test_3:"[content]( ./bla/ReaDme.md)",