Skip to content

Commit 25329b5

Browse files
committed
Add link headings to epub
1 parent 7bad4fa commit 25329b5

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

lib/ex_doc/formatter/epub/templates/extra_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h1 id="content">
33
<%= title_content %>
44
</h1>
5-
<%= content %>
5+
<%= H.link_headings(content) %>
66
<%# Extra content specified by the user (e.g. custom Javascript) %>
77
<%= before_closing_body_tag(config, :epub) %>
88
</body>

lib/ex_doc/formatter/epub/templates/module_template.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<%= if doc = module.rendered_doc do %>
1313
<section id="moduledoc" class="docstring">
14-
<%= doc %>
14+
<%= H.link_moduledoc_headings(doc) %>
1515
</section>
1616
<% end %>
1717

lib/ex_doc/formatter/html/templates.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ defmodule ExDoc.Formatter.HTML.Templates do
228228

229229
defp relative_asset([h | _], output, _pattern), do: Path.relative_to(h, output)
230230

231+
# TODO: Move link_headings and friends to html.ex or even to autolinking code,
232+
# so content is built with it upfront instead of added at the template level.
233+
231234
@doc """
232235
Link headings found with `regex` with in the given `content`. IDs are
233236
prefixed with `prefix`.
@@ -307,11 +310,11 @@ defmodule ExDoc.Formatter.HTML.Templates do
307310
"""
308311
end
309312

310-
defp link_moduledoc_headings(content) do
313+
def link_moduledoc_headings(content) do
311314
link_headings(content, @heading_regex, "module-")
312315
end
313316

314-
defp link_detail_headings(content, prefix) do
317+
def link_detail_headings(content, prefix) do
315318
link_headings(content, @heading_regex, prefix <> "-")
316319
end
317320

test/ex_doc/formatter/epub/templates_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ defmodule ExDoc.Formatter.EPUB.TemplatesTest do
5959
assert content =~ ~r{<title>CompiledWithDocs [^<]*</title>}
6060
assert content =~ ~r{<h1 id="content">\s*CompiledWithDocs\s*}
6161

62+
assert content =~
63+
~r{<h2 id="module-example-unicode-escaping" class="section-heading">.*<a href="#module-example-unicode-escaping">.*<i class="ri-link-m" aria-hidden="true"></i>.*Example.*</a>.*</h2>}ms
64+
65+
assert content =~
66+
~r{<h3 id="module-example-h3-heading" class="section-heading">.*<a href="#module-example-h3-heading">.*<i class="ri-link-m" aria-hidden="true"></i>.*Example H3 heading.*</a>.*</h3>}ms
67+
6268
assert content =~
6369
~r{moduledoc.*Example.*<samp class="nc">CompiledWithDocs</samp><samp class="o">\.</samp><samp class="n">example</samp>.*}ms
6470

0 commit comments

Comments
 (0)