UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

45 lines (44 loc) 1.92 kB
import process from 'node:process'; import path from 'node:path'; import fs from 'node:fs'; import { generateIndexHtml } from '../../../apps/ui/vite.config.js'; const __dirname = globalThis[Symbol.for("import-meta-ponyfill-esmodule")](import.meta).dirname; const MAIN_DIR = __dirname + '/../../..'; export async function handleStaticHtml(app, reqPath, url, template) { let renderedPath = path.resolve(MAIN_DIR, 'website', '.vitepress', 'dist', (reqPath.substring(1) || 'index.html')); if (!renderedPath.startsWith(path.resolve(MAIN_DIR))) { return null; } if (reqPath.startsWith('/drive') || reqPath.startsWith('/gdocs') || reqPath.startsWith('/auth') || reqPath === '/' || reqPath.startsWith('/share-drive') || reqPath.endsWith('.html')) { if (fs.existsSync(renderedPath)) { const template = generateIndexHtml() .replace('</head>', process.env.ZIPKIN_URL ? `<meta name="ZIPKIN_URL" content="${process.env.ZIPKIN_URL}" />\n</head>` : '</head>') .replace(/GIT_SHA/g, process.env.GIT_SHA); return template; } else { const template = generateIndexHtml() .replace('</head>', process.env.ZIPKIN_URL ? `<meta name="ZIPKIN_URL" content="${process.env.ZIPKIN_URL}" />\n</head>` : '</head>') .replace(/GIT_SHA/g, process.env.GIT_SHA); const viteInstance = app.get('viteInstance'); return await viteInstance.transformIndexHtml(url, template); } } return null; } export async function initStaticDistPages(app) { app.use(async (req, res, next) => { const indexHtml = await handleStaticHtml(app, req.path, req.originalUrl); if (indexHtml) { res.status(200).header('Content-type', 'text/html').end(indexHtml); } else { next(); } }); }