generated from hugomods/template-mod
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow extending configurations for collections
- Loading branch information
Showing
6 changed files
with
274 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,2 @@ | ||
{{- $params := .Site.Params.decap_cms -}} | ||
{{- $config := newScratch -}} | ||
{{/* Reading settings from configuration. */}} | ||
{{- $keys := slice | ||
"backend" "i18n" "local_backend" "locale" "logo_url" "media_folder" | ||
"public_folder" "publish_mode" "search" | ||
-}} | ||
{{- range $keys -}} | ||
{{- $key := . -}} | ||
{{- if isset $params $key -}} | ||
{{- $config.Set $key (index $params $key) -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{/* Transform collections. */}} | ||
{{- with index $params "collections" -}} | ||
{{- range . -}} | ||
{{- $config.Add "collections" (slice .) }} | ||
{{- end -}} | ||
{{- end -}} | ||
{{/* Set the site URL. */}} | ||
{{- $config.Set "site_url" site.BaseURL -}} | ||
{{/* Use site language as the locale if not set. */}} | ||
{{- if not ($config.Get "locale") }} | ||
{{- $locale := partialCached "decap-cms/functions/locale" . }} | ||
{{- $config.Set "locale" $locale }} | ||
{{- end }} | ||
{{/* Transform Logo URL. */}} | ||
{{- with $config.Get "logo_url" }} | ||
{{- $logoURL := urls.Parse . }} | ||
{{- if eq $logoURL.Scheme "" }} | ||
{{- with resources.Get . }} | ||
{{- $config.Set "logo_url" .Permalink }} | ||
{{- else }} | ||
{{- $config.Set "logo_url" (. | absURL) }} | ||
{{- end }} | ||
{{- else }} | ||
{{- $config.Set "logo_url" . }} | ||
{{- end }} | ||
{{- end }} | ||
{{/* Fill i18n fields if enabled. */}} | ||
{{- with $config.Get "i18n" }} | ||
{{- $i18n := . }} | ||
{{- $locales := partialCached "decap-cms/functions/locales" . }} | ||
{{- if not .locales }} | ||
{{- $i18n = merge $i18n (dict "locales" $locales) }} | ||
{{- end }} | ||
{{- if not .default_locale }} | ||
{{- $i18n = merge $i18n (dict "default_locale" (index $locales 0)) }} | ||
{{- end }} | ||
{{- $config.Set "i18n" $i18n }} | ||
{{- end }} | ||
{{- $config := partial "decap-cms/functions/config" . -}} | ||
{{- transform.Remarshal "yaml" $config.Values | safeHTML -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
{{- $params := .Site.Params.decap_cms -}} | ||
{{- $config := newScratch -}} | ||
{{/* Reading settings from configuration. */}} | ||
{{- $keys := slice | ||
"backend" "i18n" "local_backend" "locale" "logo_url" "media_folder" | ||
"public_folder" "publish_mode" "search" | ||
-}} | ||
{{- range $keys -}} | ||
{{- $key := . -}} | ||
{{- if isset $params $key -}} | ||
{{- $config.Set $key (index $params $key) -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- $configs := default dict (index $params "_configs") -}} | ||
|
||
{{/* Transform collections. */}} | ||
{{- with index $params "collections" -}} | ||
{{- range . -}} | ||
{{- $collection := . -}} | ||
{{- with $collection.fields -}} | ||
{{- if reflect.IsSlice . -}} | ||
{{- $fields := dict -}} | ||
{{- range . -}} | ||
{{- $fields = merge $fields (dict .name .) -}} | ||
{{- end -}} | ||
{{- $collection = merge $collection (dict "fields" $fields) -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{/* Extends configurations. */}} | ||
{{- $extends := slice -}} | ||
{{- with index $collection "_extends" -}} | ||
{{- range $weight, $extendName := . -}} | ||
{{- with index $configs . -}} | ||
{{/* Assign the weight to fields for sorting. */}} | ||
{{- $extend := . -}} | ||
{{- with $extend.fields -}} | ||
{{- range $name, $field := . -}} | ||
{{- $field = merge $field (dict "weight" (add $weight 1)) -}} | ||
{{- $extend = merge $extend (dict "fields" (dict $name $field)) -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- $extends = $extends | append $extend -}} | ||
{{- else -}} | ||
{{- warnf "[decap-cms] no such extendable configuration: %s." . -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- $extends = $extends | append $collection -}} | ||
{{- $collection = partial "decap-cms/functions/deep-merge" $extends -}} | ||
{{- $collection.Delete "_extends" -}} | ||
{{- with $collection.Get "fields" -}} | ||
{{- if reflect.IsMap . -}} | ||
{{- $weightFields := slice -}} | ||
{{- $unweightFields := slice -}} | ||
{{- range $name, $field := . -}} | ||
{{- with .weight -}} | ||
{{- $weightFields = $weightFields | append $field -}} | ||
{{- else -}} | ||
{{- $unweightFields = $unweightFields | append $field -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- $fields := sort $weightFields "weight" -}} | ||
{{- range sort $unweightFields "name" -}} | ||
{{- $fields = $fields | append . -}} | ||
{{- end }} | ||
{{- $collection.Set "fields" $fields -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- $config.Add "collections" (slice $collection.Values) -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{/* Set the site URL. */}} | ||
{{- $config.Set "site_url" site.BaseURL -}} | ||
{{/* Use site language as the locale if not set. */}} | ||
{{- if not ($config.Get "locale") -}} | ||
{{- $locale := partialCached "decap-cms/functions/locale" . -}} | ||
{{- $config.Set "locale" $locale -}} | ||
{{- end -}} | ||
{{/* Transform Logo URL. */}} | ||
{{- with $config.Get "logo_url" -}} | ||
{{- $logoURL := urls.Parse . -}} | ||
{{- if eq $logoURL.Scheme "" -}} | ||
{{- with resources.Get . -}} | ||
{{- $config.Set "logo_url" .Permalink -}} | ||
{{- else -}} | ||
{{- $config.Set "logo_url" (. | absURL) -}} | ||
{{- end -}} | ||
{{- else -}} | ||
{{- $config.Set "logo_url" . -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{/* Fill i18n fields if enabled. */}} | ||
{{- with $config.Get "i18n" -}} | ||
{{- $i18n := . -}} | ||
{{- $locales := partialCached "decap-cms/functions/locales" . -}} | ||
{{- if not .locales -}} | ||
{{- $i18n = merge $i18n (dict "locales" $locales) -}} | ||
{{- end -}} | ||
{{- if not .default_locale -}} | ||
{{- $i18n = merge $i18n (dict "default_locale" (index $locales 0)) -}} | ||
{{- end -}} | ||
{{- $config.Set "i18n" $i18n -}} | ||
{{- end -}} | ||
{{- return $config -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{{- $rst := newScratch }} | ||
{{/* Walk the collections. */}} | ||
{{- range . }} | ||
{{/* Walk the collection's definitions. */}} | ||
{{- range $key, $items := . }} | ||
{{- with $rst.Get $key }} | ||
{{/* Start deep merging if the key already exists. */}} | ||
{{- $values := . }} | ||
{{- if reflect.IsSlice $values }} | ||
{{/* Start array merging. */}} | ||
{{/* Walk the definitions. */}} | ||
{{- range $items }} | ||
{{- $exist := false }} | ||
{{- $item := . }} | ||
{{- if not (partial "decap-cms/functions/is-field-definition" $item) }} | ||
{{- break }} | ||
{{- end }} | ||
{{- range $k, $v := $values }} | ||
{{- if not (partial "decap-cms/functions/is-field-definition" .) }} | ||
{{- break }} | ||
{{- end }} | ||
{{- if eq $item.name .name }} | ||
{{- $exist = true }} | ||
{{- $former := first $k $values }} | ||
{{- $latter := after (add $k 1) $values }} | ||
{{- $values = $former | append $item }} | ||
{{- range $latter }} | ||
{{- $values = $values | append . }} | ||
{{- end }} | ||
{{- break }} | ||
{{- end }} | ||
{{- end }} | ||
{{- if not $exist }} | ||
{{- $values = $values | append . }} | ||
{{- end }} | ||
{{- end }} | ||
{{- $rst.Set $key $values }} | ||
{{- else }} | ||
{{/* Start map merging. */}} | ||
{{- $rst.Set $key (merge $values $items) }} | ||
{{- end }} | ||
{{- else }} | ||
{{/* Add if it doesn't exist. */}} | ||
{{- $rst.Set $key $items }} | ||
{{- end }} | ||
{{- end }} | ||
{{- end }} | ||
{{- return $rst }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{- $scratch := newScratch }} | ||
{{- range $key, $val := index . 0 }} | ||
{{- $scratch.Set $key $val }} | ||
{{- end }} | ||
{{- return $scratch }} |
7 changes: 7 additions & 0 deletions
7
layouts/partials/decap-cms/functions/is-field-definition.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{{- $v := true }} | ||
{{- if not (reflect.IsMap .) }} | ||
{{- $v = false }} | ||
{{- else if not (isset . "name") }} | ||
{{- $v = false }} | ||
{{- end }} | ||
{{- return $v }} |