github fixes and responsive search bar

This commit is contained in:
mat 2023-12-20 23:43:58 -06:00
parent 60914e6e7f
commit 03d74ddac8
2 changed files with 29 additions and 16 deletions

View File

@ -19,15 +19,16 @@ pub fn request(response: &Response) -> Option<reqwest::RequestBuilder> {
pub fn parse_response(body: &str) -> Option<String> {
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())
let url_relative = dom
.select(
&Selector::parse("main #repository-container-header strong[itemprop='name'] > a")
.unwrap(),
)
.next()?
.value()
.attr("href")?,
)
.ok()?
.to_string();
.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<String> {
.to_string();
let readme_dom = Html::parse_fragment(&readme_html);
let title_el = readme_dom.select(&Selector::parse("h1").unwrap()).next()?;
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();
}
let title = title_el.text().collect::<String>();
title_el.text().collect::<String>()
} else {
dom.select(
&Selector::parse("main #repository-container-header strong[itemprop='name'] > a")
.unwrap(),
)
.next()?
.text()
.collect::<String>()
};
Some(format!(
r#"<a href="{url}"><h1>{title}</h1></a>

View File

@ -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 */