From 03d74ddac8fc10565a5f644c00990e774291a3a7 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 20 Dec 2023 23:43:58 -0600 Subject: [PATCH] github fixes and responsive search bar --- src/engines/postsearch/github.rs | 40 ++++++++++++++++++++------------ src/web/assets/style.css | 5 +++- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/engines/postsearch/github.rs b/src/engines/postsearch/github.rs index 40073ef..6db77ea 100644 --- a/src/engines/postsearch/github.rs +++ b/src/engines/postsearch/github.rs @@ -19,15 +19,16 @@ pub fn request(response: &Response) -> Option { pub fn parse_response(body: &str) -> Option { let dom = Html::parse_document(body); - let url = Url::join( - &Url::parse("https://github.com").unwrap(), - dom.select(&Selector::parse("main #repository-container-header a").unwrap()) - .next()? - .value() - .attr("href")?, - ) - .ok()? - .to_string(); + let url_relative = dom + .select( + &Selector::parse("main #repository-container-header strong[itemprop='name'] > a") + .unwrap(), + ) + .next()? + .value() + .attr("href")?; + let url = format!("https://github.com{url_relative}"); + let readme = dom.select(&Selector::parse("article").unwrap()).next()?; let readme_html = readme.inner_html().trim().to_string(); @@ -40,12 +41,21 @@ pub fn parse_response(body: &str) -> Option { .to_string(); let readme_dom = Html::parse_fragment(&readme_html); - let title_el = readme_dom.select(&Selector::parse("h1").unwrap()).next()?; - let title_html = title_el.html().trim().to_string(); - if readme_html.starts_with(&title_html) { - readme_html = readme_html[title_html.len()..].to_string(); - } - let title = title_el.text().collect::(); + let title = if let Some(title_el) = readme_dom.select(&Selector::parse("h1").unwrap()).next() { + let title_html = title_el.html().trim().to_string(); + if readme_html.starts_with(&title_html) { + readme_html = readme_html[title_html.len()..].to_string(); + } + title_el.text().collect::() + } else { + dom.select( + &Selector::parse("main #repository-container-header strong[itemprop='name'] > a") + .unwrap(), + ) + .next()? + .text() + .collect::() + }; Some(format!( r#"

{title}

diff --git a/src/web/assets/style.css b/src/web/assets/style.css index c6d49ed..706ec40 100644 --- a/src/web/assets/style.css +++ b/src/web/assets/style.css @@ -65,9 +65,12 @@ h1 { /* header */ .search-form { margin-bottom: 1rem; + display: flex; + gap: 0.5rem; } #search-input { - width: 20em; + max-width: 30em; + flex: 1; } /* search result */