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

Issue 5 #66

Closed
wants to merge 154 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
154 commits
Select commit Hold shift + click to select a range
69a301a
Update relative_links.rs
JEAlfonsoP Feb 25, 2023
914e3a2
Update relative_links.rs
JEAlfonsoP Feb 26, 2023
2f5d378
Update relative_links.rs
JEAlfonsoP Feb 28, 2023
77f75e5
Update relative_links.rs
JEAlfonsoP Feb 28, 2023
c92e080
Update relative_links.rs
JEAlfonsoP Feb 28, 2023
c7e0837
Update relative_links.rs
JEAlfonsoP Feb 28, 2023
8536582
Update relative_links.rs
JEAlfonsoP Feb 28, 2023
989717d
Update relative_links.rs
JEAlfonsoP Feb 28, 2023
fbf892b
Update relative_links.rs
JEAlfonsoP Feb 28, 2023
3784ce4
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
abf9b09
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
519e30f
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
39a09aa
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
0ee73e5
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
4fbd8d7
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
e3cad6f
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
194bca0
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
1438752
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
c14938c
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
0c2c6ee
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
04e019e
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
95fc09e
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
e27884e
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
f87035e
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
503c9fd
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
9734771
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
fb0370f
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
28e762e
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
f2d980c
Update relative_links.rs
JEAlfonsoP Mar 1, 2023
0f33f04
Update relative_links.rs
JEAlfonsoP Mar 2, 2023
aa2b5ef
Update relative_links.rs
JEAlfonsoP Mar 2, 2023
205f167
Update relative_links.rs
JEAlfonsoP Mar 12, 2023
b005c30
Update test.yaml
JEAlfonsoP Mar 12, 2023
585408b
Update relative_links.rs
JEAlfonsoP Mar 12, 2023
10b8cd4
Update relative_links.rs
JEAlfonsoP Mar 12, 2023
f354723
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
0d9b95c
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
23c6f48
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
5f3792d
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
b7d08a8
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
02b7d5d
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
c61f5c7
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
ef1643b
Update relative_links.rs
JEAlfonsoP Mar 13, 2023
8a44f14
Update relative_links.rs
JEAlfonsoP Mar 26, 2023
029e60b
Update Cargo.toml
JEAlfonsoP Mar 26, 2023
9fc33af
Update relative_links.rs
JEAlfonsoP Mar 26, 2023
d57a1ee
Update Cargo.toml
JEAlfonsoP Mar 26, 2023
c4f2b46
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
7f7eb6f
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
7f50888
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
7037854
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
ef89240
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
974ee9d
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
8159fca
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
a540144
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
56e93ab
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
8f88cba
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
90e8040
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
45b54c6
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
8071034
Update relative_links.rs
JEAlfonsoP Mar 29, 2023
98a9c9e
Update relative_links.rs
JEAlfonsoP Mar 30, 2023
8477327
Update relative_links.rs
JEAlfonsoP Mar 30, 2023
22bf135
Update relative_links.rs
JEAlfonsoP Mar 30, 2023
1154a28
Update relative_links.rs
JEAlfonsoP Mar 30, 2023
156f865
Update relative_links.rs
JEAlfonsoP Apr 1, 2023
7d81d1c
Update relative_links.rs
JEAlfonsoP Apr 1, 2023
dc0eb3a
Update relative_links.rs
JEAlfonsoP Apr 1, 2023
09081d3
Update relative_links.rs
JEAlfonsoP Apr 1, 2023
b67aec5
Update relative_links.rs
JEAlfonsoP Apr 1, 2023
ea7c1d5
Update relative_links.rs
JEAlfonsoP Apr 1, 2023
ecfcae5
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
d80f205
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
8433196
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
29ff9dc
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
6e096f5
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
ed358ac
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
0d9ad5c
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
ea9a1e3
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
99194de
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
c259fbf
Update relative_links.rs
JEAlfonsoP Apr 2, 2023
afebb9e
Update relative_links.rs
JEAlfonsoP Apr 4, 2023
fbdb3e3
Update relative_links.rs
JEAlfonsoP Apr 9, 2023
b9be5f0
Update relative_links.rs
JEAlfonsoP Apr 9, 2023
5861f98
Update relative_links.rs
JEAlfonsoP Apr 10, 2023
a62c00b
Update relative_links.rs
JEAlfonsoP Apr 10, 2023
9ccec5c
Update relative_links.rs
JEAlfonsoP Apr 10, 2023
82a6ddf
Update test.yaml
JEAlfonsoP Apr 10, 2023
aa8fe8d
Update test.yaml
JEAlfonsoP Apr 10, 2023
73378a4
Update relative_links.rs
JEAlfonsoP Apr 10, 2023
142c8b8
Update relative_links.rs
JEAlfonsoP Apr 10, 2023
672eccc
Update relative_links.rs
JEAlfonsoP Apr 10, 2023
f4a1dd4
Update test.yaml
JEAlfonsoP Apr 10, 2023
33f6924
Update relative_links.rs
JEAlfonsoP Apr 10, 2023
f9f4756
Update test.yaml
JEAlfonsoP Apr 10, 2023
74e175e
Update Cargo.toml
JEAlfonsoP Apr 11, 2023
44d3fd3
Update test.yaml
JEAlfonsoP Apr 11, 2023
1d8b347
Update test.yaml
JEAlfonsoP Apr 11, 2023
267fba7
Update test.yaml
JEAlfonsoP Apr 11, 2023
8dbe93b
Update test.yaml
JEAlfonsoP Apr 11, 2023
281b160
Update test.yaml
JEAlfonsoP Apr 11, 2023
83d35f0
Update relative_links.rs
JEAlfonsoP Apr 11, 2023
b0f0f44
Update test.yaml
JEAlfonsoP Apr 11, 2023
37a1a8d
Update test.yaml
JEAlfonsoP Apr 11, 2023
461f62d
Update test.yaml
JEAlfonsoP Apr 11, 2023
550a0a4
Update test.yaml
JEAlfonsoP Apr 11, 2023
0fa616e
Merge branch 'master' into issue-5
JEAlfonsoP Apr 11, 2023
8f674ac
Update Cargo.toml
JEAlfonsoP Apr 11, 2023
aa90134
Update relative_links.rs
JEAlfonsoP Apr 17, 2023
1548e06
Update relative_links.rs
JEAlfonsoP Apr 17, 2023
70ef035
Update relative_links.rs
JEAlfonsoP Apr 17, 2023
dbaf3e7
Update relative_links.rs
JEAlfonsoP Apr 17, 2023
fdcd335
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
3b715dd
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
611f87f
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
4abdfce
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
72cdbb8
Update Cargo.toml
JEAlfonsoP Apr 18, 2023
a6a05a3
Update test.yaml
SamWilsn Apr 18, 2023
28b0e06
Update Cargo.toml
SamWilsn Apr 18, 2023
82dc7eb
Run cargo fmt
SamWilsn Apr 18, 2023
28425a6
Update test.yaml
JEAlfonsoP Apr 18, 2023
a7547aa
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
83f5909
Update Cargo.toml
JEAlfonsoP Apr 18, 2023
113d217
Update Cargo.toml
JEAlfonsoP Apr 18, 2023
68c5e0e
Update test.yaml
JEAlfonsoP Apr 18, 2023
2a3f864
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
4779c69
Update test.yaml
JEAlfonsoP Apr 18, 2023
ff2428a
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
8355aa8
Update test.yaml
JEAlfonsoP Apr 18, 2023
68b7939
Update test.yaml
JEAlfonsoP Apr 18, 2023
b96ac2b
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
1ee85d5
Update Cargo.toml
JEAlfonsoP Apr 18, 2023
ae6b2b5
Update Cargo.toml
JEAlfonsoP Apr 18, 2023
ee0bd56
Update test.yaml
JEAlfonsoP Apr 18, 2023
85b6846
Update relative_links.rs
JEAlfonsoP Apr 18, 2023
63e9a46
Update relative_links.rs
JEAlfonsoP Apr 20, 2023
45bc5f9
Bit of work on implementation and more tests (#5)
SamWilsn Apr 21, 2023
c8ed588
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
86db50c
Update lint_markdown_relative_links.rs
JEAlfonsoP Apr 21, 2023
e6da31c
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
21cef25
Update lint_markdown_relative_links.rs
JEAlfonsoP Apr 21, 2023
5ca178a
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
3372b1c
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
7bf98cc
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
04babf3
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
02c32ee
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
58fc130
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
ecf0f3e
Update relative_links.rs
JEAlfonsoP Apr 21, 2023
c7b01a1
Update relative_links.rs
JEAlfonsoP Apr 22, 2023
5e56b3a
Update relative_links.rs
JEAlfonsoP Apr 22, 2023
8afa3c7
Update relative_links.rs
JEAlfonsoP Apr 22, 2023
c201af6
Update relative_links.rs
JEAlfonsoP Apr 22, 2023
3afd17d
Update relative_links.rs
JEAlfonsoP Apr 22, 2023
5a5620c
Update relative_links.rs
JEAlfonsoP Apr 22, 2023
6036658
Update relative_links.rs
JEAlfonsoP Apr 22, 2023
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
37 changes: 33 additions & 4 deletions eipw-lint/src/lints/markdown/relative_links.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
* file. You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use annotate_snippets::snippet::{Annotation, Slice, Snippet};
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet};

use comrak::nodes::Ast;

use std::fmt::Write;

use crate::lints::{Context, Error, Lint};
use crate::tree::{self, Next, TraverseExt};

use regex::bytes::{Regex, RegexSet};
use std::str;
use std::string::String;

use scraper::node::Node as HtmlNode;
use scraper::Html;
Expand All @@ -26,6 +30,7 @@ pub struct RelativeLinks<'e> {
impl<'e> Lint for RelativeLinks<'e> {
fn lint<'a, 'b>(&self, slug: &'a str, ctx: &Context<'a, 'b>) -> Result<(), Error> {
let re = Regex::new("(^/)|(://)").unwrap();
let re_eip_num = Regex::new(r"eip-\d{1,4}").unwrap();

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

Expand All @@ -37,14 +42,38 @@ impl<'e> Lint for RelativeLinks<'e> {
.into_iter()
.filter(|l| re.is_match(&l.address) && !exceptions.is_match(&l.address));

for Link { line_start, .. } in links {
for Link {
line_start,
address,
..
} in links
{
let mut footer_label = String::new();
let mut footer = vec![];
let line_with_address = str::from_utf8(&address).unwrap();
let mut test_assets_string = String::new();
if let Some(num) = re_eip_num.captures(line_with_address.as_bytes()) {
let eip_num = str::from_utf8(&num[0]).unwrap();
write!(test_assets_string, "assets/{0}/eth_sign.png", &eip_num).unwrap();
if !(line_with_address.contains(&test_assets_string)) {
write!(footer_label, "use `./{0}.md` instead", &eip_num).unwrap();
} else {
write!(footer_label, "use `../{0}` instead", &test_assets_string).unwrap();
}
footer.push(Annotation {
annotation_type: AnnotationType::Help,
id: None,
label: Some(&footer_label),
});
}

ctx.report(Snippet {
title: Some(Annotation {
id: Some(slug),
annotation_type: ctx.annotation_type(),
label: Some("non-relative link or image"),
}),
footer: vec![],
footer,
slices: vec![Slice {
line_start: usize::try_from(line_start).unwrap(),
fold: false,
Expand Down
148 changes: 147 additions & 1 deletion eipw-lint/tests/lint_markdown_relative_links.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
* file. You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use eipw_lint::lints::markdown::RelativeLinks;
Expand Down Expand Up @@ -64,6 +64,63 @@ header: value1
);
}

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

[hello](https://eips.ethereum.org/EIPS/eip-1234)
"#;

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

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

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

[hi](https://example.com/4444)
"#;

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

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | [hi](https://example.com/4444)
|
"#
);
}

#[tokio::test]
async fn inline_link_protocol_relative() {
let src = r#"---
Expand Down Expand Up @@ -194,6 +251,37 @@ Hello [hi][hello]!
assert_eq!(reports, "");
}

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

Hello [hi][hello]!

[hello]: https://eips.ethereum.org/EIPS/eip-1234
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.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#"---
Expand Down Expand Up @@ -280,6 +368,35 @@ header: value1
);
}

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

<a href="//eips.ethereum.org/EIPS/eip-1234">example</a>
"#;

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

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

#[tokio::test]
async fn anchor_link_relative_double_slash() {
let src = r#"---
Expand Down Expand Up @@ -349,3 +466,32 @@ header: value1
"#
);
}

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

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

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

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