forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
html.js
182 lines (158 loc) · 4.48 KB
/
html.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const utils = require('./utils');
const path = require('path');
/**
* @exports html
*/
const helpers = module.exports;
/**
* Parse HTML tag attributes from the `options.hash`.
*
* @param {Object} `hash` Helper options hash, e.g. `{foo: 'bar'}`
* @return {String} Stringified attributes, e.g. `foo="bar"`
* @api public
*/
const parseAttr = function parseAttributes(hash) {
return Object.keys(hash).map(function(key) {
const val = String(hash[key]).replace(/^['"]|["']$/g, '');
return key + '="' + val + '"';
}).join(' ');
};
/**
* Stringify attributes on the options `hash`.
*
* ```handlebars
* <!-- value = 'bar' -->
* <div{{attr foo=value}}></div>
* <!-- results in: <div foo="bar"></div>
* ```
* @param {Object} `options`
* @return {String}
* @api public
*/
helpers.attr = function(options) {
const val = parseAttr((options && options.hash) || {});
return val.trim() ? ' ' + val : '';
};
/**
* Add an array of `<link>` tags. Automatically resolves
* relative paths to `options.assets` if passed on the context.
*
* ```handlebars
* <!-- {stylesheets: ['foo.css', 'bar.css']} -->
* {{css stylesheets}}
*
* <!-- results in: -->
* <!-- <link type="text/css" rel="stylesheet" href="foo.css"> -->
* <!-- <link type="text/css" rel="stylesheet" href="bar.css"> -->
* ```
* @param {String|Array} `list` One or more stylesheet urls.
* @return {String}
* @api public
*/
helpers.css = function(list, options) {
if (arguments.length < 2) {
options = list;
list = [];
}
let styles = utils.arrayify(list);
let assets = '';
if (this && this.options) {
assets = this.options.assets || '';
}
if (options.hash.href) {
styles = utils.arrayify(options.hash.href);
}
return styles.map(function(item) {
const ext = path.extname(item);
let fp = item;
if (!/(^\/\/)|(:\/\/)/.test(item)) {
fp = path.posix.join(assets, item);
}
if (ext === '.less') {
return '<link type="text/css" rel="stylesheet/less" href="' + fp + '">';
}
return '<link type="text/css" rel="stylesheet" href="' + fp + '">';
}).join('\n');
};
/**
* Block helper for creating unordered lists (`<ul></ul>`)
*
* @param {Object} `context`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.ul = function(context, options) {
return ('<ul ' + (parseAttr(options.hash)) + '>') + context.map(function(item) {
if (typeof item !== 'string') {
item = options.fn(item);
}
return '<li>' + item + '</li>';
}).join('\n') + '</ul>';
};
/**
* Block helper for creating ordered lists (`<ol></ol>`)
*
* @param {Object} `context`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.ol = function(context, options) {
return ('<ol ' + (parseAttr(options.hash)) + '>') + context.map(function(item) {
if (typeof item !== 'string') {
item = options.fn(item);
}
return '<li>' + item + '</li>';
}).join('\n') + '</ol>';
};
/**
* Returns a `<figure>` with a thumbnail linked to a full picture
*
* @param {Object} `context` Object with values/attributes to add to the generated elements:
* @param {String} `context.alt`
* @param {String} `context.src`
* @param {Number} `context.width`
* @param {Number} `context.height`
* @return {String} HTML `<figure>` element with image and optional caption/link.
* @contributor: Marie Hogebrandt <https://github.com/Melindrea>
* @api public
*/
helpers.thumbnailImage = function(context) {
let figure = '';
let image = '';
const link = context.full || false;
const imageAttributes = {
alt: context.alt,
src: context.thumbnail,
width: context.size.width,
height: context.size.height
};
const figureAttributes = { id: 'image-' + context.id };
const linkAttributes = { href: link, rel: 'thumbnail' };
if (context.classes) {
if (context.classes.image) {
imageAttributes.class = context.classes.image.join(' ');
}
if (context.classes.figure) {
figureAttributes.class = context.classes.figure.join(' ');
}
if (context.classes.link) {
linkAttributes.class = context.classes.link.join(' ');
}
}
figure += '<figure ' + parseAttr(figureAttributes) + '>\n';
image += '<img ' + parseAttr(imageAttributes) + '>\n';
if (link) {
figure += '<a ' + parseAttr(linkAttributes) + '>\n' + image + '</a>\n';
} else {
figure += image;
}
if (context.caption) {
figure += '<figcaption>' + context.caption + '</figcaption>\n';
}
figure += '</figure>';
return figure;
};