Skip to content

Commit

Permalink
feat: allow extending configurations for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Dec 1, 2023
1 parent e357ef9 commit a6b1b84
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 52 deletions.
110 changes: 109 additions & 1 deletion hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,112 @@ public_folder = "/images/uploads"
search = true
preview_styles = []

# [params.decap_cms.backend]
[params.decap_cms._configs.field_body.fields.body]
name = "body"
label = "Body"
widget = "markdown"

[params.decap_cms._configs.field_categories.fields.categories]
name = "categories"
label = "Categories"
required = false
widget = "list"
summary = "{{fields.category}}"
collapsed = true
minimize_collapsed = true
[params.decap_cms._configs.field_categories.fields.categories.field]
label = "Category"
name = "category"
widget = "string"

[params.decap_cms._configs.field_date.fields.date]
name = "date"
label = "Date"
widget = "datetime"
picker_utc = true

[params.decap_cms._configs.field_description.fields.description]
name = "description"
label = "Description"
widget = "text"

[params.decap_cms._configs.field_draft.fields.draft]
name = "draft"
label = "Draft"
default = true
widget = "boolean"

[params.decap_cms._configs.field_images.fields.images]
name = "images"
label = "Images"
required = false
widget = "list"
summary = "{{fields.image}}"
collapsed = true
minimize_collapsed = true
[params.decap_cms._configs.field_images.fields.images.field]
label = "Image"
name = "image"
widget = "image"

[params.decap_cms._configs.field_keywords.fields.keywords]
name = "keywords"
label = "Keywords"
required = false
widget = "list"
summary = "{{fields.keyword}}"
collapsed = true
minimize_collapsed = true
[params.decap_cms._configs.field_keywords.fields.keywords.field]
label = "Keyword"
name = "keyword"
widget = "string"

[params.decap_cms._configs.field_layout.fields.layout]
name = "layout"
label = "Layout"
required = false
widget = "string"

[params.decap_cms._configs.field_series.fields.series]
name = "series"
label = "Series"
required = false
widget = "list"
summary = "{{fields.series}}"
collapsed = true
minimize_collapsed = true
[params.decap_cms._configs.field_series.fields.series.field]
label = "Series"
name = "series"
widget = "string"

[params.decap_cms._configs.field_slug.fields.slug]
name = "slug"
label = "Slug"
required = false
widget = "string"

[params.decap_cms._configs.field_tags.fields.tags]
name = "tags"
label = "Tags"
required = false
widget = "list"
summary = "{{fields.tag}}"
collapsed = true
minimize_collapsed = true
[params.decap_cms._configs.field_tags.fields.tags.field]
label = "Tag"
name = "tag"
widget = "string"

[params.decap_cms._configs.field_title.fields.title]
name = "title"
label = "Title"
widget = "string"

[params.decap_cms._configs.field_type.fields.type]
name = "type"
label = "Type"
required = false
widget = "string"
52 changes: 1 addition & 51 deletions layouts/_default/index.decapcmsconfig.yaml
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 -}}
104 changes: 104 additions & 0 deletions layouts/partials/decap-cms/functions/config.html
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 -}}
48 changes: 48 additions & 0 deletions layouts/partials/decap-cms/functions/deep-merge.html
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 }}
5 changes: 5 additions & 0 deletions layouts/partials/decap-cms/functions/dict2scratch.html
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 layouts/partials/decap-cms/functions/is-field-definition.html
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 }}

0 comments on commit a6b1b84

Please sign in to comment.