Skip to content

Commit

Permalink
(platenioGH-35) Implement the getCssClass shortcode
Browse files Browse the repository at this point in the history
This commit implements a helper shortcode, `getCssClass` to be used in
our patch on the `_default/baseof.html` template to retrieve the class
or classes which should be injected into the `body` of a page.

It defaults to returning an empty string.

In this implementation, If the page is a member of a top-level docs
section (`docs` by default, but can be configured to `games` or anything
else via the `Params.BookSection` parameter in the site config), such as
`content/docs/my_game/rules.md` or `content/games/my_game/rules.md`, then:

1. If the `_index.md` for that section does **not** define `cssClass` in
its frontmatter, then the default CSS class injected for that section is
the URL segment for that section; in this example, `my_game`.

2. If the `_index.md` for that section **does** define `cssClass` in its
frontmatter, then the CSS class injected for that section is the value
of that parameter. This can be used to inject multiple classes, each of
them separated by a single space.

In this current implementation, other top-level pages and sub-sections of
a given section may not override or append their own class injections.
  • Loading branch information
binarystargames authored and michaeltlombardi committed Feb 11, 2022
1 parent ae5d24f commit 1578e91
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions layouts/partials/platen/getCssClass.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{/*
This injects a class based on either cssClass in the _index.md or the filepath.
(The cssClass override is especially useful when the filepath name isn't a valid CSS class name)
*/}}
{{- $injectedClass := "" -}}
{{- $currentLink := .Permalink -}}
{{- $currentParams := .Params -}}
{{- $bookSection := default "docs" .Site.Params.BookSection -}}
{{- if eq $bookSection "*" -}}
{{- $bookSection = "/" -}}
{{- end -}}
{{- with .Site.GetPage $bookSection -}}
{{- range (where .Pages "Params.bookhidden" "ne" true) -}}
{{- $page := . -}}
{{- $filePath := replace .File.Path "\\" "/" -}}
{{- $parts := split $filePath "/" -}}
{{- $section := index $parts 1 -}}
{{- $section = replace $section ".md" "" -}}
{{- if in $currentLink $section -}}
{{- with $currentParams.cssClass -}}
{{- $injectedClass = $currentParams.cssClass -}}
{{- else -}}
{{- with $page.Params.cssClass -}}
{{- $injectedClass = $page.Params.cssClass -}}
{{- else -}}
{{- $injectedClass = $section -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- return $injectedClass -}}

0 comments on commit 1578e91

Please sign in to comment.