/
index.js
117 lines (97 loc) · 3.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Assemble + Handlebars Helpers: {{eachPartial}}, {{mdpartial}}
*
* Copyright (c) 2014 Alex Bogdanovski
* Licensed under the MIT License
*/
"use strict";
var _ = require("lodash");
var path = require("path");
var matter = require("gray-matter");
var marked = require("marked");
var minimatch = require("minimatch");
// Export helpers
module.exports.register = function(Handlebars, opts, params) {
var assemble = params.assemble;
var grunt = params.grunt;
opts = opts || {};
opts.marked = opts.marked || {};
marked.setOptions(opts.marked);
var omit = function(target) {
return _.omit(target, "pages", "pagination", "collections");
};
/**
* {{eachPartial}} - block helper that iterates over multiple partials
* Partials can be filtered using globbing patterns.
*
* @param {String} glob The pattern to match against the partial name
* @param {Object} options The options to pass to the partial
* @return {String} Returns compiled Handlebars string
* @example: {{eachPartial "*.md"}}
*/
Handlebars.registerHelper("eachPartial", function(glob, options) {
if (!_.isString(glob)) {
return;
}
var files = assemble.partials.filter(minimatch.filter(glob));
var ret = "";
for (var i = 0, j = files.length; i < j; i++) {
var src = files[i];
var name = path.basename(src, path.extname(src));
ret = ret + options.fn(_.extend({}, grunt.config.data, omit(opts), {
partialSrc: src,
partialName: name
}, assemble.options.data[name]));
}
return ret;
});
/**
* {{mdpartial}} - block helper that renders Markdown and also
* shares data to and from the partial. Similar to {{partial}}
* but for Markdown partials.
*
* @param {String} name The name of the partial to use
* @param {Object} context The context to pass to the partial
* @param {Object} options The options to pass to the partial
* @return {String} Returns compiled Handlebars string
* @example: {{mdpartial "partial-name"}}
*/
Handlebars.registerHelper("mdpartial", function(name, context, options) {
if (!Array.isArray(assemble.partials)) {
assemble.partials = [assemble.partials];
}
if (!_.isString(name)) {
return;
}
if (!options) {
options = context;
context = {};
}
// find by name
var filepath = _.first(_.filter(assemble.partials, function(fp) {
return path.basename(fp, path.extname(fp)) === name;
}));
// not found - find by path
if (!filepath || filepath.length <= 0) {
filepath = name;
}
// Process context, using YAML front-matter,
// grunt config and Assemble options.data
var pageObj = matter.read(filepath) || {};
var pageCtx = pageObj.context || {};
var pageData = pageObj.data || {};
// Remove page content from `this` and `opts` before creating new context
context = _.extend({}, pageData, pageCtx, context);
// process any templates inside context property values
context = grunt.config.process(context);
var compiledTemplate = Handlebars.compile(pageObj.content || "");
var output = compiledTemplate(context).replace(/^\s+/, "");
var content = (path.extname(filepath) === ".md") ? marked(output) : output;
context = _.extend(context, {
content: new Handlebars.SafeString(content),
partialSrc: filepath,
partialName: path.basename(filepath, path.extname(filepath))
});
return options.fn(context);
});
};