Skip to content

Commit

Permalink
feat: add hashtags to share URLs (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Syralist authored and reuixiy committed Apr 5, 2020
1 parent 9d35969 commit a323e45
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions layouts/partials/components/post-share.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
{{- $images := .Scratch.Add "images" (slice (.Site.Params.siteLogo | absURL)) -}}
{{- $images := (index (.Scratch.Get "images") 0) -}}

{{- $hashtags := newScratch -}}
{{- with .Params.tags -}}
{{- range . -}}
{{- $tag := . -}}
{{- $url := urls.Parse (. | urlize) -}}
{{- $path := $url.Path -}}
{{- with $.Site.GetPage (printf `/tags/%s` $path) -}}
{{- $hashtags.Add "tags" (replace (.LinkTitle | default $tag) " " "") -}}
{{- end -}}
{{- $hashtags.Add "tags" "," -}}
{{- end -}}
{{- end -}}
{{- $hashtags.Set "firsttag" "%23" -}}
{{- $hashtags.Add "firsttag" (index (split ($hashtags.Get "tags") ",") 0) -}}

<div class="post-share">

{{ if .Site.Params.displayShareOnText }}
Expand All @@ -19,7 +34,7 @@
{{ if .Site.Params.shareOnTwitter }}
{{ $icon := (replace .Site.Data.SVG.twitter "icon" "icon twitter-icon") }}
<div class="share-item twitter">
{{ $url := (printf `https://twitter.com/share?url=%s&text=%s&via=%s` .Permalink .Title .Site.Params.siteTwitter) }}
{{ $url := (printf `https://twitter.com/share?url=%s&text=%s&hashtags=%s&via=%s` .Permalink .Title ($hashtags.Get "tags" | default "") .Site.Params.siteTwitter) }}
<a href="{{ $url | safeURL }}" title="{{ i18n "shareOnTitle" }}{{ i18n "twitter" }}" target="_blank" rel="noopener">
{{- $icon | safeHTML -}}
</a>
Expand All @@ -29,7 +44,7 @@
{{ if .Site.Params.shareOnFacebook }}
{{ $icon := (replace .Site.Data.SVG.facebook "icon" "icon facebook-icon") }}
<div class="share-item facebook">
{{ $url := (printf `https://www.facebook.com/sharer/sharer.php?u=%s` .Permalink) }}
{{ $url := (printf `https://www.facebook.com/sharer/sharer.php?u=%s&hashtag=%s` .Permalink ($hashtags.Get "firsttag" | default "")) }}
<a href="{{ $url | safeURL }}" title="{{ i18n "shareOnTitle" }}{{ i18n "facebook" }}" target="_blank" rel="noopener">
{{- $icon | safeHTML -}}
</a>
Expand Down

0 comments on commit a323e45

Please sign in to comment.