Skip to content

Commit c4c9518

Browse files
committed
Add .cheatmd to epub to avoid broken links
Closes elixir-lang/elixir#14753.
1 parent 037ec01 commit c4c9518

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

lib/ex_doc/formatter/epub.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defmodule ExDoc.Formatter.EPUB do
6363
defp generate_extras(config) do
6464
for {_title, extras} <- config.extras,
6565
node <- extras,
66-
not is_map_key(node, :url) and node.type != :cheatmd do
66+
not is_map_key(node, :url) do
6767
output = "#{config.output}/OEBPS/#{node.id}.xhtml"
6868
html = Templates.extra_template(config, node)
6969

test/ex_doc/formatter/epub_test.exs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,66 @@ defmodule ExDoc.Formatter.EPUBTest do
126126
assert content =~ ~r{.*"Mix\.Tasks\.TaskWithDocs\".*}ms
127127
end
128128

129-
test "generates the readme file", %{tmp_dir: tmp_dir} = context do
129+
test "generates the readme file as main", %{tmp_dir: tmp_dir} = context do
130130
config = doc_config(context, main: "README", extras: ["test/fixtures/README.md"])
131131
generate_docs_and_unzip(context, config)
132132

133-
content = File.read!(tmp_dir <> "/epub/OEBPS/readme.xhtml")
134-
assert content =~ ~r{<title>README [^<]*</title>}
135-
assert content =~ ~r{<a href="RandomError.xhtml"><code(\sclass="inline")?>RandomError</code>}
133+
content = File.read!(tmp_dir <> "/epub/OEBPS/nav.xhtml")
134+
assert content =~ ~r{<li><a href="readme.xhtml">README</a></li>}
135+
end
136+
137+
test "generates extras", %{tmp_dir: tmp_dir} = context do
138+
config =
139+
doc_config(context,
140+
extras: [
141+
"test/fixtures/LICENSE",
142+
"test/fixtures/PlainText.txt",
143+
"test/fixtures/PlainTextFiles.md",
144+
"test/fixtures/README.md",
145+
"test/fixtures/LivebookFile.livemd",
146+
"test/fixtures/cheatsheets.cheatmd"
147+
]
148+
)
149+
150+
generate_docs_and_unzip(context, config)
151+
152+
content = File.read!(tmp_dir <> "/epub/OEBPS/plaintextfiles.xhtml")
153+
154+
assert content =~ ~r{Plain Text Files</h1>}s
136155

137156
assert content =~
138-
~r{<a href="CustomBehaviourImpl.xhtml#hello/1"><code(\sclass="inline")?>CustomBehaviourImpl.hello/1</code>}
157+
~r{<p>Read the <a href="license.xhtml">license</a> and the <a href="plaintext.xhtml">plain-text file</a>.}
158+
159+
plain_text_file = File.read!(tmp_dir <> "/epub/OEBPS/plaintext.xhtml")
160+
161+
assert plain_text_file =~ ~r{PlainText</h1>}s
162+
163+
assert plain_text_file =~
164+
~r{<pre>\nThis is plain\n text and nothing\n.+\s+good bye\n</pre>}s
165+
166+
assert plain_text_file =~ ~s{\n## Neither formatted\n}
167+
assert plain_text_file =~ ~s{\n `t:term/0`\n}
168+
169+
license = File.read!(tmp_dir <> "/epub/OEBPS/license.xhtml")
170+
171+
assert license =~ ~r{LICENSE</h1>}s
172+
173+
assert license =~
174+
~s{<pre>\nLicensed under the Apache License, Version 2.0 (the &quot;License&quot;)}
175+
176+
content = File.read!(tmp_dir <> "/epub/OEBPS/livebookfile.xhtml")
177+
178+
assert content =~ ~r{Title for Livebook Files</h1>}
139179

140180
assert content =~
141-
~r{<a href="TypesAndSpecs.Sub.xhtml"><code(\sclass="inline")?>TypesAndSpecs.Sub</code></a>}
181+
~s{<p>Read <code class="inline">.livemd</code> files generated by <a href="https://github.com/livebook-dev/livebook">livebook</a>.}
142182

143-
content = File.read!(tmp_dir <> "/epub/OEBPS/nav.xhtml")
144-
assert content =~ ~r{<li><a href="readme.xhtml">README</a></li>}
183+
content = File.read!(tmp_dir <> "/epub/OEBPS/cheatsheets.xhtml")
184+
185+
assert content =~ ~s{<h2 id="getting-started">}
186+
assert content =~ ~s{<h3 id="hello-world">}
187+
assert content =~ ~s{<h2 id="types">}
188+
assert content =~ ~s{<h3 id="operators">}
145189
end
146190

147191
test "ignores any external url extras", %{tmp_dir: tmp_dir} = context do

0 commit comments

Comments
 (0)