Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support toc yaml #64

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ module.exports = function StaticSiteJson(folder, options = {}) {
include: ['**/*.md', '**/*.markdown'],
});
const tocFunnel = new BroccoliFunnel(folder, {
include: ['**/pages.yml', '**/pages.json'],
include: [
'**/pages.yml',
'**/pages.yaml',
'**/pages.json',
'**/toc.yml',
'**/toc.yaml',
'**/toc.json',
],
});
const pagesTree = new TableOfContents(tocFunnel, options);
const jsonApiTree = new MarkdownToJsonApi(cleanMarkdownFunnel, options);
Expand Down
6 changes: 3 additions & 3 deletions lib/table-of-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ 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';
}

// eslint-disable-next-line class-methods-use-this,consistent-return
processString(content, relativePath) {
let pages;

if (relativePath.endsWith('pages.yml')) {
if (relativePath.endsWith('.yml') || relativePath.endsWith('.yaml')) {
pages = yaml.load(content);
} else if (relativePath.endsWith('pages.json')) {
} else if (relativePath.endsWith('.json')) {
pages = JSON.parse(content);
}

Expand Down
148 changes: 105 additions & 43 deletions test/plugins/table-of-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,124 @@

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 () {
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 StaticSiteJson(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({
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',
});
expect(output.changes()).to.deep.equal({
'content/': 'mkdir',
'content/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({
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({
'content/pages.json': 'change',
});

// NOOP
await output.build();

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',
});
});

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',
});
});
});