Serve docs directly from live badge handler

pull/282/head
Amio 2019-06-01 21:53:29 +08:00
rodzic fa35ebc5b2
commit dd1fdc2c0c
4 zmienionych plików z 55 dodań i 53 usunięć

Wyświetl plik

@ -1,20 +1,26 @@
import serveMarked from 'serve-marked'
import serve404 from '../libs/serve-404'
import serveHelp from '../libs/serve-help'
import { liveBadgeList } from '../libs/badge-list'
const badges = require('../static/.gen/badges.json')
import genHelp from '../libs/gen-help'
// Handles `/docs/:name`
export default async function (req, res) {
const [ , topic, name ] = req.url.split('/')
const helpMarkdown = genHelp(name)
if (liveBadgeList.includes(name)) {
if (helpMarkdown) {
console.info(100, `${name}: ${req.url}`)
const foundBadge = badges.live.find(b => b.id === name)
if (foundBadge) {
return serveHelp(req, res, foundBadge)
}
return serveMarked(helpMarkdown, {
title: `${name} | Badgen`,
inlineCSS,
})(req, res)
}
serve404(req, res)
}
const inlineCSS = `
.markdown-body { max-width: 800px }
li > img { vertical-align: middle; margin: 0.2em 0; font-size: 12px }
li > img + a { font-family: monospace; font-size: 0.9em }
li > img + a + i { color: #AAA }
`

Wyświetl plik

@ -6,11 +6,10 @@ import {
BadgenServeHandlerArgs as Args
} from '../libs/badgen-serve'
const help = `## URL
/runkit/cal-badge-icd0onfvrxx6/Asia/Shanghai
endpoint-id path-args (optional)
const help = `
https://badgen.net/runkit/cal-badge-icd0onfvrxx6/Asia/Shanghai
endpoint-id path-args (optional)
## RunKit Endpoint
@ -27,7 +26,7 @@ If you are not familiar with RunKit endpoint, [this guide](https://runkit.com/do
https://cal-badge-icd0onfvrxx6.runkit.sh
\`\`\`
it would response with a JSON like:
which would response with a JSON like:
\`\`\`
{

35
libs/gen-help.ts 100644
Wyświetl plik

@ -0,0 +1,35 @@
import path from 'path'
import { liveBadgeList } from './badge-list'
const liveBadgeDB = liveBadgeList.reduce((accu, curr) => {
const { meta, handlers } = require(path.resolve(__dirname, `../endpoints/${curr}`))
const { title, examples, help } = meta
accu[curr] = {
id: curr,
title,
examples,
routes: Object.keys(handlers),
help
}
return accu
}, {})
export default function genHelp (id) {
const meta = liveBadgeDB[id]
if (!meta) {
return ''
}
const { examples, routes, help } = meta
const Docs = `# /${id}\n\n${help || ''}`
const schemeLinks = routes.map(r => `- \`${r}\``)
const Schemes = `## Schemes\n\n${schemeLinks.join(' \n')}`
// @ts-ignore
const exampleList = Object.entries(examples)
.map(([url, desc]) => `- ![${url}](${url}) [${url}](${url}) <i>${desc}</i>`)
const Examples = `## Examples\n\n${exampleList.join('\n')}`
return [Docs, Schemes, Examples].join('\n\n')
}

Wyświetl plik

@ -1,38 +0,0 @@
import serveMarked from 'serve-marked'
type BadgenHelpParams = {
id: string,
title: string,
examples: { [url: string]: string }
routes: string[],
help?: any
}
function genMarkdown ({ id, help, examples, routes }: BadgenHelpParams) {
const Docs = `# /${id}\n\n${help || ''}`
const schemeLinks = routes.map(r => `- \`${r}\``)
const Schemes = `## Schemes\n\n${schemeLinks.join(' \n')}`
const exampleList = Object.entries(examples)
.map(([url, desc]) => `- ![${url}](${url}) [${url}](${url}) <i>${desc}</i>`)
const Examples = `## Examples\n\n${exampleList.join('\n')}`
const md = [Docs, Schemes, Examples].join('\n\n')
return md
}
export default function serveHelp (req, res, params: BadgenHelpParams) {
const md = genMarkdown(params)
return serveMarked(md, {
title: `${params.title} | Badgen`,
inlineCSS,
})(req, res)
}
const inlineCSS = `
.markdown-body { max-width: 800px }
li > img { vertical-align: middle; margin: 0.2em 0; font-size: 12px }
li > img + a { font-family: monospace; font-size: 0.9em }
li > img + a + i { color: #AAA }
`