From cae88a20ff4ae5db6a2b0e5c10c0bb1bebb3118f Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 15 Jan 2024 01:33:13 -0600 Subject: [PATCH] use regex for matching github repo urls --- src/engines/postsearch/github.rs | 6 ++++-- src/web/assets/style.css | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/engines/postsearch/github.rs b/src/engines/postsearch/github.rs index d631592..42b8de9 100644 --- a/src/engines/postsearch/github.rs +++ b/src/engines/postsearch/github.rs @@ -1,11 +1,11 @@ use scraper::{Html, Selector}; use url::Url; -use crate::engines::{Response, CLIENT}; +use crate::engines::{answer::regex, Response, CLIENT}; pub fn request(response: &Response) -> Option { for search_result in response.search_results.iter().take(8) { - if search_result.url.starts_with("https://github.com/") { + if regex!(r"^https:\/\/github\.com\/[\w-]+\/[\w.-]+$").is_match(&search_result.url) { return Some(CLIENT.get(search_result.url.as_str())); } } @@ -43,6 +43,8 @@ pub fn parse_response(body: &str) -> Option { let mut readme_html = ammonia::Builder::default() .link_rel(None) + .add_allowed_classes("div", &["markdown-alert"]) + .add_allowed_classes("p", &["markdown-alert-title"]) .url_relative(ammonia::UrlRelative::RewriteWithBase( Url::parse("https://github.com").unwrap(), )) diff --git a/src/web/assets/style.css b/src/web/assets/style.css index ad32d4c..a18532d 100644 --- a/src/web/assets/style.css +++ b/src/web/assets/style.css @@ -268,6 +268,13 @@ h1 { font-weight: normal; font-size: 0.8em; } +.infobox-github-readme .markdown-alert { + padding-left: 0.5em; + border-left: 0.25em solid #234; +} +.infobox-github-readme .markdown-alert-title { + font-weight: bold; +} .postsearch-infobox p { margin-bottom: 1em; }