Skip to content

Instantly share code, notes, and snippets.

@overra
Last active December 30, 2018 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overra/323d8db1aa4f6734fec14f07e2c65625 to your computer and use it in GitHub Desktop.
Save overra/323d8db1aa4f6734fec14f07e2c65625 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://unpkg.com/prismjs@1.15.0/themes/prism.css" />
<script src="https://unpkg.com/prismjs@1.15.0/prism.js"></script>
</head>
<body>
const fetch = require('isomorphic-unfetch')
const { json, send } = require('micro')
const { readFileSync } = require('fs')
const { resolve } = require('path')
const tagsToReplace = {'&': '&amp;', '<': '&lt;', '>': '&gt;'};
const replaceTag = tag => tagsToReplace[tag] || tag
const replaceTags = str => str.replace(/[&<>]/g, replaceTag)
const template = readFileSync(resolve(__dirname, './static/code.html'), 'utf8');
const getGist = url = fetch(url).then(res => res.json())
module.exports = async (req, res) => {
const id = req.url.split('/').pop()
if (id === 'favicon.ico') return send(res, 404)
const { files } = await getGist(`https://api.github.com/gists/${id}`)
const code = Object.entries(files).reduce((code, [name, info]) => {
const files = `
<pre>${name}</pre>
<pre>
<code class="language-${info.language.toLowerCase()}">
${replaceTags(info.content)}
</code>
</pre>
`
return code + file
}, '')
send(res, 200, template + code)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment