From 3b2eb2552a953d06d074b4ffcb9102156631326b Mon Sep 17 00:00:00 2001 From: Mikita Bykau Date: Sat, 18 Nov 2023 22:44:28 +0100 Subject: [PATCH] Open-Graph-meta (#44) --- index.html | 17 +++++++++++++---- src/components/meta.rs | 22 ++++++++++++++++++++-- src/pages/author.rs | 6 +++++- src/pages/post.rs | 2 ++ src/utils/head.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 80 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 79acbe9..331567d 100644 --- a/index.html +++ b/index.html @@ -6,16 +6,25 @@ + + + - + + + + + + + - - - + + +
diff --git a/src/components/meta.rs b/src/components/meta.rs index 6d6d8db..30d3c90 100644 --- a/src/components/meta.rs +++ b/src/components/meta.rs @@ -5,6 +5,8 @@ use crate::utils::head::*; #[derive(PartialEq, Properties, Clone)] pub struct MetaProps { + #[prop_or_default] + pub r#type: String, #[prop_or_default] pub title: String, #[prop_or_default] @@ -12,17 +14,22 @@ pub struct MetaProps { #[prop_or_default] pub keywords: String, #[prop_or_default] + pub image: String, + #[prop_or_default] pub noindex: bool, } #[function_component(Meta)] pub fn meta(props: &MetaProps) -> Html { let MetaProps { + r#type, title, description, keywords, + image, noindex, } = props.clone(); + let r#type = r#type.none_if_empty().unwrap_or("website".to_owned()); let title = title .none_if_empty() .map(|t| format!("{} - {}", t, crate::TITLE)) @@ -33,17 +40,28 @@ pub fn meta(props: &MetaProps) -> Html { let keywords = keywords .none_if_empty() .unwrap_or(crate::KEYWORDS.to_owned()); - let robots = if noindex { "noindex" } else { "all" }.to_string(); + let robots = if noindex { "noindex" } else { "all" }.to_owned(); + set_meta(MetaTag::OpenGraph(OpenGraph::Type), &r#type); set_title(&title); + set_meta(MetaTag::OpenGraph(OpenGraph::Title), &title); set_meta(MetaTag::Description, &description); + set_meta(MetaTag::OpenGraph(OpenGraph::Description), &description); set_meta(MetaTag::Keywords, &keywords); + set_meta(MetaTag::OpenGraph(OpenGraph::Image), &image); set_meta(MetaTag::Robots, &robots); + set_meta( + MetaTag::OpenGraph(OpenGraph::SiteName), + &crate::TITLE.to_owned(), + ); html! { <> - + + + + } } diff --git a/src/pages/author.rs b/src/pages/author.rs index 7f9fbef..473b9ed 100644 --- a/src/pages/author.rs +++ b/src/pages/author.rs @@ -27,7 +27,11 @@ pub fn author(props: &AuthorProps) -> Html { component={ |author: Option| html! { <> if let Some(author) = &author { - + } else { } diff --git a/src/pages/post.rs b/src/pages/post.rs index 5f85c3c..efaf4fb 100644 --- a/src/pages/post.rs +++ b/src/pages/post.rs @@ -44,9 +44,11 @@ pub fn post(props: &PostProps) -> Html { <> if let Some(post) = post.as_ref() { } else { diff --git a/src/utils/head.rs b/src/utils/head.rs index e3d81aa..e78c550 100644 --- a/src/utils/head.rs +++ b/src/utils/head.rs @@ -10,18 +10,50 @@ pub fn html_document() -> HtmlDocument { document().unchecked_into::() } +#[derive(Clone, Copy)] +pub enum OpenGraph { + Title, + Description, + Type, + Image, + SiteName, +} + +impl std::fmt::Display for OpenGraph { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + OpenGraph::Title => write!(f, "og:title"), + OpenGraph::Description => write!(f, "og:description"), + OpenGraph::Type => write!(f, "og:type"), + OpenGraph::Image => write!(f, "og:image"), + OpenGraph::SiteName => write!(f, "og:site_name"), + } + } +} + #[derive(Clone, Copy)] pub enum MetaTag { Description, Keywords, + OpenGraph(OpenGraph), Robots, } +impl MetaTag { + fn key(&self) -> &'static str { + match self { + MetaTag::Description | MetaTag::Keywords | MetaTag::Robots => "name", + MetaTag::OpenGraph(_) => "property", + } + } +} + impl std::fmt::Display for MetaTag { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { MetaTag::Description => write!(f, "description"), MetaTag::Keywords => write!(f, "keywords"), + MetaTag::OpenGraph(open_graph) => open_graph.fmt(f), MetaTag::Robots => write!(f, "robots"), } } @@ -30,7 +62,14 @@ impl std::fmt::Display for MetaTag { #[cfg(feature = "client")] fn meta_element(meta_type: MetaTag) -> Element { html_document() - .query_selector(format!("meta[name=\"{name}\"]", name = meta_type.to_string()).as_str()) + .query_selector( + format!( + "meta[{key}=\"{value}\"]", + key = meta_type.key(), + value = meta_type.to_string() + ) + .as_str(), + ) .ok() .flatten() .unwrap()