Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow link ignore with mlc-ignore #79

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/link_extractors/html_link_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,33 @@ enum ParserState {
Anchor,
EqualSign,
Link,
Ignore,
}

impl LinkExtractor for HtmlLinkExtractor {
fn find_links(&self, text: &str) -> Vec<MarkupLink> {
impl HtmlLinkExtractor {
pub fn find_links_with_ignore_info(&self, text: &str) -> (Vec<MarkupLink>, bool) {
let mut result: Vec<MarkupLink> = Vec::new();
let mut state: ParserState = ParserState::Text;
let mut link_column = 0;
let mut link_line = 0;
for (line, line_str) in text.lines().enumerate() {
let line_chars: Vec<char> = line_str.chars().collect();
let mut column: usize = 0;
if matches!(state, ParserState::Ignore) {
info!("Ignore line {}", line);
state = ParserState::Text;
continue;
}
while line_chars.get(column).is_some() {
match state {
ParserState::Ignore => {}
ParserState::Comment => {
if line_chars.get(column) == Some(&'-')
&& line_chars.get(column + 1) == Some(&'-')
&& line_chars.get(column + 2) == Some(&'>')
if line_str.contains("mlc-ignore") {
info!("Ignore next link from line {}", line);
state = ParserState::Ignore;
break;
} else if line_chars.len() >= column + 3
&& line_chars[column..column + 3] == ['-', '-', '>']
{
column += 2;
state = ParserState::Text;
Expand Down Expand Up @@ -104,7 +114,13 @@ impl LinkExtractor for HtmlLinkExtractor {
column += 1;
}
}
result
return (result, matches!(state, ParserState::Ignore));
}
}

impl LinkExtractor for HtmlLinkExtractor {
fn find_links(&self, text: &str) -> Vec<MarkupLink> {
return self.find_links_with_ignore_info(text).0;
}
}

Expand Down Expand Up @@ -155,6 +171,15 @@ mod tests {
assert_eq!(vec![expected], result);
}

#[test]
fn ignore_next_line() {
let le = HtmlLinkExtractor();
let result =
le.find_links("blah <!-- mlc-ignore -->\n<a href=\"some%20file.html\">foo</a>.");
assert!(result.is_empty());
}

#[test_case("<!-- mlc-ignore -->\n<a href=\"https://foo.com\">Bar</a>\n<a href=\"https://www.w3schools.com\">Visit W3Schools.com!</a>", 3, 1)]
#[test_case("<a href=\"https://www.w3schools.com\">Visit W3Schools.com!</a>", 1, 1)]
#[test_case(
"<a\nhref\n=\n \"https://www.w3schools.com\">\nVisit W3Schools.com!\n</a>",
Expand All @@ -167,7 +192,7 @@ mod tests {
1
)]
#[test_case(
"<!--comment--><a href=\"https://www.w3schools.com\">Visit W3Schools.com!</a>",
"<!--comment--><a href=\"https://www.w3schools.com\">Visit W3Schools.com!</a><!--inf comment",
1,
15
)]
Expand Down
24 changes: 23 additions & 1 deletion src/link_extractors/markdown_link_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ impl LinkExtractor for MarkdownLinkExtractor {
};

let mut result: Vec<MarkupLink> = Vec::new();
let mut ignore_next: bool = false;
for (evt, range) in parser.into_offset_iter() {
match evt {
Event::End(tag) => {
if ignore_next {
ignore_next = false;
continue;
}
match tag {
Tag::Link(_link_type, destination, _title)
| Tag::Image(_link_type, destination, _title) => {
Expand All @@ -62,7 +67,12 @@ impl LinkExtractor for MarkdownLinkExtractor {
}
Event::Html(html) => {
let line_col = line_column_from_idx(range.start);
let mut html_result = html_extractor.find_links(html.as_ref());
let (mut html_result, ignore) =
html_extractor.find_links_with_ignore_info(html.as_ref());
if ignore {
ignore_next = true;
continue;
}
html_result = html_result
.iter()
.map(|md_link| {
Expand Down Expand Up @@ -259,6 +269,18 @@ mod tests {
assert_eq!(vec![expected], result);
}

#[test]
fn link_ignored() {
let le = MarkdownLinkExtractor();
let link_str = "http://example.net/";
let input = format!(
"<!-- mlc-ignore -->\n[This link]({}) is ignore no title attribute.",
link_str
);
let result = le.find_links(&input);
assert!(result.is_empty());
}

#[test]
fn link_with_title() {
let le = MarkdownLinkExtractor();
Expand Down