Skip to content

Commit

Permalink
(epub): Avoid duplicate IDs on Package Definition
Browse files Browse the repository at this point in the history
Currently the ID generation for static files is based on the file name,
but the JS/CSS assets included by ExDoc have the same prefix, example:

* `epub-<hash>.js`
* `epub-<hash>.css`

This will produce duplicate IDs in the Package Document Definition,
which is an error.

Now we also take into account the extension for the static files.
  • Loading branch information
milmazz committed Jul 30, 2019
1 parent 36e7bbf commit e9e261b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/ex_doc/formatter/epub/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ defmodule ExDoc.Formatter.EPUB.Templates do
module_template(config, module_node, summary)
end

@doc """
Generated ID for static file
"""
def static_file_to_id(static_file) do
prefix = static_file |> HTML.filename_to_title() |> HTML.text_to_id()
extension = static_file |> Path.extname() |> String.replace_prefix(".", "-")

"#{prefix}#{extension}"
end

@doc """
Creates the Package Document Definition.
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/epub/templates/content_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<% end %>
<% end %>
<%= for static_file <- static_files do %>
<item id="<%= static_file |> HTML.filename_to_title() |> HTML.text_to_id() %>" href="<%= static_file %>" media-type="<%= media_type(Path.extname(static_file)) %>"/>
<item id="<%= static_file_to_id(static_file) %>" href="<%= static_file %>" media-type="<%= media_type(Path.extname(static_file)) %>"/>
<% end %>
<%= if config.cover do %>
<%= if Path.extname(config.cover) == ".png" do %>
Expand Down

0 comments on commit e9e261b

Please sign in to comment.