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

Add searchNodes to docs_config.js #51

Merged
merged 1 commit into from
Jan 9, 2025
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
37 changes: 32 additions & 5 deletions lib/hexdocs/bucket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,46 @@ defmodule Hexdocs.Bucket do
Enum.sort([version | all_versions], &(Version.compare(&1, &2) == :gt))
end

list =
latest_version = Hexdocs.Utils.latest_version(versions)

versions =
for version <- versions do
%{
map = %{
version: "v#{version}",
url: Hexdocs.Utils.hexdocs_url(repository, "/#{package}/#{version}"),
latest: Hexdocs.Utils.latest_version?(package, version, versions)
url: Hexdocs.Utils.hexdocs_url(repository, "/#{package}/#{version}")
}

if latest_version == version do
Map.put(map, :latest, true)
else
map
end
end

search =
if repository == "hexpm" do
[%{name: package, version: Version.to_string(version)}]
end

path = "docs_config.js"
unversioned_path = repository_path(repository, Path.join([package, path]))
cdn_key = docs_config_cdn_key(repository, package)
data = ["var versionNodes = ", Jason.encode_to_iodata!(list), ";"]

data = [
"var versionNodes = ",
Jason.encode_to_iodata!(versions),
";\n",
if search do
[
"var searchNodes = ",
Jason.encode_to_iodata!(search),
";"
]
else
[]
end
]

{unversioned_path, cdn_key, data, public?(repository)}
end

Expand Down
19 changes: 14 additions & 5 deletions test/hexdocs/queue_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ defmodule Hexdocs.QueueTest do

assert Store.get(@bucket, "queuetest/#{test}/index.html") == "contents"
assert Store.get(@bucket, "queuetest/#{test}/1.0.0/index.html") == "contents"

# no searchNodes for private packages.
docs_config = Store.get(@bucket, "queuetest/#{test}/docs_config.js")
["var versionNodes = " <> _] = String.split(docs_config, [";", "\n"], trim: true)
end

test "upload public files", %{test: test} do
Expand Down Expand Up @@ -249,21 +253,26 @@ defmodule Hexdocs.QueueTest do
assert Store.get(@public_bucket, "#{test}/3.0.0/index.html") == "contents"
assert Store.get(@public_bucket, "#{test}/index.html") == "contents"

assert "var versionNodes = " <> json = Store.get(@public_bucket, "#{test}/docs_config.js")
json = String.trim_trailing(json, ";")
docs_config = Store.get(@public_bucket, "#{test}/docs_config.js")

["var versionNodes = " <> versions_json, "var searchNodes = " <> search_json] =
String.split(docs_config, [";", "\n"], trim: true)

assert Jason.decode!(json) == [
assert Jason.decode!(versions_json) == [
%{
"url" => "http://localhost/#{URI.encode(Atom.to_string(test))}/3.0.0",
"version" => "v3.0.0",
"latest" => true
},
%{
"url" => "http://localhost/#{URI.encode(Atom.to_string(test))}/1.0.0",
"version" => "v1.0.0",
"latest" => false
"version" => "v1.0.0"
}
]

assert Jason.decode!(search_json) == [
%{"name" => "#{test}", "version" => "3.0.0"}
]
end

test "special packages" do
Expand Down
Loading