Skip to content

Instantly share code, notes, and snippets.

@alex2600
Last active May 9, 2019 19:00
Show Gist options
  • Save alex2600/56e7d1c6343fd5e83a2f18ab1ecf2437 to your computer and use it in GitHub Desktop.
Save alex2600/56e7d1c6343fd5e83a2f18ab1ecf2437 to your computer and use it in GitHub Desktop.
Wrapper function for gulp stream pipelines using highland.js (usage here: wrap markdown html output into a handlebars template)
const h = require("highland")
const handlebars = require("handlebars")
// handlebars wrapper pipeline for gulp object stream
const wrapIt = h.pipeline(function (stream) {
return h(stream) // wrap node stream into a highland stream
// .tap(vinylObject => console.log(vinylObject.path))
.map(function (vinylObject) {
const data = vinylObject.contents.toString() // convert marktdown html to string
const tpl = fs.readFileSync(tplFile, "utf8") // read handlebars template
const fn = handlebars.compile(tpl) // compile template to fn
const data2 = fn({md: data}) // inject markdown html into template
vinylObject.contents = Buffer.from(data2) // update gulp vinyl object
return vinylObject
})
.toNodeStream({objectMode: true}) // convert to normal node stream so gulp can handle it
})
gulp.task("docs", function () {
return gulp.src(docFile)
.pipe(marked())
.pipe(wrapIt)
.pipe(gulp.dest(docDirDist))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment