diff --git a/index.js b/index.js index 7d218a6..5b15fab 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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')); }; diff --git a/lib/serialize-json-blobs.js b/lib/collate-and-paginate.js similarity index 85% rename from lib/serialize-json-blobs.js rename to lib/collate-and-paginate.js index 0201adc..e384826 100644 --- a/lib/serialize-json-blobs.js +++ b/lib/collate-and-paginate.js @@ -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) @@ -25,12 +23,9 @@ 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() { @@ -38,20 +33,26 @@ class SerializeJsonBlobs extends Plugin { 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( @@ -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$/); @@ -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)); } diff --git a/test/collections.js b/test/collections.js index e9da952..5079338 100644 --- a/test/collections.js +++ b/test/collections.js @@ -35,8 +35,6 @@ describe('collections', () => { }); afterEach(async () => { - // eslint-disable-next-line no-console - console.warn.restore(); try { await input.dispose(); } finally {