Skip to content

Commit

Permalink
Refactor social service logic
Browse files Browse the repository at this point in the history
Update README

Fixes #422
  • Loading branch information
regisphilibert committed Jun 15, 2021
1 parent 43b6ba6 commit 26cf730
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 60 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]" >}}
```

### 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:
Expand Down
17 changes: 5 additions & 12 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
recent_posts_number = 2

[[params.ananke_socials]]
name = "twitter"
url = "https://twitter.com/GoHugoIO"
45 changes: 45 additions & 0 deletions layouts/partials/func/socials/Get.html
Original file line number Diff line number Diff line change
@@ -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 }}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 0 additions & 21 deletions layouts/partials/func/socials/GetProjectServices.html

This file was deleted.

45 changes: 45 additions & 0 deletions layouts/partials/func/socials/GetRegisteredServices.html
Original file line number Diff line number Diff line change
@@ -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 }}
29 changes: 24 additions & 5 deletions layouts/partials/func/socials/GetServiceData.html
Original file line number Diff line number Diff line change
@@ -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

@author @regisphilibert

@context String (.)

{{ with index $services_data $ }}
{{ $service_data = . }}
{{ end }}
@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 }}
2 changes: 1 addition & 1 deletion layouts/partials/func/socials/GetServiceIcon.html
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 0 additions & 18 deletions layouts/partials/func/socials/GetSocials.html

This file was deleted.

19 changes: 19 additions & 0 deletions layouts/partials/func/style/GetMainCSS.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/social-follow.html
Original file line number Diff line number Diff line change
@@ -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">
Expand Down
4 changes: 2 additions & 2 deletions layouts/partials/social-share.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Target":"ananke/css/main.min.css","MediaType":"text/css","Data":{}}

0 comments on commit 26cf730

Please sign in to comment.