diff --git a/index.js b/index.js index 85c6ddd..7d218a6 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,10 @@ module.exports = function StaticSiteJson(folder, options = {}) { const cleanMarkdownFunnel = new BroccoliFunnel(folder, { include: ['**/*.md', '**/*.markdown'], }); - const pagesTree = new TableOfContents([folder], options); + const tocFunnel = new BroccoliFunnel(folder, { + include: ['**/pages.yml', '**/pages.json'], + }); + const pagesTree = new TableOfContents(tocFunnel, options); const jsonApiTree = new MarkdownToJsonApi(cleanMarkdownFunnel, options); const compiledTrees = new BroccoliMergeTrees([jsonApiTree, pagesTree]); return mv(compiledTrees, (options.contentFolder || 'content')); diff --git a/lib/table-of-contents.js b/lib/table-of-contents.js index d4da7bd..a35d4a9 100644 --- a/lib/table-of-contents.js +++ b/lib/table-of-contents.js @@ -1,3 +1,4 @@ +const PersistentFilter = require('broccoli-persistent-filter'); const { Serializer } = require('jsonapi-serializer'); const yaml = require('js-yaml'); const { join } = require('path'); @@ -31,23 +32,30 @@ function subpageUrls(parentUrl, currentPage, childPages) { } } -class TableOfContentsExtractor extends Plugin { - build() { - this.inputPaths.forEach((folder) => { - let pages; - if (existsSync(join(folder, 'pages.yml'))) { - pages = yaml.safeLoad(readFileSync(join(folder, 'pages.yml'), 'utf8')); - } else if (existsSync(join(folder, 'pages.json'))) { - // eslint-disable-next-line - pages = require(join(folder, 'pages.json')); - } - if (pages) { - // add the parent id to each subpage - subpageUrls(null, null, pages); - - writeFileSync(join(this.outputPath, 'pages.json'), JSON.stringify(TableOfContentsSerializer.serialize(pages))); - } - }); + +class TableOfContentsExtractor extends PersistentFilter { + constructor(folder, options) { + super(folder, options); + this.extensions = ['yml', 'json']; + this.targetExtension = 'json'; + } + + // eslint-disable-next-line class-methods-use-this,consistent-return + processString(content, relativePath) { + let pages; + + if (relativePath.endsWith('pages.yml')) { + pages = yaml.safeLoad(content); + } else if (relativePath.endsWith('pages.json')) { + pages = JSON.parse(content); + } + + if (pages) { + // add the parent id to each subpage + subpageUrls(null, null, pages); + + return JSON.stringify(TableOfContentsSerializer.serialize(pages)); + } } } diff --git a/test/plugins/table-of-contents.js b/test/plugins/table-of-contents.js index e05d477..6011d5e 100644 --- a/test/plugins/table-of-contents.js +++ b/test/plugins/table-of-contents.js @@ -8,8 +8,7 @@ describe('table-of-contents', function () { it('should build', async function () { const input = await createTempDir(); try { - // TODO why does this need to be an array of folders - const subject = new TableOfContents([input.path()]); + const subject = new TableOfContents(input.path()); const output = createBuilder(subject); try {