1
0
Fork 0
mirror of synced 2024-06-28 03:31:09 -04:00
ultimate-vim/sources_non_forked/markdown-preview.nvim/app/routes.js
2022-05-19 20:12:11 +08:00

107 lines
3 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const logger = require('./lib/util/logger')('app/routes')
const routes = []
const use = function (route) {
routes.unshift((req, res, next) => () => route(req, res, next))
}
// /page/:number
use((req, res, next) => {
if (/\/page\/\d+/.test(req.asPath)) {
return fs.createReadStream('./out/index.html').pipe(res)
}
next()
})
// /_next/path
use((req, res, next) => {
if (/\/_next/.test(req.asPath)) {
return fs.createReadStream(path.join('./out', req.asPath)).pipe(res)
}
next()
})
// /_static/markdown.css
// /_static/highlight.css
use((req, res, next) => {
try {
if (req.mkcss && req.asPath === '/_static/markdown.css') {
if (fs.existsSync(req.mkcss)) {
return fs.createReadStream(req.mkcss).pipe(res)
}
} else if (req.hicss && req.asPath === '/_static/highlight.css') {
if (fs.existsSync(req.hicss)) {
return fs.createReadStream(req.hicss).pipe(res)
}
}
} catch (e) {
logger.error('load diy css fail: ', req.asPath, req.mkcss, req.hicss)
}
next()
})
// /_static/path
use((req, res, next) => {
if (/\/_static/.test(req.asPath)) {
const fpath = path.join('./', req.asPath)
if (fs.existsSync(fpath)) {
return fs.createReadStream(fpath).pipe(res)
} else {
logger.error('No such file:', req.asPath, req.mkcss, req.hicss)
}
}
next()
})
// images
use(async (req, res, next) => {
logger.info('image route: ', req.asPath)
const reg = /^\/_local_image_/
if (reg.test(req.asPath) && req.asPath !== '') {
const plugin = req.plugin
const buffers = await plugin.nvim.buffers
const buffer = buffers.find(b => b.id === Number(req.bufnr))
if (buffer) {
const fileDir = await plugin.nvim.call('expand', `#${req.bufnr}:p:h`)
logger.info('fileDir', fileDir)
let imgPath = decodeURIComponent(decodeURIComponent(req.asPath.replace(reg, '')))
imgPath = imgPath.replace(/\\ /g, ' ')
if (imgPath[0] !== '/' && imgPath[0] !== '\\') {
imgPath = path.join(fileDir, imgPath)
} else if (!fs.existsSync(imgPath)) {
let tmpDirPath = fileDir
while (tmpDirPath !== '/' && tmpDirPath !== '\\') {
tmpDirPath = path.normalize(path.join(tmpDirPath, '..'))
let tmpImgPath = path.join(tmpDirPath, imgPath)
if (fs.existsSync(tmpImgPath)) {
imgPath = tmpImgPath
break
}
}
}
logger.info('imgPath', imgPath)
if (fs.existsSync(imgPath) && !fs.statSync(imgPath).isDirectory()) {
if (imgPath.endsWith('svg')) {
res.setHeader('content-type', 'image/svg+xml')
}
return fs.createReadStream(imgPath).pipe(res)
}
logger.error('image not exists: ', imgPath)
}
}
next()
})
// 404
use((req, res) => {
res.statusCode = 404
return fs.createReadStream(path.join('./out', '404.html')).pipe(res)
})
module.exports = function (req, res, next) {
return routes.reduce((next, route) => route(req, res, next), next)()
}