From a543d2e0680e94eeabd8423b74dd0e2775f753c9 Mon Sep 17 00:00:00 2001 From: Regis Philibert Date: Thu, 10 Jun 2021 11:23:28 -0400 Subject: [PATCH 1/2] Add depth to import strings WIP Enable dots for nested objects: `- import fields myfield.subkey`. limited to 1 level for now. Fixes #10 --- partials/private/import.html | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/partials/private/import.html b/partials/private/import.html index ea15f66..d9de40d 100644 --- a/partials/private/import.html +++ b/partials/private/import.html @@ -15,9 +15,9 @@ {{ with partialCached "tnd-netlifycms/private/import" . . }} {{ end }} */}} -{{/* regex101.com > https://regex101.com/r/TFA6WQ/1 */}} -{{ $pattern := "^([a-zA-Z]*) ([a-zA-Z/_-]*)$"}} -{{ $string_pattern := "^import ([a-zA-Z]*) ([a-zA-Z/_-]*)$" }} +{{/* regex101.com > https://regex101.com/r/mIrmn0/1 */}} +{{ $pattern := "^([a-zA-Z]*) ([.a-zA-Z/_-]*)$"}} +{{ $string_pattern := "^import ([a-zA-Z]*) ([.a-zA-Z/_-]*)$" }} {{/* Return variable, by default, will be the passed context */}} {{ $return := . }} {{ $type := false }} @@ -59,13 +59,24 @@ {{ if and $type $file }} {{/* We look for the matching data file located in `data/netlifycms/{Type}/{File}.yaml` */}} {{ with index site.Data.netlifycms (pluralize $type) }} - {{ with index . $file }} + {{ $with := index . $file }} + {{ $file_split := split $file "." }} + {{ if eq (len $file_split) 2 }} + {{ $with = index . (index $file_split 0) (index $file_split 1) }} + {{ end }} + {{ with $with }} {{ $return = . }} {{ with $extend }} {{ $return = merge $return . }} {{ end }} {{ else }} - {{ partial "tnd-netlifycms/warn" (printf "We couldn't find %s/%s" $type $file) }} + {{ $message := printf "We couldn't find %s/%s" $type $file }} + {{ with split $file "." }} + {{ if gt . 2 }} + {{ $message = print $message " because object depth is limited to 2" }} + {{ end }} + {{ end }} + {{ partial "tnd-netlifycms/warn" $message }} {{ end }} {{ else }} {{ partial "tnd-netlifycms/warn" (printf "We couldn't find Type %s" $type) }} From 2b95697cf56a4cd402babe142dc9997fc6772e1c Mon Sep 17 00:00:00 2001 From: Regis Philibert Date: Wed, 16 Jun 2021 14:43:19 -0400 Subject: [PATCH 2/2] Add second nesting level --- partials/private/import.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/partials/private/import.html b/partials/private/import.html index d9de40d..3793227 100644 --- a/partials/private/import.html +++ b/partials/private/import.html @@ -60,10 +60,18 @@ {{/* We look for the matching data file located in `data/netlifycms/{Type}/{File}.yaml` */}} {{ with index site.Data.netlifycms (pluralize $type) }} {{ $with := index . $file }} + {{/* If we find a "." in the File string, it means user is trying to access a nested value. + We'll split the string and check for two nesting level max + */}} {{ $file_split := split $file "." }} {{ if eq (len $file_split) 2 }} + {{/* We have something like `import fields greeting.spanish */}} {{ $with = index . (index $file_split 0) (index $file_split 1) }} + {{ else if eq (len $file_split) 3 }} + {{/* We have something like `import fields greeting.spanish.singular */}} + {{ $with = index . (index $file_split 0) (index $file_split 1) (index $file_split 2) }} {{ end }} + {{ with $with }} {{ $return = . }} {{ with $extend }} @@ -71,7 +79,7 @@ {{ end }} {{ else }} {{ $message := printf "We couldn't find %s/%s" $type $file }} - {{ with split $file "." }} + {{ with $file_split }} {{ if gt . 2 }} {{ $message = print $message " because object depth is limited to 2" }} {{ end }}