Skip to content

Commit 905d39f

Browse files
committed
Fix broken doc links for packages without a version
The selected packages sidebar built each link as `hexdocs_package_url(name) <> "/" <> ver <> "/"`, which produced a double slash (e.g. `mix.hexdocs.pm//`) whenever the resolved version was empty, breaking the link. Introduce `hexdocs_package_version_url/2`, which omits the version segment when the version is empty and otherwise appends it without a trailing slash.
1 parent 2ba6879 commit 905d39f

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/hexdocs/config.gleam

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ pub fn hexdocs_package_url(package: String) -> String {
1717
"https://" <> hexdocs_package_host(package)
1818
}
1919

20+
/// Documentation URL for a package at a specific version. When the version is
21+
/// empty the version segment is omitted, linking to the latest documentation.
22+
pub fn hexdocs_package_version_url(package: String, version: String) -> String {
23+
case version {
24+
"" -> hexdocs_package_url(package)
25+
_ -> hexdocs_package_url(package) <> "/" <> version
26+
}
27+
}
28+
2029
pub fn hexpm_url() -> String {
2130
"https://hex.pm"
2231
}

src/hexdocs/view/search.gleam

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ pub fn search(model: Model) {
245245
case filter.status {
246246
version.Found(ver) ->
247247
attribute.href(
248-
config.hexdocs_package_url(filter.name)
249-
<> "/"
250-
<> ver
251-
<> "/",
248+
config.hexdocs_package_version_url(
249+
filter.name,
250+
ver,
251+
),
252252
)
253253
_ -> class("")
254254
},

test/hexdocs_test.gleam

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import gleeunit
22
import gleeunit/should
3+
import hexdocs/config
34

45
pub fn main() {
56
gleeunit.main()
67
}
78

8-
// gleeunit test functions end in `_test`
9-
pub fn hello_world_test() {
10-
1
11-
|> should.equal(1)
9+
pub fn hexdocs_package_version_url_with_version_test() {
10+
config.hexdocs_package_version_url("phoenix", "1.7.0")
11+
|> should.equal("https://phoenix.hexdocs.pm/1.7.0")
12+
}
13+
14+
pub fn hexdocs_package_version_url_without_version_test() {
15+
config.hexdocs_package_version_url("mix", "")
16+
|> should.equal("https://mix.hexdocs.pm")
17+
}
18+
19+
pub fn hexdocs_package_version_url_replaces_underscores_test() {
20+
config.hexdocs_package_version_url("ex_unit", "")
21+
|> should.equal("https://ex-unit.hexdocs.pm")
1222
}

0 commit comments

Comments
 (0)