Wednesday, 10 May 2017

Node.js parse folder of Markdown files and create an RSS

I am new to node and I am working on generating an RSS Feed from a folder with some Markdown files.

This is how I am proceeding:

(function(opt){
    /**
     * Initialization of dependencies and variables
     */
    var dirname = '../rss-feeds/';
    var promise = require('bluebird');
    var fs = promise.promisifyAll(require('fs'));
    var markdown = require('markdown').markdown;

    var rss = require('rss');
    var options = JSON.parse(fs.readFileSync('feed-options.json', 'utf-8'));
    var feed = new rss(options);

    // console.log(options);

    /**
     * get all files
     */
    var getFiles = function(path){
        return fs.readdirAsync(path);
    };

    /**
     * get the content of the file
     */
    var getContent = function(filename){
        return fs.readFileSync(dirname + filename, 'utf-8');
    };

    /**
     * get an RSS item
     */
    var getRssItem = function(content){
        var markdownFile = markdown.parse(content);
        var rssItem = {
            title: markdownFile.title,
            description: markdownFile.description,
            categories: options.categories,
            author: markdown.author
        };
        return rssItem;
    };

    /**
     * Execute the RSS creation
     */
    getFiles(dirname)
        .map(function(filename){
            return getContent(filename);
        })
        .then(function(content){            
            console.log('add item');
            feed.item(getRssItem(content));
        })
        .then(function(){
            console.log('done');
            var xml = feed.xml({ indent:true });            
        });

}).call(this);

I am reading a folder with 3 .md files but it reads only one:

> $ node ./main.js
> add item
> done



via Raffaeu

No comments:

Post a Comment