|
| 1 | +/* eslint-disable filenames/match-regex */ |
| 2 | +import { readFileSync } from 'fs' |
| 3 | +import { URL } from 'url' |
| 4 | +import { Arcdown } from 'arcdown' |
| 5 | +import arcStaticImg from 'markdown-it-arc-static-img' |
| 6 | +import navDataLoader, { |
| 7 | + unslug, |
| 8 | + other as otherLinks, |
| 9 | +} from '../../docs/nav-data.mjs' |
| 10 | +import HljsLineWrapper from '../../docs/hljs-line-wrapper.mjs' |
| 11 | + |
| 12 | +const arcdown = new Arcdown({ |
| 13 | + pluginOverrides: { |
| 14 | + markdownItToc: { |
| 15 | + containerClass: 'toc mb2 ml-2', |
| 16 | + listType: 'ul', |
| 17 | + }, |
| 18 | + }, |
| 19 | + plugins: [arcStaticImg], |
| 20 | + hljs: { |
| 21 | + sublanguages: { javascript: ['xml', 'css'] }, |
| 22 | + plugins: [new HljsLineWrapper({ className: 'code-line' })], |
| 23 | + }, |
| 24 | +}) |
| 25 | + |
| 26 | +/** @type {import('@enhance/types').EnhanceApiFn} */ |
| 27 | +export async function get(request) { |
| 28 | + const { path: activePath } = request |
| 29 | + let docPath = activePath.replace(/^\/?docs\//, '') || 'index' |
| 30 | + if (docPath.endsWith('/')) { |
| 31 | + docPath += 'index' // trailing slash == index.md file |
| 32 | + } |
| 33 | + |
| 34 | + const gacode = |
| 35 | + process.env.ARC_ENV === 'production' ? 'G-FQHNPN78V3' : 'G-0ES194BJQ6' |
| 36 | + |
| 37 | + const docURL = new URL(`../../docs/md/${docPath}.md`, import.meta.url) |
| 38 | + |
| 39 | + const sidebarData = navDataLoader('docs', activePath) |
| 40 | + |
| 41 | + let docMarkdown |
| 42 | + try { |
| 43 | + docMarkdown = readFileSync(docURL.pathname, 'utf-8') |
| 44 | + } catch (_err) { |
| 45 | + let searchTerm = null |
| 46 | + if (!docPath.endsWith('/index')) { |
| 47 | + const docPathParts = docPath.split('/') |
| 48 | + searchTerm = docPathParts.pop() |
| 49 | + searchTerm = unslug(searchTerm) |
| 50 | + } |
| 51 | + const initialState = { |
| 52 | + doc: { |
| 53 | + title: '404', |
| 54 | + path: docPath, |
| 55 | + term: searchTerm || '', |
| 56 | + }, |
| 57 | + otherLinks, |
| 58 | + sidebarData, |
| 59 | + searchTerm, |
| 60 | + gacode, |
| 61 | + } |
| 62 | + |
| 63 | + return { statusCode: 404, json: initialState } |
| 64 | + } |
| 65 | + const doc = await arcdown.render(docMarkdown) |
| 66 | + |
| 67 | + let gitHubLink = 'https://github.com/enhance-dev/enhance.dev/edit/main/src/' |
| 68 | + gitHubLink += `views/docs/md/${docPath}.md` |
| 69 | + |
| 70 | + const initialState = { |
| 71 | + doc, |
| 72 | + gitHubLink, |
| 73 | + otherLinks, |
| 74 | + sidebarData, |
| 75 | + gacode, |
| 76 | + } |
| 77 | + |
| 78 | + let cacheControl = |
| 79 | + process.env.ARC_ENV === 'production' |
| 80 | + ? 'max-age=3600;' |
| 81 | + : 'no-cache, no-store, must-revalidate, max-age=0, s-maxage=0' |
| 82 | + |
| 83 | + return { |
| 84 | + cacheControl, |
| 85 | + json: initialState, |
| 86 | + } |
| 87 | +} |
0 commit comments