From 4d718e2772be256f0b8d13828ad655a5b0d2b11e Mon Sep 17 00:00:00 2001 From: IjayAbby Date: Wed, 23 Oct 2024 16:19:06 +0300 Subject: [PATCH 1/3] modify enter_link function --- eipw-lint/tests/lint_markdown_regex.rs | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/eipw-lint/tests/lint_markdown_regex.rs b/eipw-lint/tests/lint_markdown_regex.rs index 27d13b15..ce83600e 100644 --- a/eipw-lint/tests/lint_markdown_regex.rs +++ b/eipw-lint/tests/lint_markdown_regex.rs @@ -108,3 +108,40 @@ hello "# ); } + +#[tokio::test] +async fn excludes_autolink() { + let src = r#"--- +header: value1 +--- + +This is an autolink: +This is a regular link: [example](https://example.com/) +"#; + + let reports = Linter::>::default() + .clear_lints() + .deny( + "markdown-re", + Regex { + message: "should not match in autolink", + mode: Mode::Excludes, + pattern: "example", + }, + ) + .check_slice(None, src) + .run() + .await + .unwrap() + .into_inner(); + + assert_eq!( + reports, + r#"error[markdown-re]: should not match in autolink + | +6 | This is a regular link: [example](https://example.com/) + | + = info: the pattern in question: `example` +"# + ); +} From d2ad8d9d7222a5b70ed19c066b5e72c0bd8d2d10 Mon Sep 17 00:00:00 2001 From: IjayAbby Date: Wed, 23 Oct 2024 16:26:41 +0300 Subject: [PATCH 2/3] regex file modification --- eipw-lint/src/lints/markdown/regex.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/eipw-lint/src/lints/markdown/regex.rs b/eipw-lint/src/lints/markdown/regex.rs index 7d381735..e8222882 100644 --- a/eipw-lint/src/lints/markdown/regex.rs +++ b/eipw-lint/src/lints/markdown/regex.rs @@ -135,7 +135,13 @@ impl<'a, 'b, 'c> tree::Visitor for ExcludesVisitor<'a, 'b, 'c> { } fn enter_link(&mut self, ast: &Ast, link: &NodeLink) -> Result { - self.check(ast, &link.title) + // Skip the check if the link is an autolink + if link.url.starts_with('<') && link.url.ends_with('>') { + return Ok(Next::TraverseChildren); + } + // For regular links, check both title and URL + self.check(ast, &link.title)?; + self.check(ast, &link.url) } fn enter_image(&mut self, ast: &Ast, link: &NodeLink) -> Result { From 50b4290bd21bc69539659ac86154e8ff3d4eea37 Mon Sep 17 00:00:00 2001 From: IjayAbby Date: Wed, 23 Oct 2024 19:31:39 +0300 Subject: [PATCH 3/3] add depart_link --- eipw-lint/src/lints/markdown/regex.rs | 22 ++++++++++++++++------ eipw-lint/tests/lint_markdown_regex.rs | 4 ++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/eipw-lint/src/lints/markdown/regex.rs b/eipw-lint/src/lints/markdown/regex.rs index e8222882..dfd94c8b 100644 --- a/eipw-lint/src/lints/markdown/regex.rs +++ b/eipw-lint/src/lints/markdown/regex.rs @@ -68,6 +68,7 @@ struct ExcludesVisitor<'a, 'b, 'c> { pattern: &'c str, slug: &'c str, message: &'c str, + string_title: String, } impl<'a, 'b, 'c> ExcludesVisitor<'a, 'b, 'c> { @@ -131,17 +132,26 @@ impl<'a, 'b, 'c> tree::Visitor for ExcludesVisitor<'a, 'b, 'c> { } fn enter_text(&mut self, ast: &Ast, txt: &str) -> Result { + // Append text content to string_title for links + self.string_title.push_str(txt); self.check(ast, txt) } fn enter_link(&mut self, ast: &Ast, link: &NodeLink) -> Result { - // Skip the check if the link is an autolink - if link.url.starts_with('<') && link.url.ends_with('>') { - return Ok(Next::TraverseChildren); - } - // For regular links, check both title and URL self.check(ast, &link.title)?; - self.check(ast, &link.url) + // Initialize the string_title for capturing text content + self.string_title = String::new(); + Ok(Next::TraverseChildren) + } + + fn depart_link(&mut self, ast: &Ast, link: &NodeLink) -> Result<(), Self::Error> { + // Check if the captured text content is different from the URL + if self.string_title != link.url { + self.check(ast, &link.url)?; + } + // Reset the string_title + self.string_title.clear(); + Ok(()) } fn enter_image(&mut self, ast: &Ast, link: &NodeLink) -> Result { diff --git a/eipw-lint/tests/lint_markdown_regex.rs b/eipw-lint/tests/lint_markdown_regex.rs index ce83600e..cbd61087 100644 --- a/eipw-lint/tests/lint_markdown_regex.rs +++ b/eipw-lint/tests/lint_markdown_regex.rs @@ -115,8 +115,8 @@ async fn excludes_autolink() { header: value1 --- -This is an autolink: -This is a regular link: [example](https://example.com/) +This is an autolink: +This is a regular link: [example](https://example2.com/) "#; let reports = Linter::>::default()