Skip to content

Commit

Permalink
Add extend capability
Browse files Browse the repository at this point in the history
  • Loading branch information
regisphilibert committed Aug 19, 2020
1 parent 878574c commit 7ade8f2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 46 deletions.
72 changes: 52 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ module:
## Usage
### The Import statement
```
import collection posts
| type | | file name
```
### Adding to your project

1. Add this module's path to your `module.imports` config array
```
module:
Expand All @@ -56,40 +50,62 @@ outputs:
4. Add your Netlify config data in `data/netlifycms/config.yaml` and use (or not) `import` statements.
5. Add the data files matching your imports statements to the `data/netlifycms` dir under `/collections` and `/fields`

### Example of the base config data file
### Import and Extend data sets

NetlifyCMS uses yaml extensively. In order to avoid a lot of copy/paste the module allow to import a data set stored in `data/netlifycms/**/*.yaml`

It's also possible to extend the data found in the file.

See example below of usage.

### Example of the base config data file

```yaml
# data/netlifycms/config.yaml
backend:
name: git-gateway
branch: easier-netlify-cms
publish_mode: editorial_workflow
# [ ... ]
slug:
encoding: "ascii"
clean_accents: true
sanitize_replacement: "_"
collections:
- import collection pages #--> data/netlifycms/collections/pages.yaml
- import collection events #--> data/netlifycms/collections/pages.yaml
- import collection posts #--> data/netlifycms/collections/posts.yaml
- import: collection posts
extend:
name: updates
label: News Updates
folder: content/updates
```
Above, the Events and Posts collections are stored in the mentioned data file. As we import the data as is, we can use a simple `import` statement.

For updates, we need almost the same settings and fields as the Post collection, except for a few keys that need to be overwritten.
We can use a map with two keys:
- import: For the statement
- extend: The data which will be added/merged on top of the
data set.


### Example of a Collection data file

```yaml
# data/netlifycms/collections/posts.yaml
name: "posts"
label: "Posts"
folder: "content/posts"
name: "events"
label: "Events"
folder: "content/events"
create: true
editor:
preview: false
fields:
- import field title #--> data/netlifycms/fields/title.yaml
- import field authors #--> data/netlifycms/fields/authors.yaml
- import field date #--> data/netlifycms/fields/date.yaml
- import field images #--> data/netlifycms/fields/images.yaml
- import field body #--> data/netlifycms/fields/body.yaml
- import: field time #--> data/netlifycms/fields/time.yaml
extend:
name: start_time
label: Start
- import: field time
extend:
name: end_time
label: End
- {
label: "Tags",
name: "tags",
Expand All @@ -102,7 +118,11 @@ fields:
}
```

### Example of a Field data file
Above we can see that regular handwritten data and import statements, as well as extending data can perfectly coexist among the same array.

It is convenient to create a "base" field like `time.yaml` on which to extend special data. This way, the two fields storing a "Start" and an "End" time can use the same base of settings for date and time formating while retaining their own `name` and `label`.

### Examples of a Field data file

```yaml
# data/netlifycms/fields/title.yaml
Expand All @@ -111,6 +131,18 @@ name: "title"
widget: "string"
```

```yaml
# data/netlifycms/fields/time.yaml
label: "Time"
name: "time"
widget: "datetime"
default: ""
dateFormat: "DD.MM.YYYY" # e.g. 24.12.2021
timeFormat: "HH:mm" # e.g. 21:07
format: "LLL"
pickerUtc: false
```

### Settings

Module settings are set as part of the shared `data/netlifycms/config.yaml` file under the reserved `tnd_netlifycms` map key.
Expand Down
70 changes: 44 additions & 26 deletions partials/private/import.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,59 @@
{{ end }}
*/}}
{{/* regex101.com > https://regex101.com/r/TFA6WQ/1 */}}
{{ $pattern := "^import ([a-zA-Z]*) ([a-zA-Z/_-]*)$" }}

{{ $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 }}
{{ $file := false }}
{{/* We're looking for a string, so we start by eliminating Slices and Maps */}}
{{ if not (reflect.IsMap .) }}
{{ if not (reflect.IsSlice .) }}
{{ $input := false }}
{{ $extend := dict }}
{{/* We're looking for a String or a Map, so we start by eliminating Slices */}}
{{ if not (reflect.IsSlice .) }}
{{ if not (reflect.IsMap .) }}
{{/* We have a string and store it as $input */}}
{{ $input := . }}
{{/* In case that string won't bring anything we emtpty $return so it fails a `with` test. */}}
{{ $input = . }}
{{/* We use the $string_pattern for subsequent findRE */}}
{{ $pattern = $string_pattern }}
{{ else }}
{{/* We have a map. We look for .import and store it as $input */}}
{{ with .import }}
{{ $input = . }}
{{ end }}
{{/* We also look for potential .extend data */}}
{{ with .extend }}
{{ $extend = . }}
{{ end }}
{{ end }}

{{/* We're looking for a properly formated import statement */}}
{{ with findRE $pattern $input }}
{{/* We do have an import statement, we reset $return as we're about to fill it with new data.
If the import statement fails to point to valid data file, we'll return an empty dict */}}
{{ $return = dict }}
{{/* We're looking for a properly formated import statement */}}
{{ with findRE $pattern $input }}
{{/* We isolate the Type from the statement */}}
{{ with replaceRE $pattern "$1" $input }}
{{ $type = . }}
{{ end }}
{{/* We isolate the File from the statement */}}
{{ with replaceRE $pattern "$2" $input }}
{{ $file = . }}
{{ end }}
{{/* With both File and Type... */}}
{{ 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 }}
{{ $return = . }}
{{ else }}
{{ partial "tnd-netlifycms/warn" (printf "We couldn't find %s/%s" $type $file) }}
{{/* We isolate the Type from the statement */}}
{{ with replaceRE $pattern "$1" $input }}
{{ $type = . }}
{{ end }}
{{/* We isolate the File from the statement */}}
{{ with replaceRE $pattern "$2" $input }}
{{ $file = . }}
{{ end }}
{{/* With both File and Type... */}}
{{ 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 }}
{{ $return = . }}
{{ with $extend }}
{{ $return = merge $return . }}
{{ end }}
{{ else }}
{{ partial "tnd-netlifycms/warn" (printf "We couldn't find Type %s" $type) }}
{{ partial "tnd-netlifycms/warn" (printf "We couldn't find %s/%s" $type $file) }}
{{ end }}
{{ else }}
{{ partial "tnd-netlifycms/warn" (printf "We couldn't find Type %s" $type) }}
{{ end }}
{{ end }}
{{ end }}
Expand Down

0 comments on commit 7ade8f2

Please sign in to comment.