Skip to content

Commit

Permalink
internal/frontend: properly generate tags for level 5 and 6 headings
Browse files Browse the repository at this point in the history
Before this change we were emitting h7 and h8 tags, which were then
getting sanitized out of the output.

Change-Id: I8d3d6d1691882ab1202e48d06a988f16cce03be0
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/544296
LUCI-TryBot-Result: Go LUCI <[email protected]>
kokoro-CI: kokoro <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
Run-TryBot: Michael Matloob <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
matloob committed Nov 30, 2023
1 parent 9f20944 commit e743b59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions internal/frontend/goldmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func (r *htmlRenderer) renderHeading(w util.BufWriter, source []byte, node ast.N
}
newLevel := n.Level + r.offset
if entering {
if n.Level > 6 {
// TODO(matloob): Do we want the div and h elements to have analagous classes?
// Currently we're using newLevel for the div's class but n.Level for the h element's
// class.
if newLevel > 6 {
_, _ = w.WriteString(fmt.Sprintf(`<div class="h%d" role="heading" aria-level="%d"`, newLevel, n.Level))
} else {
_, _ = w.WriteString(fmt.Sprintf(`<h%d class="h%d"`, newLevel, n.Level))
Expand All @@ -104,7 +107,7 @@ func (r *htmlRenderer) renderHeading(w util.BufWriter, source []byte, node ast.N
}
_ = w.WriteByte('>')
} else {
if n.Level > 6 {
if newLevel > 6 {
_, _ = w.WriteString("</div>\n")
} else {
_, _ = w.WriteString(fmt.Sprintf("</h%d>\n", newLevel))
Expand Down
18 changes: 18 additions & 0 deletions internal/frontend/readme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ func TestReadme(t *testing.T) {
{Level: 1, Text: "One", ID: "readme-one"},
},
},
{
name: "Heading level six",
unit: unit,
readme: &internal.Readme{
Filepath: sample.ReadmeFilePath,
Contents: "# One\n\n###### Six\n\n# One",
},
wantHTML: "<h3 class=\"h1\" id=\"readme-one\">One</h3>\n" +
"<div role=\"heading\" aria-level=\"6\" id=\"readme-six\">Six</div>\n" +
"<h3 class=\"h1\" id=\"readme-one-1\">One</h3>",

wantOutline: []*Heading{
{Level: 1, Text: "One", ID: "readme-one", Children: []*Heading{
{Level: 6, Text: "Six", ID: "readme-six"},
}},
{Level: 1, Text: "One", ID: "readme-one-1"},
},
},
{
name: "Github markdown emoji markup is properly rendered",
unit: unit,
Expand Down

0 comments on commit e743b59

Please sign in to comment.