Skip to content

Commit 907b8cc

Browse files
committed
fix: md router
1 parent b8f4abb commit 907b8cc

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/waku/internal/middleware/md-router.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import * as path from 'node:path'
21
import type { MiddlewareHandler } from 'hono'
3-
import * as Config from '../../../internal/config.js'
4-
import * as Llms from '../../../internal/llms.js'
5-
import * as Mdx from '../../../internal/mdx.js'
62

73
export const aiUserAgents = [
84
'GPTBot',
@@ -38,7 +34,14 @@ export const aiUserAgents = [
3834

3935
export const terminalUserAgents = ['curl/', 'Wget/', 'HTTPie/', 'httpie-go/', 'xh/']
4036

37+
const isDev = process.env['NODE_ENV'] !== 'production'
38+
4139
async function resolveContent() {
40+
const path = await import('node:path')
41+
const Config = await import('../../../internal/config.js')
42+
const Llms = await import('../../../internal/llms.js')
43+
const Mdx = await import('../../../internal/mdx.js')
44+
4245
const config = await Config.resolve({ server: true })
4346
const { rehypePlugins, remarkPlugins } = Mdx.getCompileOptions('txt', config)
4447
const pagesDir = path.resolve(config.rootDir, config.srcDir, config.pagesDir)
@@ -52,6 +55,13 @@ async function resolveContent() {
5255
})
5356
}
5457

58+
async function fetchMarkdown(url: URL, assetPath: string) {
59+
const assetUrl = new URL(assetPath, url.origin)
60+
const response = await globalThis.fetch(assetUrl)
61+
if (!response.ok) return null
62+
return response.text()
63+
}
64+
5565
export function middleware(): MiddlewareHandler {
5666
return async (context, next) => {
5767
const url = new URL(context.req.url)
@@ -63,8 +73,16 @@ export function middleware(): MiddlewareHandler {
6373
const acceptsMarkdown = acceptHeader.includes('text/markdown')
6474

6575
if (url.pathname === '/' && (acceptsMarkdown || isTerminal)) {
66-
const content = await resolveContent()
67-
context.res = new Response(content.short, {
76+
let text: string | null
77+
if (isDev) {
78+
const content = await resolveContent()
79+
text = content.short
80+
} else {
81+
text = await fetchMarkdown(url, '/llms.txt')
82+
}
83+
if (!text) return next()
84+
85+
context.res = new Response(text, {
6886
headers: {
6987
'Content-Type': 'text/markdown; charset=utf-8',
7088
},
@@ -76,13 +94,20 @@ export function middleware(): MiddlewareHandler {
7694
if (!isMarkdownRequest && !isAiAgent && !isTerminal) return next()
7795

7896
const pagePath = url.pathname.replace(/\.md$/, '').replace(/\/index$/, '')
79-
const content = await resolveContent()
80-
const result = content.results.find(
81-
(r) => r.path.replace(/\/$/, '') === pagePath.replace(/\/$/, ''),
82-
)
83-
if (!result) return next()
8497

85-
context.res = new Response(result.content, {
98+
let text: string | null
99+
if (isDev) {
100+
const content = await resolveContent()
101+
const result = content.results.find(
102+
(r) => r.path.replace(/\/$/, '') === pagePath.replace(/\/$/, ''),
103+
)
104+
text = result?.content ?? null
105+
} else {
106+
text = await fetchMarkdown(url, `/assets/md${url.pathname}`)
107+
}
108+
if (!text) return next()
109+
110+
context.res = new Response(text, {
86111
headers: {
87112
'Content-Type': `${url.pathname.endsWith('.txt') ? 'text/plain' : 'text/markdown'}; charset=utf-8`,
88113
},

0 commit comments

Comments
 (0)