javascript - How to structure a Node Js blog with marked md parser -


currently working on creating simple blog engine using nodejs / express4. want publish blog posts using markdown language.

i have found marked: https://github.com/chjj/marked , perfect need, , have created real basic solution grab data md file , display in client.

the issue having fitting solution correct mvc structure of nodejs application. here have produced, works 1 md file - in index.js:

app.get('/test', function(req, res) {   var path = __dirname + '/node_modules/marked/doc/todo.md';   var file = fs.readfilesync(path, 'utf8');   res.send(marked(file.tostring())); }); 

i have found solution recursively scan particular directory blog posts (in md) using walk:

var walker  = walk.walk('./node_modules/marked/doc', { followlinks: false }); walker.on('file', function(root, stat, next) {   console.log(root + '/' + stat.name);   next(); }); 

how can concatenate multiple md posts 1 html response in correct manor - other page requests go like:

var users = require('./routes/users'); app.use('/users', users); 

in index.js, then

router.get('/', function(req, res, next) {   res.render('users', { title: 'express' }); }); 

in routes users.js

i have tried like:

var string = ''; app.get('/test', function(req, res) {   walker.on('file', function(root, stat, next) {     var path = __dirname + root + '/' + stat.name;     var file = fs.readfilesync(path, 'utf8');     string += (marked(file.tostring()));     next();   });   res.send(string); }); 

this doesn't work, , isn't best solution. suggestions?

something (entirely untested) might help:

app.get('/test', function(req, res) {   var string = '';    walker.on('file', function(root, stat, next) {     var path = __dirname + root + '/' + stat.name;     var file = fs.readfilesync(path, 'utf8');     string += (marked(file.tostring()));     next();   });    walker.on('end', function() {     res.send(string);   });    walker.walk(/* whatever */); }); 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -