Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
feat: Add filterTransform option
Browse files Browse the repository at this point in the history
* allow uesr to provide transformations which apply to the filter values
* make test more real-world :)
* fix syntax
* match formatting in docs
* use old school options defaulting
* add closing brace & indicate the need for non-JSON config
* clarify usage even more
  • Loading branch information
bnchdrff authored and stefanjudis committed Sep 16, 2016
1 parent 3d8a461 commit 50d316c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
31 changes: 31 additions & 0 deletions docs/global-settings.md
Expand Up @@ -50,3 +50,34 @@ For using the [Content Delivery API](https://www.contentful.com/developers/docs/

*Recommended way here is to set the `host` in the `metalsmith.json` or global config and overwrite it if needed in depending source files.*

### `filterTransforms` *(optional)*

If you want to use dynamic values in a source file's filter query, you can provide an object containing named functions that will be invoked during the metalsmith build. For example, if you provide a configuration like this:

```javascript
{
// ...
filterTransforms: {
__NOW__() {
return (new Date()).toISOString();
},
},
// ...
}
```

Note that you will need to invoke metalsmith programatically & process configuration as javascript, not JSON, in this case.

You can then use the keyword `__NOW__` in your source file's `filter` values, like so:

```markdown
---
title: Post overview of entries including "rabbit"
contentful:
content_type: post
filter:
'startdate[lte]': '__NOW__'
'enddate[gt]': '__NOW__'
layout: posts.html
---
```
2 changes: 1 addition & 1 deletion docs/source-file-settings.md
Expand Up @@ -296,4 +296,4 @@ POSTS-CONTENT-DIFFERENT-SPACE

## File meta available in template

Contentful-metalsmith adds some [handy meta information](./file-meta.md) to the data available in data to make the generation of sites even easier.
Contentful-metalsmith adds some [handy meta information](./file-meta.md) to the data available in data to make the generation of sites even easier.
2 changes: 1 addition & 1 deletion lib/processor.js
Expand Up @@ -114,7 +114,7 @@ function processFile (file, options) {
const spaceId = file.contentful.space_id || options.space_id
const accessToken = file.contentful.access_token || options.access_token
const host = file.contentful.host || options.host
const query = util.getEntriesQuery(file.contentful)
const query = util.getEntriesQuery(file.contentful, options.filterTransforms)

const client = getContentfulClient(accessToken, spaceId, host)

Expand Down
12 changes: 11 additions & 1 deletion lib/util.js
Expand Up @@ -29,12 +29,15 @@ function _slug (value) {
* Get entries query
*
* @param {Object} options file meta options
* @param {Object} filterTransforms transformations for filter values
*
* @return {Object} query obejct
*/
function getEntriesQuery (options) {
function getEntriesQuery (options, filterTransformsOptions) {
const query = {}

const filterTransforms = filterTransformsOptions || {}

if (options.content_type && options.content_type !== '*') {
query.content_type = options.content_type
}
Expand All @@ -44,6 +47,13 @@ function getEntriesQuery (options) {
}

if (options.filter) {
// If a transformation is available for this keyword, apply it.
for (let key in options.filter) {
if (typeof filterTransforms[options.filter[key]] === 'function') {
options.filter[key] = filterTransforms[options.filter[key]]()
}
}

Object.assign(query, options.filter)
}

Expand Down
12 changes: 12 additions & 0 deletions lib/util.spec.js
Expand Up @@ -28,6 +28,18 @@ test('util.getEntriesQuery - it should set given properties properly', t => {
t.is(query.order, 'ASC')
})

test('util.getEntriesQuery - it should replace KEYWORD with the result of a function KEYWORD as defined in options.filterTransforms', t => {
const aCertainNow = new Date().toISOString()

let query = util.getEntriesQuery({ filter: { 'fields.startdate[lte]': '__NOW__' } }, {
__NOW__ () {
return aCertainNow
}
})

t.is(query['fields.startdate[lte]'], aCertainNow, 'filterTransforms correctly applied')
})

test('util.getFileName - should use the correct defaults', t => {
const fileName = util.getFileName(
{
Expand Down

0 comments on commit 50d316c

Please sign in to comment.