Skip to content

Commit 75a74ea

Browse files
committed
feat: support passing an ignore array of paths not to check
resolves #486
1 parent e04efc5 commit 75a74ea

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export interface ModuleOptions {
2929
logLevel?: LogLevel
3030
failOnError?: boolean
3131
options?: ConfigData
32+
/**
33+
* A list of routes to ignore (that is, not check validity for)
34+
* @default [/\.(xml|rss|json)$/]
35+
*/
36+
ignore?: Array<string | RegExp>
3237
/**
3338
* allow to hook into `html-validator`
3439
* enabling this option block the response until the HTML check and the hook has finished
@@ -43,4 +48,5 @@ export const DEFAULTS: Required<Omit<ModuleOptions, 'logLevel'>> & { logLevel?:
4348
failOnError: false,
4449
options: defaultHtmlValidateConfig,
4550
hookable: false,
51+
ignore: [/\.(xml|rss|json)$/],
4652
}

src/runtime/nitro.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { NitroAppPlugin, RenderResponse } from 'nitropack'
22
import { getRequestPath } from 'h3'
3-
import { useChecker, getValidator } from './validator'
3+
import { useChecker, getValidator, isIgnored } from './validator'
44
// @ts-expect-error virtual module
55
import config from '#html-validator-config'
66

@@ -9,7 +9,7 @@ export default <NitroAppPlugin> function (nitro) {
99
const { checkHTML } = useChecker(validator, config.usePrettier, config.logLevel)
1010

1111
nitro.hooks.hook('render:response', async (response: Partial<RenderResponse>, { event }) => {
12-
if (typeof response.body === 'string' && (response.headers?.['Content-Type'] || response.headers?.['content-type'])?.includes('html')) {
12+
if (typeof response.body === 'string' && (response.headers?.['Content-Type'] || response.headers?.['content-type'])?.includes('html') && !isIgnored(event.path, config.ignore)) {
1313
// Block the response only if it's not hookable
1414
const promise = checkHTML(getRequestPath(event), response.body)
1515
if (config.hookable) {

src/runtime/validator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ export const useChecker = (
6666

6767
return { checkHTML, invalidPages }
6868
}
69+
70+
export function isIgnored(path: string, ignore: Array<string | RegExp> = []) {
71+
return ignore.some(ignore => typeof ignore === 'string' ? path === ignore : ignore.test(path))
72+
}

0 commit comments

Comments
 (0)