Fix markup heading parsing, fix emphasis parsing (#36284)

Fixes #36106, fix #17958

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Heath Dutton🕴️
2026-01-23 15:24:58 -05:00
committed by GitHub
parent cfd7218395
commit 0f78b99998
15 changed files with 260 additions and 196 deletions
+49 -1
View File
@@ -12,7 +12,9 @@ import (
"strings"
"sync"
"code.gitea.io/gitea/modules/htmlutil"
"code.gitea.io/gitea/modules/markup/common"
"code.gitea.io/gitea/modules/translation"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
@@ -234,6 +236,49 @@ func postProcessString(ctx *RenderContext, procs []processor, content string) (s
return buf.String(), nil
}
func RenderTocHeadingItems(ctx *RenderContext, nodeDetailsAttrs map[string]string, out io.Writer) {
locale, ok := ctx.Value(translation.ContextKey).(translation.Locale)
if !ok {
locale = translation.NewLocale("")
}
_, _ = htmlutil.HTMLPrintTag(out, "details", nodeDetailsAttrs)
_, _ = htmlutil.HTMLPrintf(out, "<summary>%s</summary>\n", locale.TrString("toc"))
baseLevel := 6
for _, header := range ctx.TocHeadingItems {
if header.HeadingLevel < baseLevel {
baseLevel = header.HeadingLevel
}
}
currentLevel := baseLevel
indent := []byte{' ', ' '}
_, _ = htmlutil.HTMLPrint(out, "<ul>\n")
for _, header := range ctx.TocHeadingItems {
for currentLevel < header.HeadingLevel {
_, _ = out.Write(indent)
_, _ = htmlutil.HTMLPrint(out, "<ul>\n")
indent = append(indent, ' ', ' ')
currentLevel++
}
for currentLevel > header.HeadingLevel {
indent = indent[:len(indent)-2]
_, _ = out.Write(indent)
_, _ = htmlutil.HTMLPrint(out, "</ul>\n")
currentLevel--
}
_, _ = out.Write(indent)
_, _ = htmlutil.HTMLPrintf(out, "<li><a href=\"#%s\">%s</a></li>\n", header.AnchorID, header.InnerText)
}
for currentLevel > baseLevel {
indent = indent[:len(indent)-2]
_, _ = out.Write(indent)
_, _ = htmlutil.HTMLPrint(out, "</ul>\n")
currentLevel--
}
_, _ = htmlutil.HTMLPrint(out, "</ul>\n</details>\n")
}
func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output io.Writer) error {
if !ctx.usedByRender && ctx.RenderHelper != nil {
defer ctx.RenderHelper.CleanUp()
@@ -284,6 +329,9 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
}
// Render everything to buf.
if ctx.TocShowInSection == TocShowInMain && len(ctx.TocHeadingItems) > 0 {
RenderTocHeadingItems(ctx, nil, output)
}
for _, node := range newNodes {
if err := html.Render(output, node); err != nil {
return fmt.Errorf("markup.postProcess: html.Render: %w", err)
@@ -314,7 +362,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Nod
return node.NextSibling
}
processNodeAttrID(ctx, node)
processNodeHeadingAndID(ctx, node)
processFootnoteNode(ctx, node) // FIXME: the footnote processing should be done in the "footnote.go" renderer directly
if isEmojiNode(node) {