Skip to content

Commit 56b17a8

Browse files
authored
Add searchNodes to docs_config.js (#51)
1 parent 4181403 commit 56b17a8

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

Diff for: lib/hexdocs/bucket.ex

+32-5
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,46 @@ defmodule Hexdocs.Bucket do
7777
Enum.sort([version | all_versions], &(Version.compare(&1, &2) == :gt))
7878
end
7979

80-
list =
80+
latest_version = Hexdocs.Utils.latest_version(versions)
81+
82+
versions =
8183
for version <- versions do
82-
%{
84+
map = %{
8385
version: "v#{version}",
84-
url: Hexdocs.Utils.hexdocs_url(repository, "/#{package}/#{version}"),
85-
latest: Hexdocs.Utils.latest_version?(package, version, versions)
86+
url: Hexdocs.Utils.hexdocs_url(repository, "/#{package}/#{version}")
8687
}
88+
89+
if latest_version == version do
90+
Map.put(map, :latest, true)
91+
else
92+
map
93+
end
94+
end
95+
96+
search =
97+
if repository == "hexpm" do
98+
[%{name: package, version: Version.to_string(version)}]
8799
end
88100

89101
path = "docs_config.js"
90102
unversioned_path = repository_path(repository, Path.join([package, path]))
91103
cdn_key = docs_config_cdn_key(repository, package)
92-
data = ["var versionNodes = ", Jason.encode_to_iodata!(list), ";"]
104+
105+
data = [
106+
"var versionNodes = ",
107+
Jason.encode_to_iodata!(versions),
108+
";\n",
109+
if search do
110+
[
111+
"var searchNodes = ",
112+
Jason.encode_to_iodata!(search),
113+
";"
114+
]
115+
else
116+
[]
117+
end
118+
]
119+
93120
{unversioned_path, cdn_key, data, public?(repository)}
94121
end
95122

Diff for: test/hexdocs/queue_test.exs

+14-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ defmodule Hexdocs.QueueTest do
3939

4040
assert Store.get(@bucket, "queuetest/#{test}/index.html") == "contents"
4141
assert Store.get(@bucket, "queuetest/#{test}/1.0.0/index.html") == "contents"
42+
43+
# no searchNodes for private packages.
44+
docs_config = Store.get(@bucket, "queuetest/#{test}/docs_config.js")
45+
["var versionNodes = " <> _] = String.split(docs_config, [";", "\n"], trim: true)
4246
end
4347

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

252-
assert "var versionNodes = " <> json = Store.get(@public_bucket, "#{test}/docs_config.js")
253-
json = String.trim_trailing(json, ";")
256+
docs_config = Store.get(@public_bucket, "#{test}/docs_config.js")
257+
258+
["var versionNodes = " <> versions_json, "var searchNodes = " <> search_json] =
259+
String.split(docs_config, [";", "\n"], trim: true)
254260

255-
assert Jason.decode!(json) == [
261+
assert Jason.decode!(versions_json) == [
256262
%{
257263
"url" => "http://localhost/#{URI.encode(Atom.to_string(test))}/3.0.0",
258264
"version" => "v3.0.0",
259265
"latest" => true
260266
},
261267
%{
262268
"url" => "http://localhost/#{URI.encode(Atom.to_string(test))}/1.0.0",
263-
"version" => "v1.0.0",
264-
"latest" => false
269+
"version" => "v1.0.0"
265270
}
266271
]
272+
273+
assert Jason.decode!(search_json) == [
274+
%{"name" => "#{test}", "version" => "3.0.0"}
275+
]
267276
end
268277

269278
test "special packages" do

0 commit comments

Comments
 (0)