Skip to content

Commit

Permalink
Merge pull request #11 from theNewDynamic/10-nested-objects
Browse files Browse the repository at this point in the history
Add depth to import strings [WIP]
  • Loading branch information
regisphilibert authored Jun 16, 2021
2 parents 27a9135 + 2b95697 commit 77f5b07
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions partials/private/import.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -59,13 +59,32 @@
{{ 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 }}
{{/* 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 }}
{{ $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 $file_split }}
{{ 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) }}
Expand Down

0 comments on commit 77f5b07

Please sign in to comment.