Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
171d892
commit 7e302a6
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <jai.chandra@gmail.com>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"metalsmith": "^2.1.0", | ||
"metalsmith-in-place": "^1.4.3", | ||
"metalsmith-layouts": "^1.6.4", | ||
"metalsmith-markdown": "0.2.1" | ||
} | ||
} |