Skip to content

Commit

Permalink
Merge pull request #91 from cloudfour/feat-add_collections_helper
Browse files Browse the repository at this point in the history
Feature: add collections helper
  • Loading branch information
erikjung committed May 17, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents e32e46d + b3468f9 commit b0d3ff1
Showing 6 changed files with 121 additions and 28 deletions.
24 changes: 19 additions & 5 deletions src/helpers/page.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import R from 'ramda';
import {relative as relativePath} from 'path';
import {splitPath} from '../utils/object';
import {splitPath, normalizePath, isPathChild} from '../utils/object';
import {sortByProp} from '../utils/list';
import {
resourcePath,
isType
} from '../utils/shared';

const isDir = isType(undefined);
const pickProps = R.pick(['id', 'url', 'data']);
const pickProps = R.pick(['id', 'url', 'name', 'data']);

/**
* Return an inner object/array from the Drizzle context.
@@ -41,6 +41,8 @@ function extractSubset (path, drizzle) {
* The relative base path for the supplied resource type.
*/
function destRoot (type, drizzle) {
const options = drizzle.options;

// TODO: this is unfortunate, and due to difficulty using defaults.keys
const keys = new Map([
['page', 'pages'],
@@ -49,8 +51,8 @@ function destRoot (type, drizzle) {
]);

return relativePath(
drizzle.options.dest.root,
drizzle.options.dest[keys.get(type)]
options.dest.root,
options.dest[keys.get(type)]
);
}

@@ -96,7 +98,6 @@ export default function register (options) {
}

if (options.sortby) {
// TODO: do we want to access via "data.foo" or just "foo"
results = sortByProp(['data', options.sortby], results);
}

@@ -117,5 +118,18 @@ export default function register (options) {
return result;
});

Handlebars.registerHelper('collections', (...args) => {
const path = R.is(String, args[0]) ? args[0] : '.';
const context = args[1] || args[0];
const drizzle = context.data.root.drizzle;
const tree = drizzle.tree.collections;
const treePath = normalizePath(`collections.${path}`);
const results = tree
.filter(item => isPathChild(item.id, treePath))
.map(item => menuItem(item, drizzle));

return results;
});

return Handlebars;
}
41 changes: 23 additions & 18 deletions src/parse/tree.js
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@
* @module parse/tree
*/

import { resourcePath } from '../utils/shared';

/**
return {
data : allData[0],
@@ -15,22 +13,22 @@ return {
};
**/

function walkResources (resources, options, resourceType, resourceTree = {}) {
function walkResources (
resources,
options,
resourceType,
resourceTree = []
) {
for (var resourceKey in resources) {
if (resources[resourceKey].resourceType &&
resources[resourceKey].resourceType === resourceType.singular) {
resourceTree.items = resourceTree.items || [];
resourceTree.items.push({
id: resources[resourceKey].id,
key: resourceKey,
path: resourcePath(resources[resourceKey].id,
options.dest[resourceType.plural], options)
});
const item = resources[resourceKey];
if (item.resourceType && item.resourceType === resourceType.singular) {
resourceTree.push(item);
} else {
resourceTree.children = resourceTree.children || [];
resourceTree.children.push(
walkResources(resources[resourceKey], options,
resourceType, resourceTree[resourceKey])
walkResources(
item,
options,
resourceType,
resourceTree
);
}
}
@@ -44,9 +42,16 @@ function parseTree (allData, options) {
patterns : allData[2],
templates: allData[3],
tree: {
pages: walkResources(allData[1], options, options.keys.pages),
pages: walkResources(
allData[1],
options,
options.keys.pages
),
collections: walkResources(
allData[2], options, options.keys.collections)
allData[2],
options,
options.keys.collections
)
},
options : options
};
43 changes: 41 additions & 2 deletions src/utils/object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { idKeys, keyname, relativePathArray } from './shared';
import {idKeys, keyname, relativePathArray} from './shared';
import R from 'ramda';
import DrizzleError from './error';
import {join, relative} from 'path';

/**
* Return a reference to the deeply-nested object indicated by the items
@@ -144,12 +145,50 @@ function splitPath (path) {
return result;
}

/**
* Normalize a sloppy (or dot-separated) path into a "valid" path.
*
* @param {String} path
* A raw input path string.
*
* @return {String}
* A normalized path string.
*
* @example
* normalizePath('foo/bar//baz.bang.1./');
* // 'foo/bar/baz/bang/1'
*/
function normalizePath (path) {
return join(...splitPath(path));
}

/**
* Check if one path is a direct child of another.
*
* @param {String} pathA
* @param {String} pathB
* @return {Boolean}
*
* @example
* isPathChild('components/button', 'components');
* // true
*/
function isPathChild (pathA, pathB) {
const relPath = relative(
normalizePath(pathA),
normalizePath(pathB)
);
return relPath === '..';
}

export { deepCollection, // object
deepObj, // object
deepPattern, // object
flattenById,
keyname, // object
resourceId, //object
resourceKey, // object
splitPath
splitPath,
normalizePath,
isPathChild
};
2 changes: 1 addition & 1 deletion test/config.js
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ var config = {
},
dest: {
root: './test/dist',
collections: './test/dist/collections',
collections: './test/dist/patterns',
pages: './test/dist',
patterns: './test/dist/patterns'
},
21 changes: 21 additions & 0 deletions test/fixtures/pages/helpers-demo-collections.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{#* inline "collection-item"}}
<output test-collection-index="{{@index}}" test-collection-id="{{id}}">
{{url}}
{{name}}
</output>
{{/inline}}

<!-- Iterating over collections -->
{{#each (collections) as |collection|}}
{{> collection-item collection}}
{{/each}}

<!-- Iterating over collections: "components" -->
{{#each (collections "components") as |collection|}}
{{> collection-item collection}}
{{/each}}

<!-- Iterating over collections: "typography" -->
{{#each (collections "typography") as |collection|}}
{{> collection-item collection}}
{{/each}}
18 changes: 16 additions & 2 deletions test/write/pages.js
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ describe ('write/pages', () => {
expect(contents).not.to.contain('Body content should replace this.');
});
});
it ('should write page files with functioning {{data}} helpers', () => {
it ('should write page files with functioning {{data}} helper', () => {
return testUtils.fileContents(drizzleData.pages.usingHelpers.outputPath)
.then(contents => {
expect(contents).to.contain('<output>cat is in the well</output>');
@@ -56,7 +56,7 @@ describe ('write/pages', () => {
expect(contents).to.contain('<output>5</output>');
});
});
it ('should write page files with functioning {{pages}} helpers', () => {
it ('should write page files with functioning {{pages}} helper', () => {
return testUtils.fileContents(drizzleData.pages.usingPageHelpers.outputPath)
.then(contents => {
expect(contents).to.contain('<output>default: 04-sandbox.html</output>');
@@ -65,6 +65,20 @@ describe ('write/pages', () => {
expect(contents).to.contain('<output>page: pages.nerkle</output>');
});
});
it ('should write page files with functioning {{collections}} helper', () => {
return testUtils.fileContents(drizzleData.pages['helpers-demo-collections'].outputPath)
.then(contents => {
expect(contents).to.contain(
'<output test-collection-index="0" test-collection-id="collections.components">'
);
expect(contents).to.contain(
'<output test-collection-index="0" test-collection-id="collections.components.button">'
);
expect(contents).to.contain(
'<output test-collection-index="0" test-collection-id="collections.typography.headings">'
);
});
});
});
describe ('write to page destination', () => {
var alteredData;

0 comments on commit b0d3ff1

Please sign in to comment.