Skip to content

Commit 3d2a614

Browse files
authored
feat: add html-validator:check nitro hook (#372)
1 parent 141d40c commit 3d2a614

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ export interface ModuleOptions {
2929
logLevel?: LogLevel
3030
failOnError?: boolean
3131
options?: ConfigData
32+
/**
33+
* allow to hook into `html-validator`
34+
* enabling this option block the response until the HTML check and the hook has finished
35+
*
36+
* @default false
37+
*/
38+
hookable: boolean
3239
}
3340

3441
export const DEFAULTS: Required<Omit<ModuleOptions, 'logLevel'>> & { logLevel?: LogLevel } = {
3542
usePrettier: false,
3643
failOnError: false,
37-
options: defaultHtmlValidateConfig
44+
options: defaultHtmlValidateConfig,
45+
hookable: false
3846
}

src/runtime/nitro.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ export default <NitroAppPlugin> function (nitro) {
77
const validator = getValidator(config.options)
88
const { checkHTML } = useChecker(validator, config.usePrettier, config.logLevel)
99

10-
nitro.hooks.hook('render:response', (response: RenderResponse, { event }) => {
10+
nitro.hooks.hook('render:response', async (response: RenderResponse, { event }) => {
1111
if (typeof response.body === 'string' && (response.headers['Content-Type'] || response.headers['content-type'])?.includes('html')) {
12-
// We deliberately do not await so as not to block the response
13-
checkHTML(event.req.url, response.body)
12+
// Block the response only if it's not hookable
13+
const promise = checkHTML(event.req.url, response.body)
14+
if (config.hookable) {
15+
await nitro.hooks.callHook('html-validator:check', await promise, response, { event })
16+
}
1417
}
1518
})
1619
}

src/runtime/validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const useChecker = (
3434
console.log(`No HTML validation errors found for ${chalk.bold(url)}`)
3535
}
3636

37-
return
37+
return { valid, results }
3838
}
3939

4040
if (!valid) { invalidPages.push(url) }
@@ -54,6 +54,8 @@ export const useChecker = (
5454
} else {
5555
console.error(message)
5656
}
57+
58+
return { valid, results }
5759
}
5860

5961
return { checkHTML, invalidPages }

0 commit comments

Comments
 (0)