[GITEA] convert feed items' titles to plain text
Refs: https://codeberg.org/forgejo/forgejo/pulls/1595 (cherry picked from commit35b962e631
) (cherry picked from commit1004e35b84
) (cherry picked from commitaf51dd594d
) (cherry picked from commit7fb66b4556
)
This commit is contained in:
parent
8854d1d4dd
commit
0c409950f1
2 changed files with 49 additions and 1 deletions
|
@ -21,6 +21,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"github.com/gorilla/feeds"
|
"github.com/gorilla/feeds"
|
||||||
|
"github.com/jaytaylor/html2text"
|
||||||
)
|
)
|
||||||
|
|
||||||
func toBranchLink(ctx *context.Context, act *activities_model.Action) string {
|
func toBranchLink(ctx *context.Context, act *activities_model.Action) string {
|
||||||
|
@ -239,8 +240,15 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
|
||||||
content = desc
|
content = desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// It's a common practice for feed generators to use plain text titles.
|
||||||
|
// See https://codeberg.org/forgejo/forgejo/pulls/1595
|
||||||
|
plainTitle, err := html2text.FromString(title, html2text.Options{OmitLinks: true})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
items = append(items, &feeds.Item{
|
items = append(items, &feeds.Item{
|
||||||
Title: title,
|
Title: plainTitle,
|
||||||
Link: link,
|
Link: link,
|
||||||
Description: desc,
|
Description: desc,
|
||||||
Author: &feeds.Author{
|
Author: &feeds.Author{
|
||||||
|
|
40
tests/integration/api_feed_plain_text_titles_test.go
Normal file
40
tests/integration/api_feed_plain_text_titles_test.go
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFeedPlainTextTitles(t *testing.T) {
|
||||||
|
// This test verifies that items' titles in feeds are generated as plain text.
|
||||||
|
// See https://codeberg.org/forgejo/forgejo/pulls/1595
|
||||||
|
|
||||||
|
t.Run("Feed plain text titles", func(t *testing.T) {
|
||||||
|
t.Run("Atom", func(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
|
||||||
|
req := NewRequest(t, "GET", "/user2/repo1.atom")
|
||||||
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
data := resp.Body.String()
|
||||||
|
assert.Contains(t, data, "<title>the_1-user.with.all.allowedChars closed issue user2/repo1#4</title>")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("RSS", func(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
|
||||||
|
req := NewRequest(t, "GET", "/user2/repo1.rss")
|
||||||
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
data := resp.Body.String()
|
||||||
|
assert.Contains(t, data, "<title>the_1-user.with.all.allowedChars closed issue user2/repo1#4</title>")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue