-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
oembed.js
58 lines (50 loc) · 1.33 KB
/
oembed.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
/*
* See oEmbed standard here: https://oembed.com/
*/
const url = require('url')
const { send } = require('micro')
const toIFrame = (url, width, height) =>
`<iframe
src="https://carbon.now.sh/embed${url}"
width="${width}px"
height="${height}px"
style="width:${width}px; height:${height}px; border:0; overflow:auto;"
sandbox="allow-scripts allow-same-origin"
scrolling="auto">
</iframe>
`
module.exports = (req, res) => {
let embedUrl = req.query.url
try {
embedUrl = decodeURIComponent(embedUrl)
} catch (e) {
// eslint-disable-next-line
console.log(e)
/* URL is already decoded */
}
try {
const { query: queryString, pathname } = url.parse(embedUrl)
const snippetID = pathname.split('/').pop()
const width = Math.min(Number(req.query.maxwidth) || Infinity, 1024)
const height = Math.min(Number(req.query.maxheight) || Infinity, 480)
const obj = {
version: '1.0',
type: 'rich',
provider_name: 'Carbon',
width,
height,
html: toIFrame(
`${snippetID && snippetID !== 'undefined' ? `/${snippetID}` : ''}?${
queryString ? queryString : ''
}`,
width,
height
)
}
return send(res, 200, obj)
} catch (e) {
// eslint-disable-next-line
console.error(e)
return send(res, 500, e.message)
}
}