Skip to content

Commit 0d41a41

Browse files
Fix cross-package links in docs (#946)
Trying to get the "Expression" link at https://jsr.io/@babel-test-6ae45912/[email protected]/doc/~/parseExpression to work: - it's missing the `/doc` in the URL - if the type is imported from a non-default entrypoint, it should link to it - if it's imported from a specific version, it should link to that version (this makes it work with pre-releases)
1 parent daee95f commit 0d41a41

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Diff for: api/src/docs.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,23 @@ impl HrefResolver for DocResolver {
11261126
deno_semver::jsr::JsrPackageReqReference::from_str(src).ok()?;
11271127
let req = jsr_package_req.req();
11281128

1129-
Some(format!("/{}/~/{symbol}", req.name))
1129+
let mut version_path = Cow::Borrowed("");
1130+
if let Some(range) = req.version_req.range() {
1131+
if let Ok(version) = Version::new(&range.to_string()) {
1132+
// If using a specific version, link to it (e.g. prerelease)
1133+
version_path = Cow::Owned(format!("@{}", version));
1134+
}
1135+
}
1136+
1137+
let mut internal_path = Cow::Borrowed("");
1138+
if let Some(path) = jsr_package_req.sub_path() {
1139+
internal_path = Cow::Owned(format!("/{path}"));
1140+
}
1141+
1142+
Some(format!(
1143+
"/{}{version_path}/doc{internal_path}/~/{symbol}",
1144+
req.name
1145+
))
11301146
}
11311147
_ => None,
11321148
}
@@ -1417,6 +1433,28 @@ mod tests {
14171433
"/@foo/[email protected]/doc/mod/~/bar"
14181434
);
14191435
}
1436+
1437+
{
1438+
assert_eq!(
1439+
resolver
1440+
.resolve_import_href(
1441+
&["Expression".to_string()],
1442+
"jsr:@babel/[email protected]"
1443+
)
1444+
.as_deref(),
1445+
Some("/@babel/[email protected]/doc/~/Expression")
1446+
);
1447+
1448+
assert_eq!(
1449+
resolver
1450+
.resolve_import_href(
1451+
&["version".to_string()],
1452+
"jsr:@babel/core/package.json"
1453+
)
1454+
.as_deref(),
1455+
Some("/@babel/core/doc/package.json/~/version")
1456+
);
1457+
}
14201458
}
14211459

14221460
#[test]

0 commit comments

Comments
 (0)