-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
code.js
executable file
·57 lines (51 loc) · 1.63 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
'use strict';
const fs = require('fs');
const path = require('path');
const utils = require('../utils');
const object = require('./object');
/**
* Embed code from an external file as preformatted text.
*
* ```js
* <%= embed('path/to/file.js') %>
*
* // specify the language to use
* <%= embed('path/to/file.hbs', 'html') %>
* ```
* @param {String} `fp` filepath to the file to embed.
* @param {String} `language` Optionally specify the language to use for syntax highlighting.
* @return {String}
* @api public
*/
exports.embed = (fp, ext) => {
ext = typeof ext !== 'string' ? path.extname(fp).slice(1) : ext;
let code = fs.readFileSync(fp, 'utf8');
// if the string is markdown, escape backticks
if (ext === 'markdown' || ext === 'md') {
code = code.split('`').join('`');
}
return utils.toCodeBlock(code, ext) + '\n';
};
/**
* Generate the HTML for a jsFiddle link with the given `params`
*
* ```js
* <%= jsfiddle({id: '0dfk10ks', {tabs: true}}) %>
* ```
* @param {Object} `params`
* @return {String}
* @api public
*/
exports.jsfiddle = attr => {
if (!attr || !object.isPlainObject(attr)) return '';
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';
attr = object.omit(attr, ['id', 'tabs', 'skin']);
return '<iframe ' + utils.toAttributes(attr) + '></iframe>';
};