Last active
December 30, 2018 23:01
-
-
Save overra/323d8db1aa4f6734fec14f07e2c65625 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('isomorphic-unfetch') | |
const { json, send } = require('micro') | |
const { readFileSync } = require('fs') | |
const { resolve } = require('path') | |
const tagsToReplace = {'&': '&', '<': '<', '>': '>'}; | |
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