Skip to content

Instantly share code, notes, and snippets.

@mistergraphx
Created January 6, 2017 10:53
Show Gist options
  • Save mistergraphx/5daab215e4deba79feba684000e84891 to your computer and use it in GitHub Desktop.
Save mistergraphx/5daab215e4deba79feba684000e84891 to your computer and use it in GitHub Desktop.
Create a navigation with a .md file structure, read title and description from frontmatter datas
/* Building an index of elements : pages,post, …
https://medium.com/@bushwazi/super-simple-static-site-generation-with-node-jade-gulp-and-json-9ded83508fa6#.hvt5p0bqm
https://github.com/Bushwazi/static-site-generation-with-jade-gulp/blob/master/_src/gulpfile.js
*/
// https://www.npmjs.com/package/gulp-gray-matter
// https://www.npmjs.com/package/gray-matter#options
var frontmatter = require('gulp-gray-matter')
var fsRecurs = require('fs-readdir-recursive')
var matter = require('gray-matter');
var util = require('util')
var pageDir = './src/pages/'
var summaryJSON = new Object()
function buildIndex(baseDir,objectType){
var counter = 0 ;
files = fsRecurs(baseDir)
files.map(function(url,ind,arr){
//current = fs.readFileSync(pageDir + url, 'utf-8')
file = matter.read(baseDir + url)
if(path.basename(file.path,'.md') === 'index')
url = '/'
else
url = path.dirname(file.path).replace(baseDir, '') + '/' + path.basename(file.path,'.md') + '.html'
//console.log(path.dirname(file.path).replace(pageDir, ''))
summaryJSON[objectType + '-' + counter] = {
'path': file.path,
'url': url,
'title': file.data.title,
'description': file.data.description
}
counter++;
});
return summaryJSON;
}
console.log(buildIndex(pageDir,'page'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment