From e2fddcf681e9206b3da826680205537f87690b7f Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 2 Nov 2024 13:35:24 +0100 Subject: [PATCH] chore: fix deprecation --- modules/markup/markdown/callout/github.go | 6 ++--- .../markup/markdown/callout/github_legacy.go | 4 ++- modules/markup/markdown/transform_codespan.go | 3 ++- modules/markup/markdown/transform_heading.go | 2 +- modules/markup/markdown/util/text.go | 26 +++++++++++++++++++ modules/markup/mdstripper/mdstripper.go | 2 +- 6 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 modules/markup/markdown/util/text.go diff --git a/modules/markup/markdown/callout/github.go b/modules/markup/markdown/callout/github.go index 9b8b611d18..adc2071823 100644 --- a/modules/markup/markdown/callout/github.go +++ b/modules/markup/markdown/callout/github.go @@ -50,7 +50,7 @@ func (g *GitHubCalloutTransformer) Transform(node *ast.Document, reader text.Rea return ast.WalkContinue, nil } firstTextNode, ok := firstParagraph.FirstChild().(*ast.Text) - if !ok || string(firstTextNode.Text(reader.Source())) != "[" { + if !ok || string(firstTextNode.Value(reader.Source())) != "[" { return ast.WalkContinue, nil } secondTextNode, ok := firstTextNode.NextSibling().(*ast.Text) @@ -59,14 +59,14 @@ func (g *GitHubCalloutTransformer) Transform(node *ast.Document, reader text.Rea } // If the second node's text isn't one of the supported attention // types, continue walking. - secondTextNodeText := secondTextNode.Text(reader.Source()) + secondTextNodeText := secondTextNode.Value(reader.Source()) attentionType := strings.ToLower(strings.TrimPrefix(string(secondTextNodeText), "!")) if _, has := supportedAttentionTypes[attentionType]; !has { return ast.WalkContinue, nil } thirdTextNode, ok := secondTextNode.NextSibling().(*ast.Text) - if !ok || string(thirdTextNode.Text(reader.Source())) != "]" { + if !ok || string(thirdTextNode.Value(reader.Source())) != "]" { return ast.WalkContinue, nil } diff --git a/modules/markup/markdown/callout/github_legacy.go b/modules/markup/markdown/callout/github_legacy.go index 32a278bc8d..32f33b02c7 100644 --- a/modules/markup/markdown/callout/github_legacy.go +++ b/modules/markup/markdown/callout/github_legacy.go @@ -7,6 +7,8 @@ package callout import ( "strings" + "code.gitea.io/gitea/modules/markup/markdown/util" + "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/text" @@ -40,7 +42,7 @@ func (g *GitHubLegacyCalloutTransformer) Transform(node *ast.Document, reader te if !ok { return ast.WalkContinue, nil } - calloutText := string(calloutNode.Text(reader.Source())) + calloutText := string(util.Text(calloutNode, reader.Source())) calloutType := strings.ToLower(calloutText) // We only support "Note" and "Warning" callouts in legacy mode, // match only those. diff --git a/modules/markup/markdown/transform_codespan.go b/modules/markup/markdown/transform_codespan.go index a2cd4fb5ba..45e3c20c8e 100644 --- a/modules/markup/markdown/transform_codespan.go +++ b/modules/markup/markdown/transform_codespan.go @@ -9,6 +9,7 @@ import ( "strings" "code.gitea.io/gitea/modules/markup" + mdutil "code.gitea.io/gitea/modules/markup/markdown/util" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/renderer/html" @@ -49,7 +50,7 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod } func (g *ASTTransformer) transformCodeSpan(_ *markup.RenderContext, v *ast.CodeSpan, reader text.Reader) { - colorContent := v.Text(reader.Source()) + colorContent := mdutil.Text(v, reader.Source()) if matchColor(strings.ToLower(string(colorContent))) { v.AppendChild(v, NewColorPreview(colorContent)) } diff --git a/modules/markup/markdown/transform_heading.go b/modules/markup/markdown/transform_heading.go index 6d48f34d93..d869ee5de9 100644 --- a/modules/markup/markdown/transform_heading.go +++ b/modules/markup/markdown/transform_heading.go @@ -19,7 +19,7 @@ func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Headin v.SetAttribute(attr.Name, []byte(fmt.Sprintf("%v", attr.Value))) } } - txt := v.Text(reader.Source()) + txt := v.Lines().Value(reader.Source()) header := markup.Header{ Text: util.UnsafeBytesToString(txt), Level: v.Level, diff --git a/modules/markup/markdown/util/text.go b/modules/markup/markdown/util/text.go new file mode 100644 index 0000000000..8a42e5835b --- /dev/null +++ b/modules/markup/markdown/util/text.go @@ -0,0 +1,26 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package util + +import ( + "bytes" + + "github.com/yuin/goldmark/ast" +) + +func textOfChildren(n ast.Node, src []byte, b *bytes.Buffer) { + for c := n.FirstChild(); c != nil; c = c.NextSibling() { + if t, ok := c.(*ast.Text); ok { + b.Write(t.Value(src)) + } else { + textOfChildren(c, src, b) + } + } +} + +func Text(n ast.Node, src []byte) []byte { + var b bytes.Buffer + textOfChildren(n, src, &b) + return b.Bytes() +} diff --git a/modules/markup/mdstripper/mdstripper.go b/modules/markup/mdstripper/mdstripper.go index 2a69d95224..aefa41d3d2 100644 --- a/modules/markup/mdstripper/mdstripper.go +++ b/modules/markup/mdstripper/mdstripper.go @@ -46,7 +46,7 @@ func (r *stripRenderer) Render(w io.Writer, source []byte, doc ast.Node) error { coalesce := prevSibIsText r.processString( w, - v.Text(source), + v.Value(source), coalesce) if v.SoftLineBreak() { r.doubleSpace(w)