Skip to content

Commit

Permalink
Allow links to devp2p specs
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Dec 6, 2022
1 parent 27851cf commit b4c2124
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [ "eipw-lint", "eipw-lint-js" ]
[package]
name = "eipw"
description = "Ethereum Improvement Proposal linter that's one more than eipv"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
license = "MPL-2.0"
rust-version = "1.65"
Expand All @@ -16,7 +16,7 @@ repository = "https://github.com/ethereum/eipw"
annotate-snippets = "0.9.1"
tokio = { version = "1.21.2", features = [ "macros" ] }
clap = { version = "4.0.17", features = [ "derive" ] }
eipw-lint = { version = "0.2.0", path = "eipw-lint", features = [ "tokio" ] }
eipw-lint = { version = "0.3.0", path = "eipw-lint", features = [ "tokio" ] }
serde_json = "1.0.86"
thiserror = "1.0.37"

Expand Down
4 changes: 2 additions & 2 deletions eipw-lint-js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eipw-lint-js"
version = "0.2.6"
version = "0.3.0"
edition = "2021"
license = "MPL-2.0"
rust-version = "1.65"
Expand All @@ -23,7 +23,7 @@ wasm-bindgen = { version = "0.2.83", features = [ "serde-serialize" ] }
serde-wasm-bindgen = "0.4.5"
wasm-bindgen-futures = "0.4.33"
console_error_panic_hook = { version = "0.1.7", optional = true }
eipw-lint = { version = "0.2.0", path = "../eipw-lint" }
eipw-lint = { version = "0.3.0", path = "../eipw-lint" }
js-sys = "0.3.60"
serde_json = "1.0.86"
serde = { version = "1.0", features = [ "derive" ] }
Expand Down
2 changes: 1 addition & 1 deletion eipw-lint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eipw-lint"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
license = "MPL-2.0"
rust-version = "1.65"
Expand Down
7 changes: 6 additions & 1 deletion eipw-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,12 @@ pub fn default_lints() -> impl Iterator<Item = (&'static str, Box<dyn Lint>)> {
"markdown-link-first",
markdown::LinkFirst(r"(?i)eip-[0-9]+").boxed(),
),
("markdown-rel-links", markdown::RelativeLinks.boxed()),
("markdown-rel-links", markdown::RelativeLinks {
exceptions: &[
"^https://(www\\.)?github\\.com/ethereum/consensus-specs/blob/[a-f0-9]{40}/.+$",
"^https://(www\\.)?github\\.com/ethereum/devp2p/blob/[0-9a-f]{40}/.+$"
]
}.boxed()),
(
"markdown-link-status",
markdown::LinkStatus {
Expand Down
16 changes: 8 additions & 8 deletions eipw-lint/src/lints/markdown/relative_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ use comrak::nodes::Ast;
use crate::lints::{Context, Error, Lint};
use crate::tree::{self, Next, TraverseExt};

use regex::bytes::Regex;
use regex::bytes::{Regex, RegexSet};

use scraper::node::Node as HtmlNode;
use scraper::Html;

use snafu::Snafu;

#[derive(Debug)]
pub struct RelativeLinks;
pub struct RelativeLinks<'e> {
pub exceptions: &'e [&'e str],
}

impl Lint for RelativeLinks {
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 cs_re = Regex::new(
"^https://(www\\.)?github\\.com/ethereum/consensus-specs/blob/[a-f0-9]{40}/.+$",
)
.unwrap();

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

let mut visitor = Visitor::default();
ctx.body().traverse().visit(&mut visitor)?;

let links = visitor
.links
.into_iter()
.filter(|l| re.is_match(&l.address) && !cs_re.is_match(&l.address));
.filter(|l| re.is_match(&l.address) && !exceptions.is_match(&l.address));

for Link { line_start, .. } in links {
ctx.report(Snippet {
Expand Down
33 changes: 20 additions & 13 deletions eipw-lint/tests/lint_markdown_relative_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ header: value1

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny(
"markdown-rel",
RelativeLinks {
exceptions: &[
"^https://(www\\.)?github\\.com/ethereum/consensus-specs/blob/[a-f0-9]{40}/.+$",
],
},
)
.check_slice(None, src)
.run()
.await
Expand All @@ -40,7 +47,7 @@ header: value1

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand Down Expand Up @@ -68,7 +75,7 @@ header: value1

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand Down Expand Up @@ -96,7 +103,7 @@ Hello [hi](/foo)!

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand Down Expand Up @@ -124,7 +131,7 @@ Hello [hi](./foo/bar)!

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand All @@ -147,7 +154,7 @@ Hello [hi][hello]!

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand Down Expand Up @@ -177,7 +184,7 @@ Hello [hi][hello]!

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand All @@ -198,7 +205,7 @@ https://example.com/

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand Down Expand Up @@ -226,7 +233,7 @@ header: value1

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand Down Expand Up @@ -254,7 +261,7 @@ header: value1

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand Down Expand Up @@ -282,7 +289,7 @@ header: value1

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand All @@ -303,7 +310,7 @@ header: value1

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks)
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
Expand All @@ -324,7 +331,7 @@ header: value1

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

0 comments on commit b4c2124

Please sign in to comment.