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

Fix regular expression for compactability #114

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion eipw-lint/src/lints/markdown/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -131,11 +132,26 @@ impl<'a, 'b, 'c> tree::Visitor for ExcludesVisitor<'a, 'b, 'c> {
}

fn enter_text(&mut self, ast: &Ast, txt: &str) -> Result<Next, Self::Error> {
// 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<Next, Self::Error> {
self.check(ast, &link.title)
self.check(ast, &link.title)?;
// 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<Next, Self::Error> {
Expand Down
37 changes: 37 additions & 0 deletions eipw-lint/tests/lint_markdown_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,40 @@ hello
"#
);
}

#[tokio::test]
async fn excludes_autolink() {
let src = r#"---
header: value1
---

This is an autolink: <https://example1.com/>
This is a regular link: [example](https://example2.com/)
"#;

let reports = Linter::<Text<String>>::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`
"#
);
}