Skip to content

Commit

Permalink
fixing collation and pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Jan 8, 2020
1 parent 2981e5a commit 239a50e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const BroccoliMergeTrees = require('broccoli-merge-trees');
const BroccoliFunnel = require('broccoli-funnel');
const { mv } = require('broccoli-stew');
const TableOfContents = require('./lib/table-of-contents');
const CollateJsonApiBlobs = require('./lib/collate-and-paginate');
const MarkdownToJsonApi = require('./lib/markdown-to-jsonapi');


Expand All @@ -14,6 +15,13 @@ module.exports = function StaticSiteJson(folder, options = {}) {
});
const pagesTree = new TableOfContents(tocFunnel, options);
const jsonApiTree = new MarkdownToJsonApi(cleanMarkdownFunnel, options);
const compiledTrees = new BroccoliMergeTrees([jsonApiTree, pagesTree]);

// the default content folder is "content" and this tree needs to know
// about contentFolder for pagination links
const collationTree = new CollateJsonApiBlobs(jsonApiTree, {
contentFolder: 'content',
...options,
});
const compiledTrees = new BroccoliMergeTrees([jsonApiTree, pagesTree, collationTree]);
return mv(compiledTrees, (options.contentFolder || 'content'));
};
25 changes: 15 additions & 10 deletions lib/serialize-json-blobs.js → lib/collate-and-paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ const Plugin = require('broccoli-plugin');
const {
readFileSync,
writeFileSync,
mkdirSync,
} = require('fs');
const walkSync = require('walk-sync');
const { join } = require('path');
const _ = require('lodash');
const constructSerializer = require('./construct-serializer');

function preparePages(blobs, pageSize, pageSortFunction) {
return _.chain(blobs)
Expand All @@ -25,33 +23,36 @@ class SerializeJsonBlobs extends Plugin {
constructor(inputNode, options = {}) {
super([inputNode], options);
this.options = _.assign({}, {
contentFolder: 'content',
contentTypes: ['html', 'content'],
collationFileName: 'all.json',
pageSize: 10,
}, options);
this.contentSerializer = constructSerializer(options);
}

build() {
let blobs = [];
this.inputPaths.forEach((inputPath) => {
const paths = walkSync(inputPath);
const folderConents = [];

paths.forEach((path) => {
if (path.endsWith('/')) {
mkdirSync(join(this.outputPath, path));
return;
}
const fileContent = readFileSync(join(inputPath, path)).toString();
const deserializedFile = JSON.parse(fileContent);
folderConents.push(deserializedFile);
const jsonApiBlob = this.contentSerializer.serialize(deserializedFile);
writeFileSync(join(this.outputPath, path), JSON.stringify(jsonApiBlob));
});

blobs = [...blobs, ...folderConents];
});

function groupData(pageData) {
return pageData.reduce((prev, current) => {
prev.data.push(current.data);
return prev;
}, { data: [] });
}

if (this.options.collate) {
if (this.options.paginate) {
const contentPages = preparePages(
Expand All @@ -60,7 +61,7 @@ class SerializeJsonBlobs extends Plugin {
this.options.paginateSortFunction,
);
contentPages.forEach((pageData, index) => {
const serializedPageData = this.contentSerializer.serialize(pageData);
const serializedPageData = groupData(pageData);
let fileName;

const fileNameMatch = this.options.collationFileName.match(/(.*)\.json$/);
Expand Down Expand Up @@ -98,7 +99,11 @@ class SerializeJsonBlobs extends Plugin {
}
});
} else {
const collection = this.contentSerializer.serialize(blobs);
const collection = blobs.reduce((prev, blob) => {
prev.data.push(blob.data);
return prev;
}, { data: [] });

const outputFile = join(this.outputPath, this.options.collationFileName);
writeFileSync(outputFile, JSON.stringify(collection));
}
Expand Down
2 changes: 0 additions & 2 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ describe('collections', () => {
});

afterEach(async () => {
// eslint-disable-next-line no-console
console.warn.restore();
try {
await input.dispose();
} finally {
Expand Down

0 comments on commit 239a50e

Please sign in to comment.