Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves relative link detection #30

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading