From 98e110677422a7b172598f93d5943b45d254a78b Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 28 Jun 2016 21:56:26 +0100 Subject: [PATCH 1/2] feat(menu): write complete metadata to menu file menu service only used to write title,permalink and weight to the json which is not write, it should write everything from yaml frontmatter --- src/Menu/index.js | 17 +++++----- test/docs/menu-new.json | 9 +++++ test/menu.spec.js | 75 ++++++++++++++++++++++++----------------- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/Menu/index.js b/src/Menu/index.js index 276cb0a..df3c79b 100644 --- a/src/Menu/index.js +++ b/src/Menu/index.js @@ -23,19 +23,18 @@ class Menu { /** * adds a new child to the menu items * - * @param {String} title - * @param {String} permalink - * @param {Array} [categories] - * @param {Number} [weight] + * @param {Object} child + * + * @note - A child is expected to have title and permalink * * @public */ - addChild (title, permalink, categories, weight) { - categories = categories || ['root'] - weight = weight || 0 - _.each(categories, (category) => { + addChild (child) { + child.categories = child.categories || ['root'] + child.weight = child.weight || 0 + _.each(child.categories, (category) => { this.items[category] = this.items[category] || [] - this.items[category].push({title, permalink, weight}) + this.items[category].push(child) }) } diff --git a/test/docs/menu-new.json b/test/docs/menu-new.json index 2d6366f..4d85ef9 100644 --- a/test/docs/menu-new.json +++ b/test/docs/menu-new.json @@ -3,11 +3,17 @@ { "title": "Routing", "permalink": "routing", + "categories": [ + "basics" + ], "weight": 0 }, { "title": "Controllers", "permalink": "controllers", + "categories": [ + "basics" + ], "weight": 0 } ], @@ -15,6 +21,9 @@ { "title": "Model", "permalink": "model", + "categories": [ + "database" + ], "weight": 0 } ] diff --git a/test/menu.spec.js b/test/menu.spec.js index 6c7e346..8ed10bf 100644 --- a/test/menu.spec.js +++ b/test/menu.spec.js @@ -20,62 +20,74 @@ require('co-mocha') describe('Menu', function () { it('should add a new item to the list', function () { const menu = new Menu() - menu.addChild('Routing', 'routing', ['basics']) - expect(menu.tree()).deep.equal({basics: [{title: 'Routing', permalink: 'routing', weight: 0}]}) + const child = {title: 'Routing', permalink: 'routing', categories: ['basics']} + menu.addChild(child) + expect(menu.tree()).deep.equal({basics: [child]}) }) it('should add multiple items to the menu list', function () { const menu = new Menu() - menu.addChild('Routing', 'routing', ['basics']) - menu.addChild('Controllers', 'controllers', ['basics']) - expect(menu.tree()).deep.equal({basics: [{title: 'Routing', permalink: 'routing', weight: 0}, {title: 'Controllers', permalink: 'controllers', weight: 0}]}) + const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics']} + const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']} + menu.addChild(child1) + menu.addChild(child2) + expect(menu.tree()).deep.equal({basics: [child1, child2]}) }) it('should add same child to multiple categories', function () { const menu = new Menu() - menu.addChild('Ioc', 'ioc-container', ['basics', 'fundamentals']) - expect(menu.tree()).deep.equal({fundamentals: [{title: 'Ioc', permalink: 'ioc-container', weight: 0}], basics: [{title: 'Ioc', permalink: 'ioc-container', weight: 0}]}) + const child = {title: 'Ioc', permalink: 'ioc-container', categories: ['basics', 'fundamentals']} + menu.addChild(child) + expect(menu.tree()).deep.equal({fundamentals: [child], basics: [child]}) }) it('should add same childs to root categories when no categories are defined', function () { const menu = new Menu() - menu.addChild('Ioc', 'ioc-container') - expect(menu.tree()).deep.equal({root: [{title: 'Ioc', permalink: 'ioc-container', weight: 0}]}) + const child = {title: 'Ioc', permalink: 'ioc-container'} + menu.addChild(child) + expect(menu.tree()).deep.equal({root: [child]}) }) it('should return item using the permalink', function () { const menu = new Menu() - menu.addChild('Ioc', 'ioc-container', ['basics', 'fundamentals']) - menu.addChild('Routing', 'routing', ['basics']) - menu.addChild('Controllers', 'controllers', ['basics']) - const child = menu.getChild('ioc-container') - expect(child).deep.equal({permalink: 'ioc-container', title: 'Ioc', weight: 0}) + const child1 = {title: 'Ioc', permalink: 'ioc-container', categories: ['basics', 'fundamentals']} + const child2 = {title: 'Routing', permalink: 'routing', categories: ['basics']} + const child3 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']} + menu.addChild(child1) + menu.addChild(child2) + menu.addChild(child3) + const ioc = menu.getChild('ioc-container') + expect(ioc).deep.equal(child1) const routes = menu.getChild('routing') - expect(routes).deep.equal({permalink: 'routing', title: 'Routing', weight: 0}) + expect(routes).deep.equal(child2) }) it('should return undefined when unable to find item using the permalink', function () { const menu = new Menu() - menu.addChild('Ioc', 'ioc-container', ['basics', 'fundamentals']) - menu.addChild('Routing', 'routing', ['basics']) - menu.addChild('Controllers', 'controllers', ['basics']) - const child = menu.getChild('ioc') - expect(child).to.equal(undefined) + const child = {title: 'Ioc', permalink: 'ioc-container', categories: ['basics', 'fundamentals']} + menu.addChild(child) + const ioc = menu.getChild('ioc') + expect(ioc).to.equal(undefined) }) it('should return the tree sorted by childs weight', function () { const menu = new Menu() - menu.addChild('Routing', 'routing', ['basics'], 1) - menu.addChild('Controllers', 'controllers', ['basics'], 0) - expect(menu.tree()).deep.equal({basics: [{title: 'Controllers', permalink: 'controllers', weight: 0}, {title: 'Routing', permalink: 'routing', weight: 1}]}) + const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics'], weight: 1} + const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics'], weight: 0} + menu.addChild(child1) + menu.addChild(child2) + expect(menu.tree()).deep.equal({basics: [child2, child1]}) }) it('should return tree only for the defined categories', function () { const menu = new Menu() - menu.addChild('Routing', 'routing', ['basics']) - menu.addChild('Controllers', 'controllers', ['basics']) - menu.addChild('Model', 'model', ['database']) - expect(menu.tree(['database'])).deep.equal({database: [{title: 'Model', permalink: 'model', weight: 0}]}) + const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics']} + const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']} + const child3 = {title: 'Model', permalink: 'model', categories: ['database']} + menu.addChild(child1) + menu.addChild(child2) + menu.addChild(child3) + expect(menu.tree(['database'])).deep.equal({database: [child3]}) }) it('should be able to load menu items from a json file', function * () { @@ -93,9 +105,12 @@ describe('Menu', function () { it('should write menu items to a file', function * () { const menuFilePath = path.join(__dirname, './docs/menu-new.json') const menu = new Menu(menuFilePath) - menu.addChild('Routing', 'routing', ['basics']) - menu.addChild('Controllers', 'controllers', ['basics']) - menu.addChild('Model', 'model', ['database']) + const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics']} + const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']} + const child3 = {title: 'Model', permalink: 'model', categories: ['database']} + menu.addChild(child1) + menu.addChild(child2) + menu.addChild(child3) yield menu.write() const contents = yield Q.nfcall(fsExtra.readJSON, menuFilePath) expect(contents).deep.equal(menu.items) From 58139b9d50ec6e57063063f2ac02726fdffc94c6 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 28 Jun 2016 22:01:26 +0100 Subject: [PATCH 2/2] chore(release): release 1.0.3 --- CHANGELOG.md | 10 ++++++++++ package.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e5957..28780ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ + +## 1.0.3 (2016-06-28) + + +### Features + +* **menu:** write complete metadata to menu file([98e1106](https://github.com/poppinss/docketjs/commit/98e1106)) + + + ## 1.0.2 (2016-06-18) diff --git a/package.json b/package.json index 733ff1b..865028b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docketjs", - "version": "1.0.2", + "version": "1.0.3", "description": "DocketJs is a battery included Markdown to HTML converter for Node.js. It has everything you need to successfully convert a bunch of markdown files to HTML and generate toc and a menu file.", "main": "index.js", "scripts": {