Skip to content

Commit

Permalink
Merge pull request #30 from HiDeoo/hd-improve-relative-link-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored Feb 8, 2024
2 parents 924f660 + 43e89f9 commit 12f12e4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/starlight-links-validator/libs/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/starlight-links-validator/libs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions packages/starlight-links-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion packages/starlight-links-validator/tests/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
])
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 12f12e4

Please sign in to comment.