From 7e302a6adbb14648b528c1a27123d67591fe5023 Mon Sep 17 00:00:00 2001 From: Jai Chandra Date: Thu, 31 Mar 2016 14:28:49 -0400 Subject: [PATCH] initial commit --- README.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 51 +++++++++++++++++++++++++++++++++ package.json | 26 +++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md index 2e2f348..5c190e6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,83 @@ # metalsmith-models Metalsmith plugin to include JSON models into files and expose the data as metadata on the file object. + +# Installation + +``` +npm install --save metalsmith-models +``` + +# CLI +In metalsmith.json + +``` +{ + "source": "src", + "destination": "build", + "plugins": { + "metalsmith-models": { + "directory": "models" + }, + } +} +``` + +# API + +``` +var Metalsmith = require('metalsmith'); +var models = require('metalsmith-models'); + +var metalsmith = new Metalsmith(__dirname) +.use(models({ + directory: "models" +})); +``` + +## Options +### options.directory +Type: `String` Default value: `models` Source directory of all JSON files. + +# Usage +JSON file can be loaded into the files using YAML front matter as below: + +##### data_file.json +``` +{ + "key1": "value 1", + "key2": "value 2" +} +``` + +##### page.hbs +``` +--- +model: data_file +--- +``` +Where data_file is the name of the JSON file that is placed under the models directory (See CLI Usage or API section). + +The path of the JSON files is always relative to the root directory (Ex: models). + +##### How to use the data. +In case of pages using handlebars: +``` +{{model.key1}} +``` + + +### Loading multile JSON files +If you need to load multiple JSON files in a page, use below format: +``` +--- +model: + obj1: data_file1 + obj2: data_file2 +--- +``` +##### How to use the data. +In case of pages using handlebars: +``` +{{model.obj1.key1}} +{{model.obj2.key1}} +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..6ebc502 --- /dev/null +++ b/index.js @@ -0,0 +1,51 @@ +var debug = require('debug')('metalsmith-models'); + +var fileCount = 0; +module.exports = function(opts) { + + var dir = opts.directory || 'models'; + + return function(files, metalsmith, done) { + fileCount = 0; + Object.keys(files).forEach(function(file) { + + var filePath; + debug('stringifying file: %s', file); + var data = files[file]; + + if (typeof data.model === 'string') { + filePath = metalsmith.path(dir, data.model) + '.json'; + readFile(filePath, metalsmith, done, function(err, res) { + try { + data.model = JSON.parse(res ? res.contents : {}); + } catch (e) { + throw new Error('Error loading data for file ' + file + '\n\n' + err); + } + }); + + } else if (typeof data.model === 'object') { + Object.keys(data.model).forEach(function(key) { + filePath = metalsmith.path(dir, data.model[key]) + '.json'; + + readFile(filePath, metalsmith, done, function(err, res) { + try { + data.model[key] = JSON.parse(res ? res.contents : {}); + } catch (e) { + throw new Error('Error loading data for file ' + file + '\n\n' + err); + } + }); + }); + } + }); + }; +} + +function readFile(path, metalsmith, done, callback) { + fileCount++; + metalsmith.readFile(path, function(err, res) { + --fileCount; + callback(err, res); + if (fileCount == 0) + done(); + }); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..8ae4f1b --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "metalsmith-models", + "version": "0.0.1", + "description": "Metalsmith plugin to include JSON models into files and expose the data as metadata on the file object.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/jaichandra/metalsmith-models.git" + }, + "keywords": [ + "metalsmith", + "json", + "models" + ], + "author": "Jai Langoju ", + "license": "MIT", + "devDependencies": { + "metalsmith": "^2.1.0", + "metalsmith-in-place": "^1.4.3", + "metalsmith-layouts": "^1.6.4", + "metalsmith-markdown": "0.2.1" + } +}