Skip to content

Commit

Permalink
feat: improves messages for errors related to the Astro `trailingSlas…
Browse files Browse the repository at this point in the history
…h` option to indicate if a link is missing a trailing slash or if a link has a trailing slash when it should not
  • Loading branch information
HiDeoo committed Nov 27, 2024
1 parent 0e4f143 commit a87760a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
18 changes: 10 additions & 8 deletions packages/starlight-links-validator/libs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const ValidationErrorType = {
InvalidLink: 'invalid link',
LocalLink: 'local link',
RelativeLink: 'relative link',
TrailingSlash: 'trailing slash',
TrailingSlashMissing: 'missing trailing slash',
TrailingSlashForbidden: 'forbidden trailing slash',
} as const

export function validateLinks(
Expand Down Expand Up @@ -168,13 +169,14 @@ function validateLink(context: ValidationContext) {
return
}

if (
path.length > 0 &&
((astroConfig.trailingSlash === 'always' && !path.endsWith('/')) ||
(astroConfig.trailingSlash === 'never' && path.endsWith('/')))
) {
addError(errors, filePath, link, ValidationErrorType.TrailingSlash)
return
if (path.length > 0) {
if (astroConfig.trailingSlash === 'always' && !path.endsWith('/')) {
addError(errors, filePath, link, ValidationErrorType.TrailingSlashMissing)
return
} else if (astroConfig.trailingSlash === 'never' && path.endsWith('/')) {
addError(errors, filePath, link, ValidationErrorType.TrailingSlashForbidden)
return
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/starlight-links-validator/tests/trailing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ test('validates links when the `trailingSlash` Astro option is set to `never`',
expectValidationErrorCount(output, 6, 1)

expectValidationErrors(output, 'test/', [
['/guides/example/', ValidationErrorType.TrailingSlash],
['/guides/example/#description', ValidationErrorType.TrailingSlash],
['/guides/example/', ValidationErrorType.TrailingSlashForbidden],
['/guides/example/#description', ValidationErrorType.TrailingSlashForbidden],
['/unknown', ValidationErrorType.InvalidLink],
['/unknown/', ValidationErrorType.InvalidLink],
['/guides/example#unknown', ValidationErrorType.InvalidHash],
Expand All @@ -29,8 +29,8 @@ test('validates links when the `trailingSlash` Astro option is set to `always`',
expectValidationErrorCount(output, 6, 1)

expectValidationErrors(output, 'test/', [
['/guides/example', ValidationErrorType.TrailingSlash],
['/guides/example#description', ValidationErrorType.TrailingSlash],
['/guides/example', ValidationErrorType.TrailingSlashMissing],
['/guides/example#description', ValidationErrorType.TrailingSlashMissing],
['/unknown', ValidationErrorType.InvalidLink],
['/unknown/', ValidationErrorType.InvalidLink],
['/guides/example#unknown', ValidationErrorType.InvalidHash],
Expand Down

0 comments on commit a87760a

Please sign in to comment.