summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWojtek Mach <wojtekmach@users.noreply.github.com>2023-05-09 11:14:22 +0200
committerGitHub <noreply@github.com>2023-05-09 11:14:22 +0200
commite104b8d6095c200b450964f1cf53338db8f91335 (patch)
tree66bccaafd10f97b1decae258306d212f45d43066
parentb8770d414d1b67ead1ed02d0f5f13c8a8fba4f27 (diff)
downloadelixir-e104b8d6095c200b450964f1cf53338db8f91335.tar.gz
Don't render HTML comments in IEx docs (#12551)
-rw-r--r--lib/elixir/lib/io/ansi/docs.ex25
-rw-r--r--lib/elixir/test/elixir/io/ansi/docs_test.exs48
2 files changed, 73 insertions, 0 deletions
diff --git a/lib/elixir/lib/io/ansi/docs.ex b/lib/elixir/lib/io/ansi/docs.ex
index 5f6d7c7e7..ba6a8b07e 100644
--- a/lib/elixir/lib/io/ansi/docs.ex
+++ b/lib/elixir/lib/io/ansi/docs.ex
@@ -366,6 +366,10 @@ defmodule IO.ANSI.Docs do
process_fenced_code_block(rest, text, indent, options, _delimiter = "~~~")
end
+ defp process(["<!--" <> line | rest], text, indent, options) do
+ process(drop_comment([line | rest]), text, indent, options)
+ end
+
defp process(all = [line | rest], text, indent, options) do
{stripped, count} = strip_spaces(line, 0, :infinity)
@@ -804,6 +808,22 @@ defmodule IO.ANSI.Docs do
Regex.replace(~r{\[([^\]]*?)\]\((.*?)\)}, text, "\\1 (\\2)")
end
+ defp drop_comment(line) when is_binary(line) do
+ [_comment, rest] = :binary.split(line, "-->")
+ rest
+ end
+
+ defp drop_comment([line | rest]) do
+ case :binary.split(line, "-->") do
+ [_] -> drop_comment(rest)
+ [_, line] -> [line | rest]
+ end
+ end
+
+ defp drop_comment([]) do
+ []
+ end
+
# We have four entries: **, __, *, _ and `.
#
# The first four behave the same while the last one is simpler
@@ -839,6 +859,11 @@ defmodule IO.ANSI.Docs do
### Inline delimiters
+ defp handle_inline("<!--" <> rest, nil, buffer, acc, options) do
+ rest = drop_comment(rest)
+ handle_inline(rest, [], buffer, acc, options)
+ end
+
defp handle_inline(<<delimiter, mark, mark, rest::binary>>, nil, buffer, acc, options)
when rest != "" and delimiter in @delimiters and mark in @single do
acc = [delimiter, Enum.reverse(buffer) | acc]
diff --git a/lib/elixir/test/elixir/io/ansi/docs_test.exs b/lib/elixir/test/elixir/io/ansi/docs_test.exs
index 167d0964a..7e9bb9eda 100644
--- a/lib/elixir/test/elixir/io/ansi/docs_test.exs
+++ b/lib/elixir/test/elixir/io/ansi/docs_test.exs
@@ -598,6 +598,54 @@ defmodule IO.ANSI.DocsTest do
assert format_markdown(table) == expected
end
+
+ test "HTML comments are ignored" do
+ markdown = """
+ <!-- comment -->
+ hello
+ """
+
+ assert format_markdown(markdown) == "hello\n\e[0m"
+
+ markdown = """
+ <!-- comment -->
+
+ hello
+ """
+
+ assert format_markdown(markdown) == "hello\n\e[0m"
+
+ markdown = """
+ hello
+ <!-- comment -->
+ world
+ """
+
+ assert format_markdown(markdown) == "hello\n\e[0m\nworld\n\e[0m"
+
+ markdown = """
+ hello
+
+ <!-- comment -->
+
+ world
+ """
+
+ assert format_markdown(markdown) == "hello\n\e[0m\nworld\n\e[0m"
+
+ markdown = """
+ hello
+ <!-- comment --> world
+ """
+
+ assert format_markdown(markdown) == "hello world\n\e[0m"
+
+ markdown = """
+ hello <!-- comment --> world
+ """
+
+ assert format_markdown(markdown) == "hello world\n\e[0m"
+ end
end
describe "erlang" do