Last active
June 5, 2017 00:30
-
-
Save uxdiva/cb373641e9488a055bc96f4d9f53acde to your computer and use it in GitHub Desktop.
gulp function to write jade variables to a json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var through = require('through2'); | |
var matter = require('jade-var-matter'); | |
var jsonfile = require('jsonfile') | |
// Example -- sending jade files to writeToJson function | |
gulp.task('postsmeta', function() { | |
return gulp.src( jadesrc +'posts/*') | |
.pipe(writeToJson( | |
dest = jadesrc +'pages/data.json' // set file path | |
)) | |
}) | |
function writeToJson (dest) { | |
// USAGE: pipes the jade source files in -- these files should have variables in the front matter | |
// example -var name: "Little Engine", desc: "I am a post" | |
// Plugins jade-var-matter, jsonfile, through2 | |
// Optional: "dest" -- sets file path output --> otherwise a data.json is placed at the root | |
// example: dest = jadesrc +'pages/data.json' | |
if (!dest) { | |
dest = './data.json' | |
} | |
// delete current list (so we won't get duplicates) | |
del(dest) | |
return through.obj(function(file, enc, cb){ | |
// read jade variables into an object (uses plugin jade-var-matter ) | |
var obj = matter(file.contents.toString()); | |
// append the items to the destination Json file (used the plugin jsonfile) | |
jsonfile.writeFile(dest, obj, {flag: 'a'}, function (err) { | |
if (err) throw err; | |
}) | |
cb(null); | |
}); | |
} | |
// Note: Json file is not in the correct format for reading multiple level arrays (like with multiple post) | |
// check gist postToJson |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment