Refactor social service logic

Update README

Fixes #422
This commit is contained in:
Regis Philibert 2021-06-15 11:47:37 -04:00
parent 43b6ba6ae2
commit 26cf73029a
14 changed files with 230 additions and 60 deletions

View File

@ -123,6 +123,66 @@ This theme includes a shortcode for a contact form that you can add to any page
{{< form-contact action="https://formspree.io/your@email.com" >}}
```
### Social Follow + Share
The theme automatically adds "Follow" link icons to the header and footer and "Share" link icons to pages unless `disable_share` site parameter is set to true. Each built-in services sports a label, an icon and a color.
In order to register a service to be used, user must add an `ananke_socials` parameter to its project configuration file and list them through it in the desired order. Each entry must bear a
- name*: It matches the built-in service reference (Ex: twitter, github)
- url*: The url of the handle's profile on the service (Ex: https://twitter.com/theNewDynamic, https://github.com/
theNewDynamic)
```yaml
params:
ananke_socials:
- name: twitter
url: https://twitter.com/theNewDynamic
- name: github
url: https://github.com/theNewDynamic
```
If user needs to overwrite default `color` and `label` of the service, they simply need to append the following to the entry:
- label: The displayed name of the service to be used to popuplate `[title]` attributes and read-only. (Ex: Twitter, GitHub)
- color: Used for styling purposes. (Ex: '#1da1f2', '#6cc644')
```yaml
params:
ananke_socials:
- name: twitter
url: https://twitter.com/theNewDynamic
label: TND Twitter
- name: github
url: https://github.com/theNewDynamic
label: TND GitHub Account
color: '#ff6800'
```
#### Social Icons Customization
On top of easily customizing the built-in services' label and color, user can overwrite their icon by adding an svg file at `/assets/ananke/socials` with a filename matching the service's name.
For example, in order to use your own GitHub icon, simply add an svg file at `/assets/ananke/socials/github.svg`
#### Built-in Services
Here is the list of built-in services. Those marked with an `*` are also part of the "Share" module.
- twitter*
- instagram
- youtube
- github
- gitlab
- keybase
- linkedin*
- medium
- mastodon
- slack
- stackoverflow
- facebook*
- rss
#### Complement
In order to add an unkown service (absent from the list above), you simply need to add all three settings to `ananke_socials`: name, url, label, color, and optionally add an icon file matching the `name` to the `assets/ananke/socials` directory. In the absence of an icon, the theme will print the service's label.
### Update font or body classes
The theme is set, by default, to use a near-white background color and the "Avenir" or serif typeface. You can change these in your config file with the `body_classes` parameter, like this:

View File

@ -30,18 +30,11 @@ enableRobotsTXT = true
favicon = ""
site_logo = ""
description = "The last theme you'll ever need. Maybe."
facebook = ""
twitter = "https://twitter.com/GoHugoIO"
instagram = ""
youtube = ""
github = ""
gitlab = ""
linkedin = ""
mastodon = ""
slack = ""
stackoverflow = ""
rss = ""
# choose a background color from any on this page: http://tachyons.io/docs/themes/skins/ and preface it with "bg-"
background_color_class = "bg-black"
featured_image = "/images/gohugo-default-sample-hero-image.jpg"
recent_posts_number = 2
[[params.ananke_socials]]
name = "twitter"
url = "https://twitter.com/GoHugoIO"

View File

@ -0,0 +1,45 @@
{{/*
socials/Get
Returns the list of services registered by the user complemented by the built-in service default data if found.
@author @regisphilibert
@context Any (.)
@access public
@returns Slice of Maps
- String (.name)
String (.url)
String (.label)
String (.color)?
Bool (.share)?
@uses
- partial
@example - Go Template
{{ with partialCached "socials/Get" context context }}
{{ something = . }}
{{ end }}
*/}}
{{ $socials := slice }}
{{ with partial "func/socials/GetRegisteredServices" "GetRegisteredServices" }}
{{ range . }}
{{ $service := . }}
{{/* We fetch the default data and add it to service map if found */}}
{{ with partialCached "func/socials/GetServiceData" .name .name }}
{{ $service = merge . $service }}
{{ end }}
{{/* We fetch the icon and add it to service map fi found */}}
{{ with partialCached "func/socials/GetServiceIcon" .name .name }}
{{ $service = $service | merge (dict "icon" . ) }}
{{ end }}
{{/* In case no label is provided (on a non-built-in service) we add the .name as label to the service map */}}
{{ with .label }}{{ else }}
{{ $service = $service | merge (dict "label" $service.name ) }}
{{ end }}
{{ $socials = $socials | append $service }}
{{ end }}
{{ end }}
{{ return $socials }}

View File

@ -1,3 +1,29 @@
{{/*
GetBuiltInServicesData
Returns a map whose keys stores the map of defaults data for any built-in service.
We use a key to easily grab the data with `index $that github`
@author @regisphilibert
@context Any (.)
@access public
@returns Map
## Contributors can add a built-in service
1. Adding it to the following map with the following format:
```yaml
# [...]
shinyandnew:
label: Shiny And New
color: '#cccccc'
```
2. Edit README file with service.
*/}}
{{ return (`
facebook:
share: true

View File

@ -1,21 +0,0 @@
{{ $services := slice }}
{{ $legacy_api_services := slice "facebook" "twitter" "instagram" "youtube" "github" "gitlab" "keybase" "linkedin" "medium" "mastodon" "slack" "stackoverflow" "rss" }}
{{ $user_using_legacy := false }}
{{ range $service := $legacy_api_services }}
{{ with index site.Params . }}
{{ $user_using_legacy = true }}
{{ $services = $services | append (dict "name" $service "url" .) }}
{{ end }}
{{ end }}
{{ with site.Params.ananke_socials }}
{{ range $service := . }}
{{ with .name }}
{{ $services = $services | append $service }}
{{ end }}
{{ end }}
{{ end }}
{{ return $services }}

View File

@ -0,0 +1,45 @@
{{/*
socials/GetRegisteredServices
Retrieves the list of user registered services.
Support legacy settings (root of params with service name as key)
@author @regisphilibert
@context Any (.)
@access private
@returns Slice of Maps
- String (.name)
String (.url)
String (.label)?
String (.color)?
*/}}
{{ $registered_services := slice }}
{{/* We first look for legacy settings that lives at the root of the site.Params as such (github: https://github.com/
theNewDynamic) and them to the list with key as .name and value as .url */}}
{{ $user_using_legacy := false }}
{{ $legacy_api_services := slice "facebook" "twitter" "instagram" "youtube" "github" "gitlab" "keybase" "linkedin" "medium" "mastodon" "slack" "stackoverflow" "rss" }}
{{ range $name := $legacy_api_services }}
{{ with $url := index site.Params . }}
{{/* If we can find a parameter matching the key with a set value, we add it with proper name and url */}}
{{/* We also note that user is using legacy for potential potential deprecation warnings */}}
{{ $user_using_legacy = true }}
{{ $registered_services = $registered_services | append (dict "name" $name "url" $url) }}
{{ end }}
{{ end }}
{{/* Then we go through the current way of registering services as per referenced in README */}}
{{ with site.Params.ananke_socials }}
{{ range $service := . }}
{{/* Only if the service has a .name, we add it all its keys to the slice of registered services */}}
{{ with .name }}
{{ $registered_services = $registered_services | append $service }}
{{ end }}
{{ end }}
{{ end }}
{{ return $registered_services }}

View File

@ -1,8 +1,27 @@
{{ $service_data := dict }}
{{ $services_data := partialCached "func/socials/GetServicesData" "socials/GetServicesData" }}
{{/*
socials/GetServiceDefaults
Returns the defaults of any given service as stored in GetBuildInServicesDefaults
{{ with index $services_data $ }}
@author @regisphilibert
@context String (.)
@access private
@returns Map
- String (.label)
String (.color)
@uses
- func/socials/GetBuiltInServicesDefaults
*/}}
{{ $service_data := dict }}
{{ with partialCached "func/socials/GetBuiltInServicesDefaults" "socials/GetBuiltInServicesDefaults" }}
{{/* If the passed context string (held in $) is found as a key of the map returned by the above returning partial
We store it in the returning variable */}}
{{ with index . $ }}
{{ $service_data = . }}
{{ end }}
{{ end }}
{{ return $service_data }}

View File

@ -1,7 +1,7 @@
{{/*
GetServiceIcon
User can overwrite/complement in assets/ananke/socials
User can overwrite/complement in assets/ananke/socials/{service_name}.svg
@author @regisphilibert

View File

@ -1,18 +0,0 @@
{{/*
*/}}
{{ $socials := slice }}
{{ with partial "func/socials/GetProjectServices" "GetProjectServices" }}
{{ range . }}
{{ $service := . }}
{{ with partialCached "func/socials/GetServiceData" .name .name }}
{{ $service = merge . $service }}
{{ end }}
{{ with partialCached "func/socials/GetServiceIcon" .name .name }}
{{ $service = $service | merge (dict "icon" . ) }}
{{ end }}
{{ $socials = $socials | append $service }}
{{ end }}
{{ end }}
{{ return $socials }}

View File

@ -22,6 +22,25 @@
{{ end }}
{{ end }}
{{ with partialCached "func/socials/Get" "socials/Get" }}
{{ $socials_rules := slice }}
{{ range $service := . }}
{{ with .color }}
{{ $rule := printf `
.ananke-socials a.%s:hover {
color: %s
}` $service.name $service.color }}
{{ $socials_rules = $socials_rules | append $rule }}
{{ end }}
{{ end }}
{{ with $socials_rules }}
{{ $socials_rules = delimit . "" }}
{{ $socials_css := $socials_rules | resources.FromString "ananke/css/generated_socials.css" }}
{{ $assets_to_concat = $assets_to_concat | append $socials_css }}
{{ end }}
{{ end }}
{{/* We look for any custom css files registered by the user under `site.params.custom_css and if found in the theme's
css asset directory we add to aforementioned slice */}}
{{ with site.Params.custom_css }}

View File

@ -1,4 +1,4 @@
{{ $socials := partialCached "func/socials/GetSocials" "socials/GetSocials" }}
{{ $socials := partialCached "func/socials/Get" "socials/Get" }}
<div class="ananke-socials">
{{ range $socials }}
<a href="{{ .url }}" target="_blank" class="{{ .name }} ananke-social-link link-transition stackoverflow link dib z-999 pt3 pt0-l mr1" title="{{ .label }} link" rel="noopener" aria-label="follow on {{ .label }}——Opens in a new window">

View File

@ -3,13 +3,13 @@
{{ $facebook_href := printf "https://www.facebook.com/sharer.php?u=%s" $url }}
{{ $twitter_href := printf "https://twitter.com/share?url=%s&text=%s" $url $title }}
{{ with .Site.Social.twitter }}
{{ with site.Social.twitter }}
{{ $twitter_href = printf "%s&via=%s" $twitter_href . }}
{{ end }}
{{ $linkedin_href := printf "https://www.linkedin.com/shareArticle?mini=true&url=%s&title=%s" $url $title }}
{{ $hrefs := dict "facebook" $facebook_href "twitter" $twitter_href "linkedin" $linkedin_href }}
{{ $services := where (partialCached "func/socials/GetSocials" "socials/GetSocials") "share" true }}
{{ $services := where (partialCached "func/socials/Get" "socials/Get") "share" true }}
{{ if not .Params.disable_share }}
<div id="sharing" class="mt3 ananke-socials">
{{ range $service := $services }}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
{"Target":"ananke/css/main.min.css","MediaType":"text/css","Data":{}}