From 000a99a3fcbe313bf3a438786ea20357732e0960 Mon Sep 17 00:00:00 2001 From: prototypa Date: Mon, 7 Nov 2022 13:46:58 -0500 Subject: [PATCH] Add basic Social Share buttons --- src/components/atoms/SocialShare.astro | 21 ++++++++++++++++ src/components/blog/SinglePost.astro | 8 ++++--- src/components/core/BasicScripts.astro | 33 ++++++++++++++++++++++++++ src/pages/[...blog]/[slug].astro | 5 ++-- 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/components/atoms/SocialShare.astro diff --git a/src/components/atoms/SocialShare.astro b/src/components/atoms/SocialShare.astro new file mode 100644 index 0000000..ef9b104 --- /dev/null +++ b/src/components/atoms/SocialShare.astro @@ -0,0 +1,21 @@ +--- +import { Icon } from 'astro-icon'; +const { text, url, class: className = "inline-block" } = Astro.props; +--- + +
+ Share: + + + + + +
diff --git a/src/components/blog/SinglePost.astro b/src/components/blog/SinglePost.astro index ff20dfb..085b927 100644 --- a/src/components/blog/SinglePost.astro +++ b/src/components/blog/SinglePost.astro @@ -1,10 +1,11 @@ --- import Picture from '~/components/core/Picture.astro'; import PostTags from '~/components/atoms/Tags.astro'; +import SocialShare from '~/components/atoms/SocialShare.astro'; import { getFormattedDate } from '~/utils/utils'; -const { post } = Astro.props; +const { post, canonical } = Astro.props; ---
@@ -41,8 +42,9 @@ const { post } = Astro.props; > {post.Content ? : } -
- +
+ +
diff --git a/src/components/core/BasicScripts.astro b/src/components/core/BasicScripts.astro index efa8072..d325267 100644 --- a/src/components/core/BasicScripts.astro +++ b/src/components/core/BasicScripts.astro @@ -37,6 +37,39 @@ document.documentElement.classList.toggle('dark'); localStorage.theme = document.documentElement.classList.contains('dark') ? 'dark' : 'light'; }); + + attachEvent('[data-aw-social-share]', 'click', function (elem) { + const network = elem.getAttribute('data-aw-social-share'); + const url = encodeURIComponent(elem.getAttribute('data-aw-url')); + const text = encodeURIComponent(elem.getAttribute('data-aw-text')); + + let href; + switch (network) { + case 'facebook': + href = `https://www.facebook.com/sharer.php?u=${url}`; + break; + case 'twitter': + href = `https://twitter.com/intent/tweet?url=${url}&text=${text}`; + break; + case 'linkedin': + href = `https://www.linkedin.com/shareArticle?mini=true&url=${url}&title=${text}`; + break; + case 'whatsapp': + href = `https://wa.me/?text=${text}%20${url}`; + break; + case "mail": + href = `mailto:?subject=%22${text}%22&body=${text}%20${url}`; + break; + + default: + return; + } + + const newlink = document.createElement('a'); + newlink.target = '_blank'; + newlink.href = href; + newlink.click(); + }); }; window.onpageshow = function () { const elem = document.querySelector('[data-aw-toggle-menu]'); diff --git a/src/pages/[...blog]/[slug].astro b/src/pages/[...blog]/[slug].astro index f54800a..f103423 100644 --- a/src/pages/[...blog]/[slug].astro +++ b/src/pages/[...blog]/[slug].astro @@ -23,11 +23,12 @@ export async function getStaticPaths() { } const { post } = Astro.props; +const canonical = post.canonical || getCanonical(getPermalink(post.slug, 'post')) const meta = { title: `${post.title} — ${SITE.name}`, description: post.description, - canonical: post.canonical || getCanonical(getPermalink(post.slug, 'post')), + canonical: canonical, image: await findImage(post.image), ogTitle: post.title, ogType: 'article', @@ -35,5 +36,5 @@ const meta = { --- - +