diff --git a/packages/starlight-links-validator/libs/remark.ts b/packages/starlight-links-validator/libs/remark.ts index 60648f9..0b2f3e4 100644 --- a/packages/starlight-links-validator/libs/remark.ts +++ b/packages/starlight-links-validator/libs/remark.ts @@ -6,6 +6,7 @@ import GitHubSlugger, { slug } from 'github-slugger' import type { Nodes } from 'hast' import { fromHtml } from 'hast-util-from-html' import { hasProperty } from 'hast-util-has-property' +import isAbsoluteUrl from 'is-absolute-url' import type { Root } from 'mdast' import type { MdxJsxAttribute, MdxJsxExpressionAttribute } from 'mdast-util-mdx-jsx' import { toString } from 'mdast-util-to-string' @@ -133,7 +134,7 @@ export function getValidationData() { } function isInternalLink(link: string) { - return nodePath.isAbsolute(link) || link.startsWith('#') || link.startsWith('.') + return !isAbsoluteUrl(link) } function normalizeFilePath(base: string, filePath?: string) { diff --git a/packages/starlight-links-validator/libs/validation.ts b/packages/starlight-links-validator/libs/validation.ts index 073364f..a88546d 100644 --- a/packages/starlight-links-validator/libs/validation.ts +++ b/packages/starlight-links-validator/libs/validation.ts @@ -113,7 +113,7 @@ function validateLink(context: ValidationContext) { throw new Error('Failed to validate a link with no path.') } - if (path.startsWith('.')) { + if (path.startsWith('.') || !link.startsWith('/')) { if (options.errorOnRelativeLinks) { addError(errors, filePath, link, ValidationErrorType.RelativeLink) } diff --git a/packages/starlight-links-validator/package.json b/packages/starlight-links-validator/package.json index d5b3abc..d94a278 100644 --- a/packages/starlight-links-validator/package.json +++ b/packages/starlight-links-validator/package.json @@ -17,6 +17,7 @@ "github-slugger": "2.0.0", "hast-util-from-html": "2.0.1", "hast-util-has-property": "3.0.0", + "is-absolute-url": "4.0.1", "kleur": "4.1.5", "mdast-util-to-string": "4.0.0", "unist-util-visit": "5.0.0" diff --git a/packages/starlight-links-validator/tests/basics.test.ts b/packages/starlight-links-validator/tests/basics.test.ts index 7b83c1d..4485937 100644 --- a/packages/starlight-links-validator/tests/basics.test.ts +++ b/packages/starlight-links-validator/tests/basics.test.ts @@ -18,7 +18,7 @@ test('should not build with invalid links', async () => { try { await loadFixture('basics-invalid-links') } catch (error) { - expectValidationErrorCount(error, 25, 4) + expectValidationErrorCount(error, 26, 4) expectValidationErrors(error, 'test/', [ ['/', ValidationErrorType.InvalidLink], @@ -55,6 +55,7 @@ test('should not build with invalid links', async () => { ['./test', ValidationErrorType.RelativeLink], ['./guides/example', ValidationErrorType.RelativeLink], ['../test', ValidationErrorType.RelativeLink], + ['test', ValidationErrorType.RelativeLink], ]) } }) diff --git a/packages/starlight-links-validator/tests/fixtures/basics-invalid-links/src/content/docs/relative.md b/packages/starlight-links-validator/tests/fixtures/basics-invalid-links/src/content/docs/relative.md index af5f41b..f03f0c3 100644 --- a/packages/starlight-links-validator/tests/fixtures/basics-invalid-links/src/content/docs/relative.md +++ b/packages/starlight-links-validator/tests/fixtures/basics-invalid-links/src/content/docs/relative.md @@ -7,3 +7,4 @@ title: Relative - [Link to another page in the same directory](./test) - [Link to another page in another directory](./guides/example) - [Link to another page in a parent directory](../test) +- [Another link to another page in the same directory](test) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 60d6217..fbe4453 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -71,6 +71,9 @@ importers: hast-util-has-property: specifier: 3.0.0 version: 3.0.0 + is-absolute-url: + specifier: 4.0.1 + version: 4.0.1 kleur: specifier: 4.1.5 version: 4.1.5 @@ -3344,6 +3347,11 @@ packages: side-channel: 1.0.4 dev: true + /is-absolute-url@4.0.1: + resolution: {integrity: sha512-/51/TKE88Lmm7Gc4/8btclNXWS+g50wXhYJq8HWIBAGUBnoAdRu1aXeh364t/O7wXDAcTJDP8PNuNKWUDWie+A==} + engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + dev: false + /is-alphabetical@2.0.1: resolution: {integrity: sha512-FWyyY60MeTNyeSRpkM2Iry0G9hpr7/9kD40mD/cGQEuilcZYS4okz8SN2Q6rLCJ8gbCt6fN+rC+6tMGS99LaxQ==}