38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
import Fastify from 'fastify';
|
|
import Prism from 'prismjs';
|
|
import loadLanguages from 'prismjs/components/index.js';
|
|
|
|
loadLanguages();
|
|
|
|
const fastify = Fastify({
|
|
logger: true
|
|
})
|
|
fastify.get('/', async (request, reply) => {
|
|
return reply.status(200).send(`ok`)
|
|
})
|
|
// Declare a route
|
|
fastify.post('/', function (request, reply) {
|
|
const data = request.body?.trim()
|
|
if(!data.startsWith('```')) {
|
|
return reply.send(request.body)
|
|
}
|
|
const regex = data.match(/^```([a-zA-Z]*)$/m)
|
|
const lang = regex[1]
|
|
if(!lang) {
|
|
return reply.send(request.body)
|
|
}
|
|
const highlighted = Prism.highlight(data.replace(/```[a-zA-Z]*/m, '').replace('```', ''), Prism.languages[lang], lang)
|
|
|
|
// remove any backticks and the language
|
|
const highlightedCleaned = highlighted.trim()
|
|
return `<pre><code>${highlightedCleaned}</code></pre>`
|
|
})
|
|
|
|
// Run the server!
|
|
fastify.listen(process.env.PORT || 3000, '0.0.0.0', function (err, address) {
|
|
if (err) {
|
|
fastify.log.error(err)
|
|
process.exit(1)
|
|
}
|
|
console.log(`Server is now listening on ${address}`)
|
|
}) |