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