mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-11 16:39:11 +02:00
fix: correct HTML nesting structure in TOC rendering
Fix invalid HTML structure where <ul> was directly nested inside <ul>. According to HTML specification, nested <ul> elements must be wrapped in <li> elements. Before: <ul><ul>...</ul></ul> After: <ul><li><ul>...</ul></li></ul>
This commit is contained in:
parent
24f253fe44
commit
a8a1873b27
@ -254,26 +254,35 @@ func RenderTocHeadingItems(ctx *RenderContext, nodeDetailsAttrs map[string]strin
|
||||
currentLevel := baseLevel
|
||||
indent := []byte{' ', ' '}
|
||||
_, _ = htmlutil.HTMLPrint(out, "<ul>\n")
|
||||
for _, header := range ctx.TocHeadingItems {
|
||||
for i, header := range ctx.TocHeadingItems {
|
||||
// Go deeper: open nested <ul> elements (wrapped in <li> for valid HTML)
|
||||
for currentLevel < header.HeadingLevel {
|
||||
_, _ = out.Write(indent)
|
||||
_, _ = htmlutil.HTMLPrint(out, "<ul>\n")
|
||||
_, _ = htmlutil.HTMLPrint(out, "<li><ul>\n")
|
||||
indent = append(indent, ' ', ' ')
|
||||
currentLevel++
|
||||
}
|
||||
// Go shallower: close nested </ul></li> elements
|
||||
for currentLevel > header.HeadingLevel {
|
||||
indent = indent[:len(indent)-2]
|
||||
_, _ = out.Write(indent)
|
||||
_, _ = htmlutil.HTMLPrint(out, "</ul>\n")
|
||||
_, _ = htmlutil.HTMLPrint(out, "</ul></li>\n")
|
||||
currentLevel--
|
||||
}
|
||||
_, _ = out.Write(indent)
|
||||
_, _ = htmlutil.HTMLPrintf(out, "<li><a href=\"#%s\">%s</a></li>\n", header.AnchorID, header.InnerText)
|
||||
_, _ = htmlutil.HTMLPrintf(out, "<li><a href=\"#%s\">%s</a>", header.AnchorID, header.InnerText)
|
||||
// Check if next item is at a deeper level - if so, don't close the <li> yet
|
||||
nextIsDeeper := i+1 < len(ctx.TocHeadingItems) && ctx.TocHeadingItems[i+1].HeadingLevel > header.HeadingLevel
|
||||
if !nextIsDeeper {
|
||||
_, _ = htmlutil.HTMLPrint(out, "</li>")
|
||||
}
|
||||
_, _ = htmlutil.HTMLPrint(out, "\n")
|
||||
}
|
||||
// Close any remaining nested levels
|
||||
for currentLevel > baseLevel {
|
||||
indent = indent[:len(indent)-2]
|
||||
_, _ = out.Write(indent)
|
||||
_, _ = htmlutil.HTMLPrint(out, "</ul>\n")
|
||||
_, _ = htmlutil.HTMLPrint(out, "</ul></li>\n")
|
||||
currentLevel--
|
||||
}
|
||||
_, _ = htmlutil.HTMLPrint(out, "</ul>\n</details>\n")
|
||||
|
||||
@ -38,16 +38,16 @@ include_toc: true
|
||||
result = re.ReplaceAllString(result, "\n")
|
||||
expected := `<details><summary>toc</summary>
|
||||
<ul>
|
||||
<li><a href="#user-content-tag-link-and-bold" rel="nofollow">tag link and Bold</a></li>
|
||||
<ul>
|
||||
<li><a href="#user-content-code-block-a" rel="nofollow">code block <a></a></li>
|
||||
<ul>
|
||||
<ul>
|
||||
<li><a href="#user-content-tag-link-and-bold" rel="nofollow">tag link and Bold</a>
|
||||
<li><ul>
|
||||
<li><a href="#user-content-code-block-a" rel="nofollow">code block <a></a>
|
||||
<li><ul>
|
||||
<li><ul>
|
||||
<li><a href="#user-content-markdown-bold" rel="nofollow">markdown bold</a></li>
|
||||
</ul>
|
||||
</ul>
|
||||
</ul></li>
|
||||
</ul></li>
|
||||
<li><a href="#user-content-last" rel="nofollow">last</a></li>
|
||||
</ul>
|
||||
</ul></li>
|
||||
</ul>
|
||||
</details>
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user