Works for inline links
This commit is contained in:
parent
a9c96314f7
commit
3ae35f405b
|
@ -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,
|
||||||
match end_range {
|
)),
|
||||||
Some(_) => {
|
Event::End(Tag::Link(link_type, dest, title)) => Event::End(Tag::Link(
|
||||||
let range = Range::<usize> {
|
link_type,
|
||||||
start: start_range.take().unwrap().start,
|
pulldown_cmark::CowStr::from(RE.replace_all(&dest, "/index.md").into_owned()),
|
||||||
end: end_range.take().unwrap().end,
|
title,
|
||||||
};
|
)),
|
||||||
ranges_to_replace_in.push(range);
|
// Event::FootnoteReference(text) => Event::FootnoteReference(
|
||||||
}
|
// pulldown_cmark::CowStr::from(RE.replace_all(&text, "/index.md").into_owned()),
|
||||||
None => {}
|
// ),
|
||||||
}
|
// 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),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
let content: String = ranges_to_replace_in
|
|
||||||
.iter()
|
// dbg!(
|
||||||
.map(|range| {
|
// match
|
||||||
// let substring: &str = "[fdfsd]: sdfsdfsdf/dfsdf/readme.md";
|
cmark(events.iter(), &mut buf, None).unwrap() // .map(|_| buf)
|
||||||
let substring: &str = &content[range.clone()];
|
// {
|
||||||
RE.replace_all(substring, "/index.md").into_owned()
|
// Ok(_) => ,
|
||||||
})
|
// Err(_) => content.to_string(),
|
||||||
.collect();
|
// }
|
||||||
content
|
// )
|
||||||
|
;
|
||||||
|
buf
|
||||||
|
|
||||||
|
// //TODO : change
|
||||||
|
// content.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
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)",
|
||||||
|
|
Loading…
Reference in New Issue