Skip to content

Commit

Permalink
Initial files
Browse files Browse the repository at this point in the history
  • Loading branch information
rphillips-nz committed Mar 7, 2021
1 parent e08ba29 commit f9e8671
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function (eleventyConfig, config = {}) {
const paths = {
data: '_data',
includes: '_includes',
layouts: '_includes',
...config.dir
};

eleventyConfig.addFilter('ccJsonify', obj => obj ? JSON.stringify(obj) : null);
eleventyConfig.addNunjucksShortcode('ccConfigPath', key => paths[key] || '');
};
103 changes: 101 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,101 @@
# eleventy-plugin-cloudcannon
🔌 Eleventy plugin to create CloudCannon editor details
# Eleventy Plugin CloudCannon

An Eleventy (11ty) plugin that creates [CloudCannon](https://cloudcannon.com/) editor details.

## Installation

Available on [npm](https://www.npmjs.com/package/@cloudcannon/eleventy-plugin-cloudcannon) (TODO).

```
npm install @cloudcannon/eleventy-plugin-cloudcannon --save
```

Add the following `addPlugin` call to your `module.exports` function in the Eleventy config file
(`.eleventy.js` by default):

```
const pluginCloudCannon = require("eleventy-plugin-cloudcannon");
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginCloudCannon, options);
};
```

If you set custom `dir` values for your site, pass them to the plugin as well:

```
const pluginCloudCannon = require("eleventy-plugin-cloudcannon");
module.exports = function (eleventyConfig) {
const config = {
dir: {
data: '_my-custom-data',
layouts: '_layouts',
includes: '_my-includes'
}
};
eleventyConfig.addPlugin(pluginCloudCannon, config);
return config;
};
```

If you are running this locally rather than on CloudCannon, copy the templates as well:

```
cp -R node_modules/eleventy-plugin-cloudcannon/cloudcannon .
```

## Options

Matches what you set or return in your main config. All optional, including the parameter itself.

| Key | Type | Default | Description |
| ------------ | ------ | ---------------------------------------------------------------------- | --------------------------------------------------- |
| `pathPrefix` | string | `""` | The custom pathPrefix setting your site uses (TODO) |
| `input` | string | `"."` | The custom input path your site uses (TODO) |
| `dir` | object | `{ "data": "_data", "includes": "_includes", "layouts": "_includes" }` | The custom paths your site uses (if any) |

## Data

This plugin reads data from `cloudcannon` if available (defaults to `_data/cloudcannon.json`).

Details on each property here are listed in the relevant parts of the
[CloudCannon documentation](https://cloudcannon.com/documentation/).

The following is an empty template as an example.

```
{
"timezone": "",
"collections": {
"projects": {
"_path": "",
"name": "",
"title": "",
"output": false,
"_sort-key": "",
"_subtext-key": "",
"_image-key": "",
"_image-size": "",
"_singular-name": "",
"_singular-key": "",
"_disable-add": false,
"_icon": "",
"_add-options": []
}
},
"comments": {},
"input-options": {},
"editor": {},
"source-editor": {},
"explore": {},
"array-structures": {},
"select-data": {}
}
```

## License

MIT
39 changes: 39 additions & 0 deletions cloudcannon/config.11tydata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const path = require('path');

module.exports = {
environment: process.env.ELEVENTY_ENV,

getCollections: function (cloudcannon) {
if (cloudcannon && cloudcannon.collections) {
return cloudcannon.collections;
}

const { all, ...collections } = this.ctx.collections;
const keys = Object.keys(collections);

// Maps tags to basePaths, since items with same tags can exist in separate folders
const basePaths = all.reduce((memo, item) => {
const tag = (item.data.tags || [])[0];

if (tag) {
memo[tag] = memo[tag] || new Set();
memo[tag].add(path.dirname(item.inputPath.replace('./', '')));
}

return memo;
}, {});

// Creates a collection entry for each basePath defined for a tag
// TODO: use the top-most common basePath to prevent subfolders becoming separate entries
return keys.reduce((memo, key) => {
basePaths[key].forEach((basePath) => {
memo[key] = {
_path: basePath,
output: true // TODO
};
});

return memo;
}, {});
}
};
70 changes: 70 additions & 0 deletions cloudcannon/config.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
eleventyExcludeFromCollections: true
permalink: /_cloudcannon/config.json
---
{
"time": "{{ build.date.toISOString() }}",

"cloudcannon": {
"name": "eleventy-plugin-cloudcannon",
"version": "0.0.1"
},

"generator": {
"name": "eleventy",
"version": "{{ pkg.dependencies['@11ty/eleventy'] }}",
"environment": "{{ environment }}",
"metadata": {
"markdown": "commonmark",
"commonmark": {}
}
},

"source": "",

{% if cloudcannon['timezone'] %}
"timezone": "{{ cloudcannon['timezone'] }}",
{% endif %}

"base-url": "",

"collections": {{ getCollections(cloudcannon) | ccJsonify | safe }},

{% if cloudcannon['comments'] %}
"comments": {{ cloudcannon['comments'] | ccJsonify | safe }},
{% endif %}

{% if cloudcannon['input-options'] %}
"input-options": {{ cloudcannon['input-options'] | ccJsonify | safe }},
{% endif %}

{% if cloudcannon['editor'] %}
"editor": {{ cloudcannon['editor'] | ccJsonify | safe }},
{% endif %}

{% if cloudcannon['source-editor'] %}
"source-editor": {{ cloudcannon['source-editor'] | ccJsonify | safe }},
{% endif %}

{% if cloudcannon['explore'] %}
"explore": {{ cloudcannon['explore'] | ccJsonify | safe }},
{% endif %}

{% if cloudcannon['array-structures'] %}
"array-structures": {{ cloudcannon['array-structures'] | ccJsonify | safe }},
{% endif %}

{% if cloudcannon['select-data'] %}
"select-data": {{ cloudcannon['select-data'] | ccJsonify | safe }},
{% endif %}

"paths": {
{% if cloudcannon['uploads-path'] %}
"uploads": "{{ cloudcannon['uploads-path'] }}",
{% endif %}
"data": "{% ccConfigPath 'data' %}",
"collections": "",
"includes": "{% ccConfigPath 'includes' %}",
"layouts": "{% ccConfigPath 'layouts' %}"
}
}
24 changes: 24 additions & 0 deletions cloudcannon/details.11tydata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function isStaticPage(item) {
return !item.template._layoutKey && (!item.data.tags || !item.data.tags.length)
}

function isPage(item) {
return item.template._layoutKey && (!item.data.tags || !item.data.tags.length)
}

module.exports = {
environment: process.env.ELEVENTY_ENV,

getStaticPages: function () {
return this.ctx.collections.all.filter(isStaticPage);
},

getPages: function () {
return this.ctx.collections.all.filter(isPage);
},

getCollections: function () {
const { all, ...collections } = this.ctx.collections;
return collections;
}
};
63 changes: 63 additions & 0 deletions cloudcannon/details.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
eleventyExcludeFromCollections: true
permalink: /_cloudcannon/details.json
---
{
"time": "{{ build.date.toISOString() }}",
"cloudcannon": {
"name": "eleventy-plugin-cloudcannon",
"version": "0.0.1"
},
"generator": {
"name": "eleventy",
"version": "{{ pkg.dependencies['@11ty/eleventy'] }}",
"environment": "{{ environment }}",
"metadata": {
"markdown": "commonmark",
"commonmark": {}
}
},
"collections": {
{%- for tag, items in getCollections() %}
"{{ tag }}": [
{% for item in items %}
{
"path": "{{ item.inputPath.replace('./', '') if item.inputPath else '' }}",
"url": "{{ item.url if item.url else '' }}",
"collection": "{{ tag }}",
"title": "{{ item.data.title }}",
"name": "{{ item.data.name }}",
"layout": "{{ item.template._layoutKey }}",
"_unlisted": {{ item.data._unlisted === true }},
"output": {{ item.url !== false }}
{% if item.data._options %},"_options": {{ item.data._options | ccJsonify | safe }}{% endif %}
{% if item.data._comments %},"_comments": {{ item.data._comments | ccJsonify | safe }}{% endif %}
}{{ '' if loop.last else ',' }}
{% endfor %}
]{{ '' if loop.last else ',' }}
{%- endfor %}
},
"pages": [
{%- for item in getPages() %}
{
"path": "{{ item.inputPath.replace('./', '') if item.inputPath else '' }}",
"url": "{{ item.url if item.url else '' }}",
"title": "{{ item.data.title }}",
"name": "{{ item.data.name }}",
"layout": "{{ item.template._layoutKey }}",
"_unlisted": {{ item.data._unlisted === true }},
"output": {{ item.url !== false }}
{% if item.data._options %},"_options": {{ item.data._options | ccJsonify | safe }}{% endif %}
{% if item.data._comments %},"_comments": {{ item.data._comments | ccJsonify | safe }}{% endif %}
}{{ '' if loop.last else ',' }}
{% endfor %}
],
"static-pages": [
{%- for item in getStaticPages() %}
{
"path": "{{ item.inputPath.replace('./', '') if item.inputPath else '' }}",
"url": "{{ item.url if item.url else '' }}"
}{{ '' if loop.last else ',' }}
{% endfor %}
]
}
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "eleventy-plugin-cloudcannon",
"version": "0.0.1",
"description": "Eleventy plugin to create CloudCannon editor details",
"main": ".eleventy.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cloudcannon/eleventy-plugin-cloudcannon.git"
},
"keywords": [
"eleventy-plugin",
"cloudcannon"
],
"author": "Ross Phillips",
"license": "MIT",
"bugs": {
"url": "https://github.com/cloudcannon/eleventy-plugin-cloudcannon/issues"
},
"homepage": "https://github.com/cloudcannon/eleventy-plugin-cloudcannon#readme"
}

0 comments on commit f9e8671

Please sign in to comment.