Skip to content

Commit

Permalink
Add data field
Browse files Browse the repository at this point in the history
  • Loading branch information
rphillips-nz committed Jun 18, 2021
1 parent 68bec19 commit 5873d6d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cloudcannon/info.11tydata.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ module.exports = {
return otherCollections;
},

getData: function (cloudcannon) {
if (!cloudcannon?.data) {
return {};
}

const { ctx } = this;
return Object.keys(cloudcannon.data).reduce((memo, key) => {
if (cloudcannon.data[key] === true) {
memo[key] = ctx[key] ?? {};
}

return memo;
}, {});
},

processItem: function (item, tag) {
return {
...item.template.frontMatter.data,
Expand Down
1 change: 1 addition & 0 deletions cloudcannon/info.njk
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ permalink: /_cloudcannon/info.json
{{ processItem(item) | ccJsonify | safe }}{{ '' if loop.last else ',' }}
{% endfor %}
],
"data": {{ getData(cloudcannon) | ccJsonify | safe }},
{% if cloudcannon._comments %}
"_comments": {{ cloudcannon._comments | ccJsonify | safe }},
{% endif %}
Expand Down
27 changes: 27 additions & 0 deletions test/info.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const info = require('../cloudcannon/info.11tydata.js');

// Simulates the context when running Eleventy
const boundGetData = info.getData.bind({
ctx: {
things: ['a', 'b', 'c'],
nope: { hello: 'there' }
}
});

const collectionItem = {
inputPath: './staff/pete.html',
url: '/staff/pete/',
Expand Down Expand Up @@ -84,6 +92,25 @@ test('gets collections', () => {
});
});

test('gets data', () => {
const cloudcannon = {
data: {
things: true,
stuff: true,
nope: false
}
};

expect(boundGetData(cloudcannon)).toEqual({
things: ['a', 'b', 'c'],
stuff: {}
});
});

test('get no data', () => {
expect(boundGetData()).toEqual({});
});

test('processes item', () => {
expect(info.processItem(page, 'tag')).toEqual(expectedProcessedPage);
});
Expand Down

0 comments on commit 5873d6d

Please sign in to comment.