From 4b5639cbac596d8207d5f0a355fa64f695b0ad50 Mon Sep 17 00:00:00 2001 From: Eric Newbury Date: Tue, 11 Jul 2023 14:19:41 -0400 Subject: [PATCH] Ignore vegalite codeblocks in search (#1736) --- lib/ex_doc/formatter/html/search_data.ex | 8 +++++ .../formatter/html/search_data_test.exs | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/ex_doc/formatter/html/search_data.ex b/lib/ex_doc/formatter/html/search_data.ex index a534dd911..d081d06d1 100644 --- a/lib/ex_doc/formatter/html/search_data.ex +++ b/lib/ex_doc/formatter/html/search_data.ex @@ -126,6 +126,7 @@ defmodule ExDoc.Formatter.HTML.SearchData do section = section |> HTML.strip_tags(" ") + |> drop_ignorable_codeblocks() |> String.trim() {clean_markdown(header), section} @@ -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 diff --git a/test/ex_doc/formatter/html/search_data_test.exs b/test/ex_doc/formatter/html/search_data_test.exs index c135f7525..9cfda6fb3 100644 --- a/test/ex_doc/formatter/html/search_data_test.exs +++ b/test/ex_doc/formatter/html/search_data_test.exs @@ -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)