-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
65 lines (58 loc) · 1.74 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const fs = require('fs')
const marked = require('marked')
const micro = require('micro')
const README = marked(fs.readFileSync('README.md', 'utf-8'))
const IndexHTML = helmet(README, {
title: 'Marked: markdown render service.',
linkCSS: 'https://markdowncss.github.io/splendor/css/splendor.css'
})
const endpoint = async (req, res) => {
if (req.method === 'POST' && req.url === '/') {
const { text, title, linkCSS, inlineCSS, gaId } = await micro.json(req)
if (text) {
res.setHeader('Content-Type', 'text/html; charset=utf-8')
return helmet(marked(text), { title, linkCSS, inlineCSS, gaId })
} else {
return micro.send(res, 400, '"text" field is required')
}
}
if (req.method === 'GET') {
switch (req.url) {
case '/':
return IndexHTML
case '/favicon.ico':
return ''
default:
return 'Use POST request.'
}
}
}
function helmet (bodyHTML, { title = '', linkCSS, inlineCSS, gaId }) {
const linkStyle = linkCSS ? `<link rel="stylesheet" href="${linkCSS}" />` : ''
const inlineStyles = inlineCSS ? `<style>${inlineCSS}</style>` : ''
const googleAnalytics = gaId ? `
<script async src="https://www.googletagmanager.com/gtag/js?id=${gaId}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gaId}');
</script>
` : ''
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>${title}</title>
${linkStyle}
${inlineStyles}
${googleAnalytics}
</head>
<body>
${bodyHTML}
</body>
</html>
`
}
module.exports = endpoint
micro(module.exports).listen(3000)