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

Adds additional help for some links #94

Merged
merged 8 commits into from
Jun 19, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add relative link suggestions for ercs
  • Loading branch information
aslikaya committed Jun 19, 2024
commit e25c50c8f9b26a896234bba06640532081fcab74
15 changes: 9 additions & 6 deletions eipw-lint/src/lints/markdown/relative_links.rs
Original file line number Diff line number Diff line change
@@ -32,8 +32,8 @@ where
S: Debug + Display + AsRef<str>,
{
fn lint<'a>(&self, slug: &'a str, ctx: &Context<'a, '_>) -> Result<(), Error> {
let re: Regex = Regex::new("(^/)|(://)").unwrap();
let eip_re = Regex::new(r"^(https?:)?//eips\.ethereum\.org/(EIPS/eip-\d+|assets/.+)$").unwrap();
let re = Regex::new("(^/)|(://)").unwrap();
let eip_re = Regex::new(r"^(https?:)?//(?:eips|ercs)\.ethereum\.org/(?:EIPS|ERCS)/(?:eip|erc)-(\d+)|(assets/.+)$").unwrap();

let exceptions = RegexSet::new(&self.exceptions).map_err(Error::custom)?;

@@ -47,11 +47,14 @@ where

for Link { address, line_start } in links {
let (suggestion, extra_help) = if let Some(caps) = eip_re.captures(&address) {
let relevant_part = &caps[2];
if relevant_part.starts_with("EIPS/") {
(format!("./{}.md", relevant_part.trim_start_matches("EIPS/")), true)
if let Some(id_number) = caps.get(2) {
let suggestion = format!("./eip-{}.md", id_number.as_str());
(suggestion, true)
} else if let Some(asset_path) = caps.get(3) {
let suggestion = format!("../{}", asset_path.as_str());
(suggestion, true)
} else {
(format!("../{}", relevant_part), true)
(address, false)
}
} else if address.contains("//creativecommons.org/publicdomain/zero/1.0/") {
("../LICENSE.md".to_string(), true)
172 changes: 172 additions & 0 deletions eipw-lint/tests/lint_markdown_relative_links.rs
Original file line number Diff line number Diff line change
@@ -103,6 +103,40 @@ header: value1
);
}

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

[hello](https://ercs.ethereum.org/ERCS/erc-1234)
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny(
"markdown-rel",
RelativeLinks {
exceptions: Vec::<&str>::new(),
},
)
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | [hello](https://ercs.ethereum.org/ERCS/erc-1234)
|
= help: use `./eip-1234.md` instead
"#
);
}

#[tokio::test]
async fn inline_link_with_scheme_to_creativecommons_copyright() {
let src = r#"---
@@ -361,6 +395,42 @@ Hello [hi][hello]!
);
}

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

Hello [hi][hello]!

[hello]: https://ercs.ethereum.org/ERCS/erc-1234
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny(
"markdown-rel",
RelativeLinks {
exceptions: Vec::<&str>::new(),
},
)
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | Hello [hi][hello]!
|
= help: use `./eip-1234.md` instead
"#
);
}

#[tokio::test]
async fn inline_autolink() {
let src = r#"---
@@ -496,6 +566,40 @@ header: value1
);
}

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

<a href="//ercs.ethereum.org/ERCS/erc-1234">example</a>
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny(
"markdown-rel",
RelativeLinks {
exceptions: Vec::<&str>::new(),
},
)
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | <a href="//ercs.ethereum.org/ERCS/erc-1234">example</a>
|
= help: use `./eip-1234.md` instead
"#
);
}

#[tokio::test]
async fn anchor_link_relative_double_slash() {
let src = r#"---
@@ -649,6 +753,40 @@ header: value1
);
}

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

<img src="//ercs.ethereum.org/assets/erc-712/eth_sign.png">
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny(
"markdown-rel",
RelativeLinks {
exceptions: Vec::<&str>::new(),
},
)
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | <img src="//ercs.ethereum.org/assets/erc-712/eth_sign.png">
|
= help: use `../assets/erc-712/eth_sign.png` instead
"#
);
}

#[tokio::test]
async fn img_with_scheme_to_eips_ethereum_org() {
let src = r#"---
@@ -682,3 +820,37 @@ header: value1
"#
);
}

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

<img src="https://ercs.ethereum.org/assets/erc-712/eth_sign.png">
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny(
"markdown-rel",
RelativeLinks {
exceptions: Vec::<&str>::new(),
},
)
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | <img src="https://ercs.ethereum.org/assets/erc-712/eth_sign.png">
|
= help: use `../assets/erc-712/eth_sign.png` instead
"#
);
}
Loading
Oops, something went wrong.