Skip to content

Commit a87760a

Browse files
committed
feat: improves messages for errors related to the Astro trailingSlash option to indicate if a link is missing a trailing slash or if a link has a trailing slash when it should not
1 parent 0e4f143 commit a87760a

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

packages/starlight-links-validator/libs/validation.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const ValidationErrorType = {
1919
InvalidLink: 'invalid link',
2020
LocalLink: 'local link',
2121
RelativeLink: 'relative link',
22-
TrailingSlash: 'trailing slash',
22+
TrailingSlashMissing: 'missing trailing slash',
23+
TrailingSlashForbidden: 'forbidden trailing slash',
2324
} as const
2425

2526
export function validateLinks(
@@ -168,13 +169,14 @@ function validateLink(context: ValidationContext) {
168169
return
169170
}
170171

171-
if (
172-
path.length > 0 &&
173-
((astroConfig.trailingSlash === 'always' && !path.endsWith('/')) ||
174-
(astroConfig.trailingSlash === 'never' && path.endsWith('/')))
175-
) {
176-
addError(errors, filePath, link, ValidationErrorType.TrailingSlash)
177-
return
172+
if (path.length > 0) {
173+
if (astroConfig.trailingSlash === 'always' && !path.endsWith('/')) {
174+
addError(errors, filePath, link, ValidationErrorType.TrailingSlashMissing)
175+
return
176+
} else if (astroConfig.trailingSlash === 'never' && path.endsWith('/')) {
177+
addError(errors, filePath, link, ValidationErrorType.TrailingSlashForbidden)
178+
return
179+
}
178180
}
179181
}
180182

packages/starlight-links-validator/tests/trailing.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ test('validates links when the `trailingSlash` Astro option is set to `never`',
1212
expectValidationErrorCount(output, 6, 1)
1313

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

3131
expectValidationErrors(output, 'test/', [
32-
['/guides/example', ValidationErrorType.TrailingSlash],
33-
['/guides/example#description', ValidationErrorType.TrailingSlash],
32+
['/guides/example', ValidationErrorType.TrailingSlashMissing],
33+
['/guides/example#description', ValidationErrorType.TrailingSlashMissing],
3434
['/unknown', ValidationErrorType.InvalidLink],
3535
['/unknown/', ValidationErrorType.InvalidLink],
3636
['/guides/example#unknown', ValidationErrorType.InvalidHash],

0 commit comments

Comments
 (0)