pub(crate) mod index;
pub(crate) mod resolver;
use crate::ErrorKind;
use pulldown_cmark::CowStr;
const MARKDOWN_FRAGMENT_MARKER: char = '#';
const MARKDOWN_POTHOLE_MARKER: char = '|';
pub(crate) fn wikilink(input: &str, has_pothole: bool) -> Result<CowStr<'_>, ErrorKind> {
let mut stripped_input = if has_pothole {
pulldown_cmark::CowStr::Borrowed(
&input[0..input.find(MARKDOWN_POTHOLE_MARKER).unwrap_or(input.len())],
)
} else {
CowStr::Borrowed(input)
};
if stripped_input.contains(MARKDOWN_FRAGMENT_MARKER) {
stripped_input = pulldown_cmark::CowStr::Borrowed(
&input[0..input.find(MARKDOWN_FRAGMENT_MARKER).unwrap_or(input.len())],
);
}
if stripped_input.is_empty() {
return Err(ErrorKind::EmptyUrl);
}
Ok(stripped_input)
}
#[cfg(test)]
mod tests {
use pulldown_cmark::CowStr;
use rstest::rstest;
use crate::checker::wikilink::wikilink;
#[rstest]
#[case("|foo", true)]
#[case("|foo#bar", true)]
#[case("|foo#bar|foo#bar", true)]
#[case("#baz", false)]
#[case("#baz#baz|foo", false)]
fn test_empty_wikilinks_are_detected(#[case] input: &str, #[case] has_pothole: bool) {
let result = wikilink(input, has_pothole);
assert!(result.is_err());
}
#[rstest]
#[case("link with spaces", true, "link with spaces")]
#[case("foo.fileextension", true, "foo.fileextension")]
#[case("specialcharacters !_@$&(){}", true, "specialcharacters !_@$&(){}")]
fn test_valid_wikilinks(#[case] input: &str, #[case] has_pothole: bool, #[case] actual: &str) {
let result = wikilink(input, has_pothole).unwrap();
let actual = CowStr::Borrowed(actual);
assert_eq!(result, actual);
}
#[rstest]
#[case("foo|bar", true, "foo")]
#[case("foo#bar", true, "foo")]
#[case("foo#bar|baz", false, "foo")]
#[case("foo#bar|baz#hashtag_in_pothole", false, "foo")]
#[case("foo with spaces#bar|baz#hashtag_in_pothole", false, "foo with spaces")]
#[case(
"specialcharacters !_@$&(){}#bar|baz#hashtag_in_pothole",
true,
"specialcharacters !_@$&(){}"
)]
fn test_fragment_and_pothole_removal(
#[case] input: &str,
#[case] has_pothole: bool,
#[case] actual: &str,
) {
let result = wikilink(input, has_pothole).unwrap();
let actual = CowStr::Borrowed(actual);
assert_eq!(result, actual);
}
}