Skip to content

Commit

Permalink
PR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Sep 17, 2024
1 parent 09ba90a commit 83551f8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 40 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,19 +464,32 @@ You can improve the developer experience on everything but function names changi
by using the `redirects` configuration. For example, if you changed the module `MyApp.MyModule`
to `MyApp.My.Module` and the extra `get-started.md` to `quickstart.md`, you can
setup the following redirects:
<!-- tabs-open -->
### Elixir
For this example, we've changed the module `MyApp.MyModule` to `MyApp.My.Module`, and the extra `get-started.md` to `quickstart.md`
```elixir
defp docs do
[
...,
redirects: %{
MyApp.MyModule => MyApp.My.Module,
"get-started" => "quickstart"
}
]
end
redirects: %{
"MyApp.MyModule" => "MyApp.My.Module",
"get-started" => "quickstart"
}
```
### Erlang
For this example, we've changed the module `:my_module` to `:my_module2`, and the extra `get-started.md` to `quickstart.md`
```erlang
{redirects, [
{"my_module", "my_module2"},
{"get-started", "quickstart"}
]}.
```
<!-- tabs-close -->
## Contributing
The easiest way to test changes to ExDoc is to locally rebuild the app and its own documentation:
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule ExDoc.Config do
output: nil | Path.t(),
package: :atom | nil,
project: nil | String.t(),
redirects: %{optional(String.t()) => String.t()},
redirects: %{optional(String.t()) => String.t()} | [{String.t(), String.t()}],
retriever: atom(),
skip_undefined_reference_warnings_on: (String.t() -> boolean),
skip_code_autolink_to: (String.t() -> boolean),
Expand Down
24 changes: 4 additions & 20 deletions lib/ex_doc/formatter/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,32 +388,16 @@ defmodule ExDoc.Formatter.HTML do
config.redirects
|> Map.put_new("index", config.main)
|> Enum.map(fn {from, to} ->
source = stringify_redirect_item(from) <> ext
destination = stringify_redirect_item(to) <> ext
unless is_binary(from), do: raise "expected a string for the source of a redirect, got: #{inspect(from)}"
unless is_binary(to), do: raise "expected a string for the destination of a redirect, got: #{inspect(to)}"
source = from <> ext
destination = to <> ext
generate_redirect(source, config, destination)

source
end)
end

defp stringify_redirect_item(item) when is_binary(item) do
item
end

defp stringify_redirect_item(item) when is_atom(item) do
inspected = inspect(item)

case to_string(item) do
"Elixir." <> ^inspected -> inspected
other -> other
end
end

defp stringify_redirect_item(item) do
raise ArgumentError,
"redirect source and destination must be a string or an atom, got: #{inspect(item)}"
end

defp disambiguate_id(extra, discriminator) do
Map.put(extra, :id, "#{extra.id}-#{discriminator}")
end
Expand Down
10 changes: 0 additions & 10 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ defmodule ExDoc.Mixfile do
"Cheatsheet.cheatmd",
"CHANGELOG.md"
] ++ test_dev_examples(Mix.env()),
redirects: redirects(Mix.env()),
source_ref: "v#{@version}",
source_url: @source_url,
groups_for_modules: [
Expand All @@ -108,15 +107,6 @@ defmodule ExDoc.Mixfile do
defp test_dev_examples(:dev), do: Path.wildcard("test/examples/*")
defp test_dev_examples(_), do: []

defp redirects(:dev) do
%{
"old-admonition" => "admonition",
Exdoc.OldMarkdown => ExDoc.Markdown
}
end

defp redirects(_), do: %{}

defp clean_test_fixtures(_args) do
File.rm_rf("test/tmp")
end
Expand Down
21 changes: 21 additions & 0 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,27 @@ defmodule ExDoc.Formatter.HTMLTest do
end
end

describe "generates redirects" do
test "redirects are generated based on the configuration", %{tmp_dir: tmp_dir} = context do
generate_docs(doc_config(context, extras: ["test/fixtures/LICENSE"], redirects: %{
"/old-license" => "license"
}))

assert File.read!(tmp_dir <> "/html/old-license.html") == """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Elixir v1.0.1 — Documentation</title>
<meta http-equiv="refresh" content="0; url=license.html">
<meta name="generator" content="ExDoc v0.34.2">
</head>
<body></body>
</html>
"""
end
end

describe "generates extras" do
@extras [
"test/fixtures/LICENSE",
Expand Down

0 comments on commit 83551f8

Please sign in to comment.