From 815dde46af07e51b91b2f16028c5c08423d0a6a7 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Thu, 2 Dec 2021 10:45:28 +0000 Subject: [PATCH 1/4] simplify table-of-contents test --- test/plugins/table-of-contents.js | 87 ++++++++++++++++--------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/test/plugins/table-of-contents.js b/test/plugins/table-of-contents.js index 6011d5e..a11343b 100644 --- a/test/plugins/table-of-contents.js +++ b/test/plugins/table-of-contents.js @@ -5,59 +5,62 @@ const { expect } = require('chai'); const TableOfContents = require('../../lib/table-of-contents'); describe('table-of-contents', function () { - it('should build', async function () { - const input = await createTempDir(); - try { - const subject = new TableOfContents(input.path()); - const output = createBuilder(subject); - - try { - // INITIAL - input.write({ - 'pages.yml': `- title: "Getting Started" + let input; + let output; + + beforeEach(async function () { + input = await createTempDir(); + }); + + afterEach(async function () { + await input.dispose(); + await output.dispose(); + }); + + it('should build pages.yml', async function () { + const subject = new TableOfContents(input.path()); + output = createBuilder(subject); + + // INITIAL + input.write({ + 'pages.yml': `- title: "Getting Started" url: 'getting-started' pages: - title: "How To Use The Guides" url: "intro"`, - }); + }); - await output.build(); + await output.build(); - expect(output.read()).to.deep.equal({ - 'pages.json': '{"data":[{"type":"pages","id":"getting-started","attributes":{"title":"Getting Started","pages":[{"title":"How To Use The Guides","url":"getting-started/intro"}]}}]}', - }); + expect(output.read()).to.deep.equal({ + 'pages.json': '{"data":[{"type":"pages","id":"getting-started","attributes":{"title":"Getting Started","pages":[{"title":"How To Use The Guides","url":"getting-started/intro"}]}}]}', + }); - expect(output.changes()).to.deep.equal({ - 'pages.json': 'create', - }); + expect(output.changes()).to.deep.equal({ + 'pages.json': 'create', + }); - // UPDATE - input.write({ - 'pages.yml': `- title: "Tutorial" + // UPDATE + input.write({ + 'pages.yml': `- title: "Tutorial" url: 'tutorial' pages: - title: "Creating Your App" url: "ember-cli"`, // change - }); - await output.build(); - - expect(output.read()).to.deep.equal({ - 'pages.json': '{"data":[{"type":"pages","id":"tutorial","attributes":{"title":"Tutorial","pages":[{"title":"Creating Your App","url":"tutorial/ember-cli"}]}}]}', - }); - - expect(output.changes()).to.deep.equal({ - 'pages.json': 'change', - }); - - // NOOP - await output.build(); - - expect(output.changes()).to.deep.equal({}); - } finally { - await output.dispose(); - } - } finally { - await input.dispose(); - } + }); + await output.build(); + + expect(output.read()).to.deep.equal({ + 'pages.json': '{"data":[{"type":"pages","id":"tutorial","attributes":{"title":"Tutorial","pages":[{"title":"Creating Your App","url":"tutorial/ember-cli"}]}}]}', + }); + + expect(output.changes()).to.deep.equal({ + 'pages.json': 'change', + }); + + // NOOP + await output.build(); + + expect(output.changes()).to.deep.equal({}); }); }); From 79412c1a56ed60b14a285fbe23a114824d4bdd19 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Thu, 2 Dec 2021 11:00:26 +0000 Subject: [PATCH 2/4] move our test to use broccoli-static-site-json instead of sub plugin --- test/plugins/table-of-contents.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/test/plugins/table-of-contents.js b/test/plugins/table-of-contents.js index a11343b..1ab373e 100644 --- a/test/plugins/table-of-contents.js +++ b/test/plugins/table-of-contents.js @@ -2,7 +2,7 @@ const { createBuilder, createTempDir } = require('broccoli-test-helper'); const { expect } = require('chai'); -const TableOfContents = require('../../lib/table-of-contents'); +const StaticSiteJson = require('../../index'); describe('table-of-contents', function () { let input; @@ -18,7 +18,7 @@ describe('table-of-contents', function () { }); it('should build pages.yml', async function () { - const subject = new TableOfContents(input.path()); + const subject = new StaticSiteJson(input.path()); output = createBuilder(subject); // INITIAL @@ -33,11 +33,14 @@ describe('table-of-contents', function () { await output.build(); expect(output.read()).to.deep.equal({ - 'pages.json': '{"data":[{"type":"pages","id":"getting-started","attributes":{"title":"Getting Started","pages":[{"title":"How To Use The Guides","url":"getting-started/intro"}]}}]}', + content: { + 'pages.json': '{"data":[{"type":"pages","id":"getting-started","attributes":{"title":"Getting Started","pages":[{"title":"How To Use The Guides","url":"getting-started/intro"}]}}]}', + }, }); expect(output.changes()).to.deep.equal({ - 'pages.json': 'create', + 'content/': 'mkdir', + 'content/pages.json': 'create', }); // UPDATE @@ -51,11 +54,13 @@ describe('table-of-contents', function () { await output.build(); expect(output.read()).to.deep.equal({ - 'pages.json': '{"data":[{"type":"pages","id":"tutorial","attributes":{"title":"Tutorial","pages":[{"title":"Creating Your App","url":"tutorial/ember-cli"}]}}]}', + content: { + 'pages.json': '{"data":[{"type":"pages","id":"tutorial","attributes":{"title":"Tutorial","pages":[{"title":"Creating Your App","url":"tutorial/ember-cli"}]}}]}', + }, }); expect(output.changes()).to.deep.equal({ - 'pages.json': 'change', + 'content/pages.json': 'change', }); // NOOP From 4432773d7665d88ee2e062c249805671ba31af90 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Thu, 2 Dec 2021 11:02:04 +0000 Subject: [PATCH 3/4] support using toc.yml instead of pages.yml --- index.js | 7 ++++++- lib/table-of-contents.js | 4 ++-- test/plugins/table-of-contents.js | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 68ee6db..1fb7033 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,12 @@ module.exports = function StaticSiteJson(folder, options = {}) { include: ['**/*.md', '**/*.markdown'], }); const tocFunnel = new BroccoliFunnel(folder, { - include: ['**/pages.yml', '**/pages.json'], + include: [ + '**/pages.yml', + '**/pages.json', + '**/toc.yml', + '**/toc.json', + ], }); const pagesTree = new TableOfContents(tocFunnel, options); const jsonApiTree = new MarkdownToJsonApi(cleanMarkdownFunnel, options); diff --git a/lib/table-of-contents.js b/lib/table-of-contents.js index 4ea9435..d612496 100644 --- a/lib/table-of-contents.js +++ b/lib/table-of-contents.js @@ -37,9 +37,9 @@ class TableOfContentsExtractor extends PersistentFilter { processString(content, relativePath) { let pages; - if (relativePath.endsWith('pages.yml')) { + if (relativePath.endsWith('.yml')) { pages = yaml.load(content); - } else if (relativePath.endsWith('pages.json')) { + } else if (relativePath.endsWith('.json')) { pages = JSON.parse(content); } diff --git a/test/plugins/table-of-contents.js b/test/plugins/table-of-contents.js index 1ab373e..856996b 100644 --- a/test/plugins/table-of-contents.js +++ b/test/plugins/table-of-contents.js @@ -68,4 +68,31 @@ describe('table-of-contents', function () { expect(output.changes()).to.deep.equal({}); }); + + it('should build toc.yml', async function () { + const subject = new StaticSiteJson(input.path()); + output = createBuilder(subject); + + // INITIAL + input.write({ + 'toc.yml': `- title: "Getting Started" + url: 'getting-started' + pages: + - title: "How To Use The Guides" + url: "intro"`, + }); + + await output.build(); + + expect(output.read()).to.deep.equal({ + content: { + 'toc.json': '{"data":[{"type":"pages","id":"getting-started","attributes":{"title":"Getting Started","pages":[{"title":"How To Use The Guides","url":"getting-started/intro"}]}}]}', + }, + }); + + expect(output.changes()).to.deep.equal({ + 'content/': 'mkdir', + 'content/toc.json': 'create', + }); + }); }); From d8d2ad0033b5934fc5ccfda7c238cca64fe31fb8 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Thu, 2 Dec 2021 11:04:30 +0000 Subject: [PATCH 4/4] support using pages.yaml instead of just pages.yml --- index.js | 2 ++ lib/table-of-contents.js | 4 ++-- test/plugins/table-of-contents.js | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1fb7033..983a15c 100644 --- a/index.js +++ b/index.js @@ -27,8 +27,10 @@ module.exports = function StaticSiteJson(folder, options = {}) { const tocFunnel = new BroccoliFunnel(folder, { include: [ '**/pages.yml', + '**/pages.yaml', '**/pages.json', '**/toc.yml', + '**/toc.yaml', '**/toc.json', ], }); diff --git a/lib/table-of-contents.js b/lib/table-of-contents.js index d612496..c55d1f5 100644 --- a/lib/table-of-contents.js +++ b/lib/table-of-contents.js @@ -29,7 +29,7 @@ function subpageUrls(parentUrl, currentPage, childPages) { class TableOfContentsExtractor extends PersistentFilter { constructor(folder, options) { super(folder, options); - this.extensions = ['yml', 'json']; + this.extensions = ['yaml', 'yml', 'json']; this.targetExtension = 'json'; } @@ -37,7 +37,7 @@ class TableOfContentsExtractor extends PersistentFilter { processString(content, relativePath) { let pages; - if (relativePath.endsWith('.yml')) { + if (relativePath.endsWith('.yml') || relativePath.endsWith('.yaml')) { pages = yaml.load(content); } else if (relativePath.endsWith('.json')) { pages = JSON.parse(content); diff --git a/test/plugins/table-of-contents.js b/test/plugins/table-of-contents.js index 856996b..16e533a 100644 --- a/test/plugins/table-of-contents.js +++ b/test/plugins/table-of-contents.js @@ -95,4 +95,31 @@ describe('table-of-contents', function () { 'content/toc.json': 'create', }); }); + + it('should build toc.yaml', async function () { + const subject = new StaticSiteJson(input.path()); + output = createBuilder(subject); + + // INITIAL + input.write({ + 'toc.yaml': `- title: "Getting Started" + url: 'getting-started' + pages: + - title: "How To Use The Guides" + url: "intro"`, + }); + + await output.build(); + + expect(output.read()).to.deep.equal({ + content: { + 'toc.json': '{"data":[{"type":"pages","id":"getting-started","attributes":{"title":"Getting Started","pages":[{"title":"How To Use The Guides","url":"getting-started/intro"}]}}]}', + }, + }); + + expect(output.changes()).to.deep.equal({ + 'content/': 'mkdir', + 'content/toc.json': 'create', + }); + }); });