Skip to content

Commit

Permalink
feat: support passing an ignore array of paths not to check
Browse files Browse the repository at this point in the history
resolves #486
  • Loading branch information
danielroe committed Apr 29, 2024
1 parent e04efc5 commit 75a74ea
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface ModuleOptions {
logLevel?: LogLevel
failOnError?: boolean
options?: ConfigData
/**
* A list of routes to ignore (that is, not check validity for)
* @default [/\.(xml|rss|json)$/]
*/
ignore?: Array<string | RegExp>
/**
* allow to hook into `html-validator`
* enabling this option block the response until the HTML check and the hook has finished
Expand All @@ -43,4 +48,5 @@ export const DEFAULTS: Required<Omit<ModuleOptions, 'logLevel'>> & { logLevel?:
failOnError: false,
options: defaultHtmlValidateConfig,
hookable: false,
ignore: [/\.(xml|rss|json)$/],
}
4 changes: 2 additions & 2 deletions src/runtime/nitro.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NitroAppPlugin, RenderResponse } from 'nitropack'
import { getRequestPath } from 'h3'
import { useChecker, getValidator } from './validator'
import { useChecker, getValidator, isIgnored } from './validator'
// @ts-expect-error virtual module
import config from '#html-validator-config'

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

nitro.hooks.hook('render:response', async (response: Partial<RenderResponse>, { event }) => {
if (typeof response.body === 'string' && (response.headers?.['Content-Type'] || response.headers?.['content-type'])?.includes('html')) {
if (typeof response.body === 'string' && (response.headers?.['Content-Type'] || response.headers?.['content-type'])?.includes('html') && !isIgnored(event.path, config.ignore)) {
// Block the response only if it's not hookable
const promise = checkHTML(getRequestPath(event), response.body)
if (config.hookable) {
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ export const useChecker = (

return { checkHTML, invalidPages }
}

export function isIgnored(path: string, ignore: Array<string | RegExp> = []) {
return ignore.some(ignore => typeof ignore === 'string' ? path === ignore : ignore.test(path))
}

0 comments on commit 75a74ea

Please sign in to comment.