front-matter icon indicating copy to clipboard operation
front-matter copied to clipboard

Streaming Support

Open finnp opened this issue 9 years ago • 2 comments

Would be cool if there were an method like this:

fs.createReadStream('file.html')
   .pipe(frontMatter.stream(function(attributes, stream) {
        console.log(attributes)
        stream.pipe(fs.createWriteStream('withoutyml.html'))
   })

finnp avatar Apr 09 '15 23:04 finnp

This is a great idea, I don't think it would be that hard to implement either.

What do you think of having a normal stream signature and emitting the attributes, maybe something like:

var fm = require('front-matter')
var stream = fm.createStream()

stream.on('attributes', function(attributes) {
  console.log(attributes)
})

fs.createReadStream('my-file.html')
  .pipe(stream)
  .pipe(fs.createWriteStream('withoutyml.html'))

jxson avatar Apr 10 '15 02:04 jxson

Your API was my first idea, but I think often you want to use the attributes to change the stream flow. Like in this example:

var fm = require('front-matter')
var stream = fm.createStream()

fs.createReadStream('my-file.html')
  .pipe(stream(function(attributes, stream) {
      (attributes.markdown ? stream.pipe(markdown2html()) : stream)
         .pipe(fs.createWriteStream('withoutyml.html'))
  })

Alternatively maybe there could be a callback that you can pass a transform stream:

var stream = fm.createStream(function(attributes, cb) {
   if(attributes.markdown) cb(null, markdown2html())
   else cb()
})
fs.createReadStream('my-file.html')
   .pipe(stream)
   .pipe(fs.createWriteStream('withoutyml.html'))

But I feel like the first solution is easier. What do you think?

finnp avatar Apr 10 '15 07:04 finnp