Skip to content

Commit

Permalink
Ignore vegalite codeblocks in search (#1736)
Browse files Browse the repository at this point in the history
  • Loading branch information
enewbury authored Jul 11, 2023
1 parent 5eb1f00 commit 4b5639c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ex_doc/formatter/html/search_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ defmodule ExDoc.Formatter.HTML.SearchData do
section =
section
|> HTML.strip_tags(" ")
|> drop_ignorable_codeblocks()
|> String.trim()

{clean_markdown(header), section}
Expand All @@ -146,4 +147,11 @@ defmodule ExDoc.Formatter.HTML.SearchData do
|> String.replace(~r/\s+/, " ")
|> String.trim()
end

@ignored_codeblocks ~w[vega-lite]

defp drop_ignorable_codeblocks(section) do
block_names = Enum.join(@ignored_codeblocks, "|")
String.replace(section, ~r/^```(?:#{block_names})\n(?:[\s\S]*?)```$/m, "")
end
end
31 changes: 31 additions & 0 deletions test/ex_doc/formatter/html/search_data_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,37 @@ defmodule ExDoc.Formatter.HTML.SearchDataTest do
assert item2["doc"] == "Section _1_ content."
end

test "drops vega-lite blocks", c do
readme_path = "#{c.tmp_dir}/README.md"

File.write!(readme_path, """
# Foo
_Foo_ content.
## Section 1 Header
```vega-lite
graph
```
Section _1_ content.
""")

config = %ExDoc.Config{output: "#{c.tmp_dir}/doc", extras: [readme_path]}
[item1, item2] = search_data([], config)["items"]

assert item1["ref"] == "readme.html"
assert item1["type"] == "extras"
assert item1["title"] == "Foo"
assert item1["doc"] == "# Foo\n\n_Foo_ content."

assert item2["ref"] == "readme.html#section-1-header"
assert item2["type"] == "extras"
assert item2["title"] == "Section 1 Header - Foo"
assert item2["doc"] == "Section _1_ content."
end

defp search_data(modules, config) do
modules = ExDoc.Retriever.docs_from_modules(modules, config)

Expand Down

0 comments on commit 4b5639c

Please sign in to comment.