2024-03-27 22:26:13 -04:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2024-06-13 21:26:33 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-03-27 22:26:13 -04:00
|
|
|
|
|
|
|
"github.com/yuin/goldmark/ast"
|
|
|
|
"github.com/yuin/goldmark/text"
|
|
|
|
)
|
|
|
|
|
2024-04-29 04:47:56 -04:00
|
|
|
func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Heading, reader text.Reader, tocList *[]markup.Header) {
|
2024-03-27 22:26:13 -04:00
|
|
|
for _, attr := range v.Attributes() {
|
|
|
|
if _, ok := attr.Value.([]byte); !ok {
|
|
|
|
v.SetAttribute(attr.Name, []byte(fmt.Sprintf("%v", attr.Value)))
|
|
|
|
}
|
|
|
|
}
|
2024-10-31 08:05:54 -04:00
|
|
|
txt := v.Text(reader.Source()) //nolint:staticcheck
|
2024-03-27 22:26:13 -04:00
|
|
|
header := markup.Header{
|
2024-06-13 21:26:33 -04:00
|
|
|
Text: util.UnsafeBytesToString(txt),
|
2024-03-27 22:26:13 -04:00
|
|
|
Level: v.Level,
|
|
|
|
}
|
|
|
|
if id, found := v.AttributeString("id"); found {
|
2024-06-13 21:26:33 -04:00
|
|
|
header.ID = util.UnsafeBytesToString(id.([]byte))
|
2024-03-27 22:26:13 -04:00
|
|
|
}
|
|
|
|
*tocList = append(*tocList, header)
|
|
|
|
g.applyElementDir(v)
|
|
|
|
}
|