badgen/preview/serve.js

55 wiersze
1.6 KiB
JavaScript
Czysty Zwykły widok Historia

2019-07-06 08:04:16 +00:00
const fs = require('fs')
2018-07-11 03:03:51 +00:00
const path = require('path')
2018-07-20 02:55:51 +00:00
const http = require('http')
const url = require('url')
2018-07-11 03:26:10 +00:00
const qs = require('querystring')
2018-07-11 03:03:51 +00:00
const serveMarked = require('serve-marked')
2018-07-01 08:11:23 +00:00
const badgen = require('..')
2019-01-02 12:49:23 +00:00
const icons = require('../test/assets/icon-data-uri.js')
2018-07-20 02:55:51 +00:00
2018-07-01 08:11:23 +00:00
const serveBadge = (req, res) => {
const { pathname, query } = url.parse(req.url)
2019-07-06 08:04:16 +00:00
const { label, style, icon, iconWidth, labelColor } = qs.parse(query)
2018-08-25 07:01:55 +00:00
const [ subject, status, color ] = pathname.split('/').splice(1)
2018-07-11 03:26:10 +00:00
.map(s => qs.unescape(s))
2018-08-24 04:33:46 +00:00
res.statusCode = 200
res.setHeader('Content-Type', 'image/svg+xml;charset=utf-8')
2019-07-06 08:04:16 +00:00
res.end(badgen({ label, labelColor, subject, status, color, style, icon: icons[icon], iconWidth }))
2018-07-01 08:11:23 +00:00
}
2019-07-06 08:04:16 +00:00
const md = fs.readFileSync(path.join(__dirname, 'preview.md'), 'utf8')
const inlineCSS = `
body { color: #333; padding-bottom: 5em; max-width: 800px }
a { text-decoration: none; color: #06D }
a:hover { text-decoration: underline }
table { border-spacing: 0 }
td { padding: 0 1em 0 0; height: 24px; font: 14px/14px sans-serif }
td a { font: 14px/14px monospace; vertical-align: top }
img { height: 30px }
`
2018-07-11 03:03:51 +00:00
2018-07-11 05:40:30 +00:00
const serve404 = (req, res) => {
res.writeHead(404)
res.end()
}
const port = 3000
2018-07-11 03:03:51 +00:00
http.createServer((req, res) => {
switch (req.url) {
case '/':
return serveMarked(md, {
title: 'badgen preview',
preset: 'merri',
inlineCSS
})(req, res)
case '/favicon.ico':
return serve404(req, res)
default:
return serveBadge(req, res)
2018-07-11 03:03:51 +00:00
}
}).listen(port)
console.log(`Preview served at http://localhost:${port}`)