Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:18:45+00:00 2026-06-14T17:18:45+00:00

So I decided to redo a blog and am using markdown, I have three

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:18:45+00:00Added an answer on June 14, 2026 at 5:18 pm

    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);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I decided to learn Django Forms. For a while now, I have been using
I decided to try to my hand at this, and have had a somewhat
I decided to have a go at PDO. The function below should move a
I decided to create a currency converter in Java, and have it so that
Im using a Core Data model for my iPhone app. I have been looking
I decided to use UIScrollView in my application since I have to scroll the
I decided to make a 2d game in Java (using the slick2d library and
I decided to have a go at building Windows Metro style apps with the
I decided to try creating a simple C wrapper for the V8 API using
I decided to implement a blog system on Google AppEngine. But: I don't want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.