diff --git a/src/hexdocs/config.gleam b/src/hexdocs/config.gleam index 1a52e65..177048c 100644 --- a/src/hexdocs/config.gleam +++ b/src/hexdocs/config.gleam @@ -17,6 +17,15 @@ pub fn hexdocs_package_url(package: String) -> String { "https://" <> hexdocs_package_host(package) } +/// Documentation URL for a package at a specific version. When the version is +/// empty the version segment is omitted, linking to the latest documentation. +pub fn hexdocs_package_version_url(package: String, version: String) -> String { + case version { + "" -> hexdocs_package_url(package) + _ -> hexdocs_package_url(package) <> "/" <> version + } +} + pub fn hexpm_url() -> String { "https://hex.pm" } diff --git a/src/hexdocs/view/search.gleam b/src/hexdocs/view/search.gleam index b1c9c4c..b8f869a 100644 --- a/src/hexdocs/view/search.gleam +++ b/src/hexdocs/view/search.gleam @@ -245,10 +245,10 @@ pub fn search(model: Model) { case filter.status { version.Found(ver) -> attribute.href( - config.hexdocs_package_url(filter.name) - <> "/" - <> ver - <> "/", + config.hexdocs_package_version_url( + filter.name, + ver, + ), ) _ -> class("") }, diff --git a/test/hexdocs_test.gleam b/test/hexdocs_test.gleam index 3831e7a..be69118 100644 --- a/test/hexdocs_test.gleam +++ b/test/hexdocs_test.gleam @@ -1,12 +1,22 @@ import gleeunit import gleeunit/should +import hexdocs/config pub fn main() { gleeunit.main() } -// gleeunit test functions end in `_test` -pub fn hello_world_test() { - 1 - |> should.equal(1) +pub fn hexdocs_package_version_url_with_version_test() { + config.hexdocs_package_version_url("phoenix", "1.7.0") + |> should.equal("https://phoenix.hexdocs.pm/1.7.0") +} + +pub fn hexdocs_package_version_url_without_version_test() { + config.hexdocs_package_version_url("mix", "") + |> should.equal("https://mix.hexdocs.pm") +} + +pub fn hexdocs_package_version_url_replaces_underscores_test() { + config.hexdocs_package_version_url("ex_unit", "") + |> should.equal("https://ex-unit.hexdocs.pm") }