Skip to content

Commit

Permalink
Don't crash on markdown comments, closes #1169
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed May 17, 2020
1 parent efbacfd commit fc945e9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
45 changes: 39 additions & 6 deletions lib/ex_doc/markdown/earmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,45 @@ defmodule ExDoc.Markdown.Earmark do
end
end

defp fixup(list) when is_list(list), do: Enum.map(list, &fixup/1)
defp fixup(binary) when is_binary(binary), do: binary
defp fixup({tag, attrs, ast}), do: {fixup_tag(tag), Enum.map(attrs, &fixup_attr/1), fixup(ast)}
defp fixup({tag, attrs, ast, _meta}), do: fixup({tag, attrs, ast})
defp fixup(list) when is_list(list) do
fixup_list(list, [])
end

defp fixup(binary) when is_binary(binary) do
binary
end

defp fixup({tag, attrs, ast}) when is_binary(tag) do
{fixup_tag(tag), Enum.map(attrs, &fixup_attr/1), fixup(ast)}
end

defp fixup({tag, attrs, ast, _meta}) when is_binary(tag) do
fixup({tag, attrs, ast})
end

defp fixup({:comment, _, _}) do
[]
end

defp fixup_tag(tag), do: String.to_atom(tag)
defp fixup_list([head | tail], acc) do
fixed = fixup(head)

defp fixup_attr({name, value}), do: {String.to_atom(name), value}
if fixed == [] do
fixup_list(tail, acc)
else
fixup_list(tail, [fixed | acc])
end
end

defp fixup_list([], acc) do
Enum.reverse(acc)
end

defp fixup_tag(tag) do
String.to_atom(tag)
end

defp fixup_attr({name, value}) do
{String.to_atom(name), value}
end
end
4 changes: 4 additions & 0 deletions test/ex_doc/markdown/earmark_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ defmodule ExDoc.Markdown.EarmarkTest do
test "empty input" do
assert Markdown.to_ast("", []) == []
end

test "comments" do
assert Markdown.to_ast("<!-- INCLUDE -->", []) == []
end
end
end

0 comments on commit fc945e9

Please sign in to comment.