Skip to content

Commit

Permalink
fix: treat links with non-http protocol as external (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlexLichter authored Dec 16, 2024
1 parent 226b006 commit 994045d
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion playground/pages/fix-test.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
absolute 404 link
</LinkDebug>
<LinkDebug to="javascript:history.back()">
javacript link
javascript link
</LinkDebug>
<LinkDebug to="/error">
to an error page
Expand Down
2 changes: 1 addition & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
absolute 404 link
</LinkDebug>
<LinkDebug to="javascript:history.back()">
javacript link
javascript link
</LinkDebug>
<LinkDebug to="/error">
to an error page
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/shared/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LinkInspectionResult, Rule, RuleTestContext } from '../types'
import { parseURL } from 'ufo'
import { hasProtocol, parseURL } from 'ufo'
import RuleAbsoluteSiteUrls from './inspections/absolute-site-urls'
import RuleDescriptiveLinkText from './inspections/link-text'
import RuleMissingHash from './inspections/missing-hash'
Expand Down Expand Up @@ -45,7 +45,8 @@ export function inspect(ctx: Pick<Required<RuleTestContext>, 'link'> & Omit<Part
let processing = true
for (const rule of validInspections) {
const isFakeAbsolute = link.startsWith('//') && !link.includes('.')
const isExternalLink = url.host && url.host !== siteConfigHost && !isFakeAbsolute
const hasNonHttpProtocol = hasProtocol(link) && !link.startsWith('http')
const isExternalLink = hasNonHttpProtocol || (url.host && url.host !== siteConfigHost && !isFakeAbsolute)
if (!rule.externalLinks && isExternalLink) {
continue
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/shared/inspections/no-javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineRule } from './util'
export default function RuleNoJavascript() {
return defineRule({
id: 'no-javascript',
externalLinks: true,
test({ link, report }) {
if (link.startsWith('javascript:')) {
report({
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('generate', () => {
"fix": "javascript:history.back()",
"link": "javascript:history.back()",
"passes": false,
"textContent": "javacript link"
"textContent": "javascript link"
},
{
"error": [],
Expand Down Expand Up @@ -493,7 +493,7 @@ describe('generate', () => {
"fix": "javascript:history.back()",
"link": "javascript:history.back()",
"passes": false,
"textContent": "javacript link"
"textContent": "javascript link"
},
{
"error": [],
Expand Down

0 comments on commit 994045d

Please sign in to comment.