Skip to content

Commit

Permalink
Refactor navigation.js
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ju committed Jan 5, 2018
1 parent f609e12 commit 50d966e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
21 changes: 21 additions & 0 deletions lib/file-helper.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
31 changes: 17 additions & 14 deletions lib/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 50d966e

Please sign in to comment.