Skip to content

Commit

Permalink
Fix escaping #{} in search_items.json (#1728)
Browse files Browse the repository at this point in the history
Inspect implementation for String is such that:

    iex> ~S"#{}"
    "\#{}"

However `\#` is invalid JSON escape code.
  • Loading branch information
wojtekmach committed Jul 4, 2023
1 parent 6043f5e commit d617f9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ex_doc/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ defmodule ExDoc.Utils do
end

def to_json(binary) when is_binary(binary) do
inspect(binary, printable_limit: :infinity)
binary
|> inspect(printable_limit: :infinity)
|> String.replace("\\#\{", "#\{")
end

def to_json(integer) when is_integer(integer) do
Expand Down
16 changes: 16 additions & 0 deletions test/ex_doc/formatter/html/search_items_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ defmodule ExDoc.Formatter.HTML.SearchItemsTest do
assert item["doc"] == ""
end

test "escaping", c do
modules =
elixirc(c, ~S'''
defmodule SearchFoo do
@moduledoc ~S"""
#{}
"""
end
''')

config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
[item] = search_items(modules, config)

assert item["doc"] == ~S"#{}"
end

@tag :otp_eep48
test "Erlang module", c do
[module] =
Expand Down

0 comments on commit d617f9e

Please sign in to comment.