gitea源码

transform_heading.go 866B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markdown
  4. import (
  5. "fmt"
  6. "code.gitea.io/gitea/modules/markup"
  7. "code.gitea.io/gitea/modules/util"
  8. "github.com/yuin/goldmark/ast"
  9. "github.com/yuin/goldmark/text"
  10. )
  11. func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Heading, reader text.Reader, tocList *[]Header) {
  12. for _, attr := range v.Attributes() {
  13. if _, ok := attr.Value.([]byte); !ok {
  14. v.SetAttribute(attr.Name, fmt.Appendf(nil, "%v", attr.Value))
  15. }
  16. }
  17. txt := v.Text(reader.Source()) //nolint:staticcheck // Text is deprecated
  18. header := Header{
  19. Text: util.UnsafeBytesToString(txt),
  20. Level: v.Level,
  21. }
  22. if id, found := v.AttributeString("id"); found {
  23. header.ID = util.UnsafeBytesToString(id.([]byte))
  24. }
  25. *tocList = append(*tocList, header)
  26. g.applyElementDir(v)
  27. }