kopia lustrzana https://github.com/badgen/badgen.net
Serve markdown docs from badge meta
rodzic
2a69d57a34
commit
f54a767c0f
|
@ -1,17 +1,19 @@
|
|||
import path from 'path'
|
||||
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')
|
||||
|
||||
// Handles `/docs/:name`
|
||||
export default async function (req, res) {
|
||||
const [ , , name ] = req.url.split('/')
|
||||
const [ , topic, name ] = req.url.split('/')
|
||||
|
||||
if (name) {
|
||||
if (liveBadgeList.includes(name)) {
|
||||
console.info(100, `${name}: ${req.url}`)
|
||||
try {
|
||||
const handlerModulePath = path.join(__dirname, name)
|
||||
const { help, examples, handlers } = await import(handlerModulePath)
|
||||
if (help || examples) {
|
||||
return serveHelp(req, res, name, { help, examples, handlers })
|
||||
const foundBadge = badges.live.find(b => b.id === name)
|
||||
if (foundBadge) {
|
||||
return serveHelp(req, res, name, foundBadge)
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.code !== 'MODULE_NOT_FOUND') {
|
||||
|
|
10
index.ts
10
index.ts
|
@ -4,6 +4,7 @@ import http from 'http'
|
|||
import serveHandler from 'serve-handler'
|
||||
|
||||
import serve404 from './libs/serve-404'
|
||||
import serveDocs from './endpoints/docs'
|
||||
|
||||
const sendError = (req, res, error) => {
|
||||
res.statusCode = 500
|
||||
|
@ -27,6 +28,11 @@ const server = http.createServer(async (req, res) => {
|
|||
return serveHandler(req, res, { public: path.join(__dirname, 'out') })
|
||||
}
|
||||
|
||||
// handle `/docs/:name`
|
||||
if (req.url!.startsWith('/docs/')) {
|
||||
return serveDocs(req, res)
|
||||
}
|
||||
|
||||
// handle endpoints
|
||||
const handlerName = badgeHandlers.find(h => req.url!.startsWith(`/${h}/`))
|
||||
|
||||
|
@ -49,4 +55,8 @@ if (require.main === module) {
|
|||
server.listen(3000)
|
||||
}
|
||||
|
||||
process.on('unhandledRejection', e => {
|
||||
console.error(500, e)
|
||||
})
|
||||
|
||||
export default server
|
||||
|
|
|
@ -44,12 +44,17 @@ export const liveBadgeList = [
|
|||
'twitter',
|
||||
]
|
||||
|
||||
export async function loadExamples () {
|
||||
const liveBadgeExamples = await Promise.all(liveBadgeList.map(async name => {
|
||||
const { meta: { title, examples } } = await import(rel('../endpoints', name))
|
||||
export async function loadBadgeMeta () {
|
||||
const liveBadgeExamples = await Promise.all(liveBadgeList.map(async id => {
|
||||
const { meta, handlers } = await import(rel('../endpoints', id))
|
||||
const { title, examples, help } = meta
|
||||
|
||||
return {
|
||||
title: title || name,
|
||||
examples
|
||||
id,
|
||||
title,
|
||||
examples,
|
||||
routes: Object.keys(handlers),
|
||||
help
|
||||
}
|
||||
}))
|
||||
|
|
@ -2,16 +2,18 @@ import { BadgenServeHandlers } from './badgen-serve'
|
|||
|
||||
type BadgenExample = [string, string]
|
||||
type BadgenHelpParams = {
|
||||
id: string,
|
||||
title: string,
|
||||
examples: BadgenExample[]
|
||||
routes: string[],
|
||||
help?: any
|
||||
examples?: BadgenExample[]
|
||||
handlers: BadgenServeHandlers
|
||||
}
|
||||
|
||||
export default function serveHelp (req, res, id, params: BadgenHelpParams) {
|
||||
const { help, examples = [], handlers } = params
|
||||
const { help, examples, routes } = params
|
||||
const Docs = help ? help : `# ${id}`
|
||||
const Schemes = `## Schemes\n\n${Object.keys(handlers).join('\n')}`
|
||||
const Examples = `## Examples\n\n${examples.map(ex => ex[0]).join('\n')}`
|
||||
const Schemes = `## Schemes\n\n${routes.join('\n')}`
|
||||
const Examples = `## Examples\n\n${Object.entries(examples).map(ex => ex[0]).join('\n')}`
|
||||
|
||||
const md = [Docs, Schemes, Examples].join('\n\n')
|
||||
res.end(md)
|
||||
|
|
|
@ -3,7 +3,7 @@ import BadgeExamples from '../components/badge-examples.js'
|
|||
import Header from '../components/home-header.js'
|
||||
import Intro from '../components/home-intro.js'
|
||||
import Footer from '../components/footer.js'
|
||||
import examples from '../static/.gen/examples.json'
|
||||
import examples from '../static/.gen/badges.json'
|
||||
|
||||
const Index = ({ badgeExamples }) => {
|
||||
const [ tab, setTab ] = useState('live')
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import path from 'path'
|
||||
import fse from 'fs-extra'
|
||||
import { loadExamples } from '../libs/examples'
|
||||
import { loadBadgeMeta } from '../libs/badge-list'
|
||||
|
||||
const rel = (...args) => path.resolve(__dirname, ...args)
|
||||
|
||||
async function main () {
|
||||
const examples = await loadExamples()
|
||||
console.log(examples)
|
||||
const badgeMeta = await loadBadgeMeta()
|
||||
console.log(badgeMeta)
|
||||
|
||||
await fse.outputJson(rel('../static/.gen/examples.json'), examples)
|
||||
await fse.outputJson(rel('../static/.gen/badges.json'), badgeMeta)
|
||||
}
|
||||
|
||||
main()
|
||||
|
|
Ładowanie…
Reference in New Issue