Skip to content

Commit b2e9894

Browse files
zeripathtechknowlogick
authored andcommitted
Fix reported issue in repo description (#6306)
1 parent 663874e commit b2e9894

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

models/repo.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,10 +722,12 @@ var (
722722

723723
// DescriptionHTML does special handles to description and return HTML string.
724724
func (repo *Repository) DescriptionHTML() template.HTML {
725-
sanitize := func(s string) string {
726-
return fmt.Sprintf(`<a href="%[1]s" target="_blank" rel="noopener noreferrer">%[1]s</a>`, s)
725+
desc, err := markup.RenderDescriptionHTML([]byte(repo.Description), repo.HTMLURL(), repo.ComposeMetas())
726+
if err != nil {
727+
log.Error(4, "Failed to render description for %s (ID: %d): %v", repo.Name, repo.ID, err)
728+
return template.HTML(markup.Sanitize(repo.Description))
727729
}
728-
return template.HTML(descPattern.ReplaceAllStringFunc(markup.Sanitize(repo.Description), sanitize))
730+
return template.HTML(markup.Sanitize(string(desc)))
729731
}
730732

731733
// LocalCopyPath returns the local repository copy path.

modules/markup/html.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,23 @@ func RenderCommitMessage(
227227
return ctx.postProcess(rawHTML)
228228
}
229229

230+
// RenderDescriptionHTML will use similar logic as PostProcess, but will
231+
// use a single special linkProcessor.
232+
func RenderDescriptionHTML(
233+
rawHTML []byte,
234+
urlPrefix string,
235+
metas map[string]string,
236+
) ([]byte, error) {
237+
ctx := &postProcessCtx{
238+
metas: metas,
239+
urlPrefix: urlPrefix,
240+
procs: []processor{
241+
descriptionLinkProcessor,
242+
},
243+
}
244+
return ctx.postProcess(rawHTML)
245+
}
246+
230247
var byteBodyTag = []byte("<body>")
231248
var byteBodyTagClosing = []byte("</body>")
232249

@@ -658,3 +675,34 @@ func genDefaultLinkProcessor(defaultLink string) processor {
658675
node.FirstChild, node.LastChild = ch, ch
659676
}
660677
}
678+
679+
// descriptionLinkProcessor creates links for DescriptionHTML
680+
func descriptionLinkProcessor(ctx *postProcessCtx, node *html.Node) {
681+
m := linkRegex.FindStringIndex(node.Data)
682+
if m == nil {
683+
return
684+
}
685+
uri := node.Data[m[0]:m[1]]
686+
replaceContent(node, m[0], m[1], createDescriptionLink(uri, uri))
687+
}
688+
689+
func createDescriptionLink(href, content string) *html.Node {
690+
textNode := &html.Node{
691+
Type: html.TextNode,
692+
Data: content,
693+
}
694+
linkNode := &html.Node{
695+
FirstChild: textNode,
696+
LastChild: textNode,
697+
Type: html.ElementNode,
698+
Data: "a",
699+
DataAtom: atom.A,
700+
Attr: []html.Attribute{
701+
{Key: "href", Val: href},
702+
{Key: "target", Val: "_blank"},
703+
{Key: "rel", Val: "noopener noreferrer"},
704+
},
705+
}
706+
textNode.Parent = linkNode
707+
return linkNode
708+
}

0 commit comments

Comments
 (0)