prettyHTML

This commit is contained in:
Chris Wanstrath 2025-11-30 12:20:10 -08:00
parent 31a4fc2a32
commit 864adfa026

View File

@ -16,6 +16,7 @@ export * from './utils'
export type HypeProps = { export type HypeProps = {
pico?: boolean pico?: boolean
reset?: boolean reset?: boolean
prettyHTML?: boolean
} }
export class Hype< export class Hype<
@ -23,16 +24,13 @@ export class Hype<
S extends Schema = {}, S extends Schema = {},
BasePath extends string = '/' BasePath extends string = '/'
> extends Hono<E, S, BasePath> { > extends Hono<E, S, BasePath> {
props?: HypeProps props: HypeProps
routesRegistered = false routesRegistered = false
constructor(props?: HypeProps & ConstructorParameters<typeof Hono<E, S, BasePath>>[0]) { constructor(props?: HypeProps & ConstructorParameters<typeof Hono<E, S, BasePath>>[0]) {
super(props) super(props)
if (props) { this.props = props ?? {}
const { pico, reset } = props
this.props = { pico, reset }
}
} }
registerRoutes() { registerRoutes() {
@ -74,16 +72,16 @@ export class Hype<
}) })
// prettify HTML output // prettify HTML output
if (process.env.NODE_ENV !== 'production') { if (this.props.prettyHTML ?? process.env.NODE_ENV !== 'production') {
this.use('*', async (c, next) => { this.use('*', async (c, next) => {
await next(); await next()
if (c.res.headers.get('content-type')?.includes('text/html')) { if (c.res.headers.get('content-type')?.includes('text/html')) {
const html = await c.res.text(); const html = await c.res.text()
const formatted = await prettier.format(html, { parser: 'html' }); const formatted = await prettier.format(html, { parser: 'html' })
c.res = new Response(formatted, c.res); c.res = new Response(formatted, c.res)
} }
}); })
} }
// css reset // css reset
@ -114,6 +112,7 @@ export class Hype<
const pageName = (c.req.param('page') ?? 'index').replace('.', '') const pageName = (c.req.param('page') ?? 'index').replace('.', '')
if (pageName.startsWith('_')) return render404(c) if (pageName.startsWith('_')) return render404(c)
console.log(process.env.PWD)
const path = join(process.env.PWD ?? '.', `./src/pages/${pageName}.tsx`) const path = join(process.env.PWD ?? '.', `./src/pages/${pageName}.tsx`)
if (!(await Bun.file(path).exists())) if (!(await Bun.file(path).exists()))