Skip to content

Commit

Permalink
Add classes to sectionize
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 26, 2022
1 parent 578de37 commit cfc4f2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/ex_doc/markdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ defmodule ExDoc.Markdown do

defp sectionize(list, matcher, acc) do
case pivot(list, acc, matcher) do
{acc, {header_tag, _, _, _} = header, rest} ->
{acc, {header_tag, header_attrs, _, _} = header, rest} ->
{inner, rest} = Enum.split_while(rest, &not_tag?(&1, header_tag))
section = {:section, [], [header | sectionize(inner, matcher, [])], %{}}
class = String.trim_trailing("#{header_tag} #{header_attrs[:class]}")
section = {:section, [class: class], [header | sectionize(inner, matcher, [])], %{}}
sectionize(rest, matcher, [section | acc])

acc ->
Expand Down
8 changes: 4 additions & 4 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ defmodule ExDoc.Formatter.HTMLTest do

content = File.read!(tmp_dir <> "/html/cheatsheets.html")

assert content =~ ~s{<section><h2 id="getting-started" class="section-heading">}
assert content =~ ~s{<section><h3 id="hello-world" class="section-heading">}
assert content =~ ~s{<section><h2 id="types" class="section-heading">}
assert content =~ ~s{<section><h3 id="operators" class="section-heading">}
assert content =~ ~s{<section class="h2"><h2 id="getting-started" class="section-heading">}
assert content =~ ~s{<section class="h3"><h3 id="hello-world" class="section-heading">}
assert content =~ ~s{<section class="h2"><h2 id="types" class="section-heading">}
assert content =~ ~s{<section class="h3"><h3 id="operators" class="section-heading">}
end

test "with absolute and dot-relative paths for extra", %{tmp_dir: tmp_dir} = context do
Expand Down
20 changes: 10 additions & 10 deletions test/ex_doc/markdown_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExDoc.MarkdownTest do
test "sectioninize" do
list = [
{:h1, [], ["H1"], %{}},
{:h2, [], ["H2-1"], %{}},
{:h2, [class: "example"], ["H2-1"], %{}},
{:p, [], ["p1"], %{}},
{:h3, [], ["H3-1"], %{}},
{:p, [], ["p2"], %{}},
Expand All @@ -14,40 +14,40 @@ defmodule ExDoc.MarkdownTest do
{:p, [], ["p4"], %{}},
{:h2, [], ["H2-2"], %{}},
{:p, [], ["p5"], %{}},
{:h3, [], ["H3-1"], %{}},
{:h3, [class: "last"], ["H3-1"], %{}},
{:p, [], ["p6"], %{}}
]

assert ExDoc.Markdown.sectionize(list, &h2_or_h3?/1) ==
[
{:h1, [], ["H1"], %{}},
{:section, [],
{:section, [class: "h2 example"],
[
{:h2, [], ["H2-1"], %{}},
{:h2, [class: "example"], ["H2-1"], %{}},
{:p, [], ["p1"], %{}},
{:section, [],
{:section, [class: "h3"],
[
{:h3, [], ["H3-1"], %{}},
{:p, [], ["p2"], %{}}
], %{}},
{:section, [],
{:section, [class: "h3"],
[
{:h3, [], ["H3-2"], %{}},
{:p, [], ["p3"], %{}}
], %{}},
{:section, [],
{:section, [class: "h3"],
[
{:h3, [], ["H3-3"], %{}},
{:p, [], ["p4"], %{}}
], %{}}
], %{}},
{:section, [],
{:section, [class: "h2"],
[
{:h2, [], ["H2-2"], %{}},
{:p, [], ["p5"], %{}},
{:section, [],
{:section, [class: "h3 last"],
[
{:h3, [], ["H3-1"], %{}},
{:h3, [class: "last"], ["H3-1"], %{}},
{:p, [], ["p6"], %{}}
], %{}}
], %{}}
Expand Down

0 comments on commit cfc4f2b

Please sign in to comment.