From 50d966e6762bfd1e26762575c21906c7da503f93 Mon Sep 17 00:00:00 2001 From: alex-ju Date: Fri, 5 Jan 2018 11:10:31 +0000 Subject: [PATCH] Refactor navigation.js --- lib/file-helper.js | 21 +++++++++++++++++++++ lib/navigation.js | 31 +++++++++++++++++-------------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/file-helper.js b/lib/file-helper.js index 68107fe7f3..bae61b83a6 100644 --- a/lib/file-helper.js +++ b/lib/file-helper.js @@ -1,6 +1,7 @@ 'use strict' const fs = require('fs') +const path = require('path') const nunjucks = require('nunjucks') // This helper function takes a path of a file and @@ -52,3 +53,23 @@ exports.getHTMLCode = path => { return html } + +// This helper function takes a path and +// returns the directories found under that path +exports.getDirectories = itemPath => { + let files + let directories + try { + files = fs.readdirSync(itemPath) + } catch (err) { + if (err.code === 'ENOENT') { + console.log(err.message) + } else { + throw err + } + } + if (files) { + directories = files.filter(filePath => fs.statSync(path.join(itemPath, filePath)).isDirectory()) + } + return directories +} diff --git a/lib/navigation.js b/lib/navigation.js index 275374ec08..dc39e152b6 100644 --- a/lib/navigation.js +++ b/lib/navigation.js @@ -5,21 +5,21 @@ // metalsmith: object containing global information such as meta data // done: function which must be called when the plugin has finished working -const fs = require('fs') -const path = require('path') const paths = require('../config/paths.json') const navigation = require('../config/navigation.json') +const fileHelper = require('../lib/file-helper.js') + module.exports = function () { return function (files, metalsmith, done) { // iterate main navigation item defined in navigation.json for (let item in navigation) { - // if is not homepage look for subitems - if (navigation[item].url !== '') { + // if we have a navigation url and it is not homepage then look for subitems + if (navigation[item].url && navigation[item].url !== '') { // define source path let itemPath = paths.source + navigation[item].url // get directories under main navigation item (e.g. ['breadcrumbs', 'checkboxes', ...]) - let directories = fs.readdirSync(itemPath).filter(filePath => fs.statSync(path.join(itemPath, filePath)).isDirectory()) + let directories = fileHelper.getDirectories(itemPath) // if we have directories convert them into menu items if (directories) { // initialise child navigation items as array @@ -28,16 +28,19 @@ module.exports = function () { for (let dir in directories) { let url = navigation[item].url + '/' + directories[dir] let frontmatter = files[url + '/index.html'] - let subitem = { - 'url': url, - 'label': frontmatter.title - } - // if frontmatter contains `theme` data, attach it to navigation item for grouping - if (frontmatter.theme) { - subitem.theme = frontmatter.theme + // if we have frontmatter for that file, create subitem + if (frontmatter) { + let subitem = { + 'url': url, + 'label': frontmatter.title + } + // if frontmatter contains `theme` data, attach it to navigation item for grouping + if (frontmatter.theme) { + subitem.theme = frontmatter.theme + } + // add subitem to navigation + navigation[item].items.push(subitem) } - // add subitem to navigation - navigation[item].items.push(subitem) } } }