Reputation: 1361
So I decided to redo a blog and am using markdown, I have three markdown files in a folder called blog and was wanting to list them in order by date. Problem is Im not sure what I did to screw up my array.
Heres my routes.js file
exports.list = function(req, res){
var walk = require('walk'), fs = require('fs'), options, walker;
var walker = walk.walk('blog');
var fs = new Array();
walker.on("file", function(root,file,next){
var f = root + "/" + file['name'].substring(0, file['name'].lastIndexOf('.'));
// push without /blog prefix
if (file['name'].substr(-2) == "md") {
fs.push(f.substring(f.indexOf('/')));
console.log(fs);
}
next();
});
walker.on("end", function() {
var model = {
layout:'home.hbs',
title: 'Entries',
files: fs
};
res.render('home.hbs', model)
});
};
But what I return in terminal is this:
[ '/first' ]
[ '/first', '/second' ]
[ '/first', '/second', '/third' ]
Say I just wanted to display the first two and have them sorted by date in a markdown file, like so:
Title: Lorem Ipsum dolor sit amet
Date: January 2d, 2012
# test message
Whats wrong with my array/rest of code
Upvotes: 1
Views: 654
Reputation: 2228
First thing I noticed is redeclaring of fs
variable. In line 2 it's Node's Filesystem module, in line 4 it's new Array()
(which should be []
if you ask me).
I'm also not sure what is walker
module for and, since it's github repo was removed and npm package is outdated, I recommend you use raw filesystem module API to list files, probably path module to handle your files locations and some async to glue it together:
// `npm install async` first.
// https://github.com/caolan/async
var fs = require('fs');
var async = require('async');
// Lists directory entries in @dir,
// filters those which names ends with @extension.
// calls callback with (err, array of strings).
//
// FIXME:
// Mathes directories - solution is fs.stat()
// (could also sort here by mtime).
function listFiles(dir, extension, callback) {
fs.readdir(dir, function(err, files) {
if (err) {
console.error('Failed to list files in `%s`: %s', dir, err);
return callback(err);
}
var slicePos = -extension.length;
callback(null, files.filter(function(filename) {
return (extension == filename.slice(slicePos))
}));
});
}
// Sorts markdown based on date entry.
// Should be based on `mtime`, I think,
// since reading whole file isn't great idea.
// (See fs.Stats.)
// At lease add caching or something, you'll figure :)
//
// Also, you better get yourself a nice markdown parser,
// but for brevity I assume that first 2 lines are:
// Title: Some Title
// Date: Valid Javascript Datestring
function sortMarkdown(pathes, callback) {
async.sortBy(pathes, function(fileName, cb) {
// Following line is dirty!
// You should probably pass absolute pathes here
// to avoid errors. Path and Fs modules are your friends.
var md = __dirname + '/blogs/' + fileName;
fs.readFile(md, 'utf8', function(err, markdown) {
if (err) {
console.error('Failed to read `%s`: %s', md, err);
return cb(err);
}
// Get second line of md.
var date = markdown.split('\n')[1];
// Get datestring with whitespaces removed.
date = date.split(':')[1].trim();
// Get timestamp. Change with -ts
// to reverse sorting order.
var ts = Date.parse(date);
// Tell async how to sort; for details, see:
// https://github.com/caolan/async#sortBy
cb(null, ts);
});
}, callback);
}
function listSortedMarkdown(dir, callback) {
// Async is just great!
// https://github.com/caolan/async#waterfall
async.waterfall([
async.apply(listFiles, dir, '.md'),
sortMarkdown,
], callback);
}
listSortedMarkdown(__dirname + '/blogs', function(err, sorted) {
return err ? console.error('Error: %s', err)
: console.dir(sorted);
});
Upvotes: 3