-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
code.js
80 lines (70 loc) · 2.06 KB
/
code.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
'use strict';
var fs = require('fs');
var path = require('path');
var utils = require('./utils');
var helpers = module.exports;
/**
* Embed code from an external file as preformatted text.
*
* ```handlebars
* {{embed 'path/to/file.js'}}
* <!-- optionally specify the language to use -->
* {{embed 'path/to/file.hbs' 'html')}}
* ```
*
* @param {String} `filepath` filepath to the file to embed.
* @param {String} `language` Optionally specify the language to use for syntax highlighting.
* @return {String}
* @api public
*/
helpers.embed = function embed(filepath, ext) {
ext = typeof ext !== 'string' ? path.extname(filepath).slice(1) : ext;
var code = fs.readFileSync(filepath, 'utf8');
if (ext === 'markdown' || ext === 'md') {
ext = 'markdown';
// if the string is markdown, escape backticks
code = code.split('`').join('`');
}
return utils.block(code, ext).trim() + '\n';
};
/**
* Embed a GitHub Gist using only the id of the Gist
*
* ```handlebars
* {{gist "12345"}}
* ```
* @param {String} `id`
* @return {String}
* @api public
*/
helpers.gist = function(id) {
return utils.tag('script', {src: 'https://gist.github.com/' + id + '.js'});
};
/**
* Generate the HTML for a jsFiddle link with the given `params`
*
* ```handlebars
* {{jsfiddle id="0dfk10ks" tabs="true"}}
* ```
* @param {Object} `params`
* @return {String}
* @api public
*/
helpers.jsfiddle = function jsFiddle(options) {
var attr = Object.assign({}, options && options.hash);
if (typeof attr.id === 'undefined') {
throw new Error('jsfiddle helper expects an `id`');
}
attr.id = 'http://jsfiddle.net/' + attr.id;
attr.width = attr.width || '100%';
attr.height = attr.height || '300';
attr.skin = attr.skin || '/presentation/';
attr.tabs = (attr.tabs || 'result,js,html,css') + attr.skin;
attr.src = attr.id + '/embedded/' + attr.tabs;
attr.allowfullscreen = attr.allowfullscreen || 'allowfullscreen';
attr.frameborder = attr.frameborder || '0';
delete attr.tabs;
delete attr.skin;
delete attr.id;
return utils.tag('iframe', attr);
};