-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (37 loc) · 986 Bytes
/
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
/**
* Dependencies.
*/
var marked = require('marked')
var through = require('through')
/**
* Render and highlight vomit component.
*
* @param {Object} data
* @api public
*/
module.exports = function(data) {
return through(function(chunk) {
var queue = this.queue.bind(this)
var str = chunk.toString()
var i = 0
str.replace(/\`\`\`vomit((.|\n)*)\`\`\`/g, function(all, expr, _, idx) {
queue(marked(str.substring(i, idx)))
queue(render(expr, data))
i = idx + all.length
})
queue(marked(str.substring(i)))
})
}
/**
* Render code and live example.
*
* @param {String} code
* @param {Object} data
* @api private
*/
function render(code, data) {
var js = '```js' + code + '```'
return `<div class="vomit-snippet"><div class="column">${marked(js)}</div><div class="column"><script>
(function() {${code}document.currentScript.parentElement.appendChild(component(${JSON.stringify(data.data)}))
})()</script></div></div>`
}