Skip to content

Commit 77f5b07

Browse files
Merge pull request #11 from theNewDynamic/10-nested-objects
Add depth to import strings [WIP]
2 parents 27a9135 + 2b95697 commit 77f5b07

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

partials/private/import.html

+24-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
{{ with partialCached "tnd-netlifycms/private/import" . . }}
1616
{{ end }}
1717
*/}}
18-
{{/* regex101.com > https://regex101.com/r/TFA6WQ/1 */}}
19-
{{ $pattern := "^([a-zA-Z]*) ([a-zA-Z/_-]*)$"}}
20-
{{ $string_pattern := "^import ([a-zA-Z]*) ([a-zA-Z/_-]*)$" }}
18+
{{/* regex101.com > https://regex101.com/r/mIrmn0/1 */}}
19+
{{ $pattern := "^([a-zA-Z]*) ([.a-zA-Z/_-]*)$"}}
20+
{{ $string_pattern := "^import ([a-zA-Z]*) ([.a-zA-Z/_-]*)$" }}
2121
{{/* Return variable, by default, will be the passed context */}}
2222
{{ $return := . }}
2323
{{ $type := false }}
@@ -59,13 +59,32 @@
5959
{{ if and $type $file }}
6060
{{/* We look for the matching data file located in `data/netlifycms/{Type}/{File}.yaml` */}}
6161
{{ with index site.Data.netlifycms (pluralize $type) }}
62-
{{ with index . $file }}
62+
{{ $with := index . $file }}
63+
{{/* If we find a "." in the File string, it means user is trying to access a nested value.
64+
We'll split the string and check for two nesting level max
65+
*/}}
66+
{{ $file_split := split $file "." }}
67+
{{ if eq (len $file_split) 2 }}
68+
{{/* We have something like `import fields greeting.spanish */}}
69+
{{ $with = index . (index $file_split 0) (index $file_split 1) }}
70+
{{ else if eq (len $file_split) 3 }}
71+
{{/* We have something like `import fields greeting.spanish.singular */}}
72+
{{ $with = index . (index $file_split 0) (index $file_split 1) (index $file_split 2) }}
73+
{{ end }}
74+
75+
{{ with $with }}
6376
{{ $return = . }}
6477
{{ with $extend }}
6578
{{ $return = merge $return . }}
6679
{{ end }}
6780
{{ else }}
68-
{{ partial "tnd-netlifycms/warn" (printf "We couldn't find %s/%s" $type $file) }}
81+
{{ $message := printf "We couldn't find %s/%s" $type $file }}
82+
{{ with $file_split }}
83+
{{ if gt . 2 }}
84+
{{ $message = print $message " because object depth is limited to 2" }}
85+
{{ end }}
86+
{{ end }}
87+
{{ partial "tnd-netlifycms/warn" $message }}
6988
{{ end }}
7089
{{ else }}
7190
{{ partial "tnd-netlifycms/warn" (printf "We couldn't find Type %s" $type) }}

0 commit comments

Comments
 (0)