Skip to content

Commit cfc4f2b

Browse files
committed
Add classes to sectionize
1 parent 578de37 commit cfc4f2b

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

lib/ex_doc/markdown.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ defmodule ExDoc.Markdown do
8686

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

9495
acc ->

test/ex_doc/formatter/html_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ defmodule ExDoc.Formatter.HTMLTest do
377377

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

380-
assert content =~ ~s{<section><h2 id="getting-started" class="section-heading">}
381-
assert content =~ ~s{<section><h3 id="hello-world" class="section-heading">}
382-
assert content =~ ~s{<section><h2 id="types" class="section-heading">}
383-
assert content =~ ~s{<section><h3 id="operators" class="section-heading">}
380+
assert content =~ ~s{<section class="h2"><h2 id="getting-started" class="section-heading">}
381+
assert content =~ ~s{<section class="h3"><h3 id="hello-world" class="section-heading">}
382+
assert content =~ ~s{<section class="h2"><h2 id="types" class="section-heading">}
383+
assert content =~ ~s{<section class="h3"><h3 id="operators" class="section-heading">}
384384
end
385385

386386
test "with absolute and dot-relative paths for extra", %{tmp_dir: tmp_dir} = context do

test/ex_doc/markdown_test.exs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ExDoc.MarkdownTest do
44
test "sectioninize" do
55
list = [
66
{:h1, [], ["H1"], %{}},
7-
{:h2, [], ["H2-1"], %{}},
7+
{:h2, [class: "example"], ["H2-1"], %{}},
88
{:p, [], ["p1"], %{}},
99
{:h3, [], ["H3-1"], %{}},
1010
{:p, [], ["p2"], %{}},
@@ -14,40 +14,40 @@ defmodule ExDoc.MarkdownTest do
1414
{:p, [], ["p4"], %{}},
1515
{:h2, [], ["H2-2"], %{}},
1616
{:p, [], ["p5"], %{}},
17-
{:h3, [], ["H3-1"], %{}},
17+
{:h3, [class: "last"], ["H3-1"], %{}},
1818
{:p, [], ["p6"], %{}}
1919
]
2020

2121
assert ExDoc.Markdown.sectionize(list, &h2_or_h3?/1) ==
2222
[
2323
{:h1, [], ["H1"], %{}},
24-
{:section, [],
24+
{:section, [class: "h2 example"],
2525
[
26-
{:h2, [], ["H2-1"], %{}},
26+
{:h2, [class: "example"], ["H2-1"], %{}},
2727
{:p, [], ["p1"], %{}},
28-
{:section, [],
28+
{:section, [class: "h3"],
2929
[
3030
{:h3, [], ["H3-1"], %{}},
3131
{:p, [], ["p2"], %{}}
3232
], %{}},
33-
{:section, [],
33+
{:section, [class: "h3"],
3434
[
3535
{:h3, [], ["H3-2"], %{}},
3636
{:p, [], ["p3"], %{}}
3737
], %{}},
38-
{:section, [],
38+
{:section, [class: "h3"],
3939
[
4040
{:h3, [], ["H3-3"], %{}},
4141
{:p, [], ["p4"], %{}}
4242
], %{}}
4343
], %{}},
44-
{:section, [],
44+
{:section, [class: "h2"],
4545
[
4646
{:h2, [], ["H2-2"], %{}},
4747
{:p, [], ["p5"], %{}},
48-
{:section, [],
48+
{:section, [class: "h3 last"],
4949
[
50-
{:h3, [], ["H3-1"], %{}},
50+
{:h3, [class: "last"], ["H3-1"], %{}},
5151
{:p, [], ["p6"], %{}}
5252
], %{}}
5353
], %{}}

0 commit comments

Comments
 (0)