diff --git a/templates/repo/issue/view_title.tmpl b/templates/repo/issue/view_title.tmpl
index db21adae2c..f60d958a1c 100644
--- a/templates/repo/issue/view_title.tmpl
+++ b/templates/repo/issue/view_title.tmpl
@@ -7,8 +7,7 @@
{{$canEditIssueTitle := and (or .HasIssuesOrPullsWritePermission .IsIssuePoster) (not .Repository.IsArchived)}}
- {{RenderIssueTitle $.Context .Issue.Title ($.Repository.ComposeMetas ctx) | RenderCodeBlock}}
- #{{.Issue.Index}}
+ {{RenderIssueTitle $.Context .Issue.Title ($.Repository.ComposeMetas ctx)}} #{{.Issue.Index}}
{{if $canEditIssueTitle}}
diff --git a/templates/repo/pulse.tmpl b/templates/repo/pulse.tmpl
index 0494883a85..3554cf6a19 100644
--- a/templates/repo/pulse.tmpl
+++ b/templates/repo/pulse.tmpl
@@ -153,7 +153,7 @@
{{range .Activity.MergedPRs}}
{{ctx.Locale.Tr "repo.activity.merged_prs_label"}}
- #{{.Index}} {{.Issue.Title | RenderEmoji $.Context | RenderCodeBlock}}
+ #{{.Index}} {{RenderRefIssueTitle $.Context .Issue.Title}}
{{TimeSinceUnix .MergedUnix ctx.Locale}}
{{end}}
@@ -172,7 +172,7 @@
{{range .Activity.OpenedPRs}}
{{ctx.Locale.Tr "repo.activity.opened_prs_label"}}
- #{{.Index}} {{.Issue.Title | RenderEmoji $.Context | RenderCodeBlock}}
+ #{{.Index}} {{RenderRefIssueTitle $.Context .Issue.Title}}
{{TimeSinceUnix .Issue.CreatedUnix ctx.Locale}}
{{end}}
@@ -191,7 +191,7 @@
{{range .Activity.ClosedIssues}}
{{ctx.Locale.Tr "repo.activity.closed_issue_label"}}
- #{{.Index}} {{.Title | RenderEmoji $.Context | RenderCodeBlock}}
+ #{{.Index}} {{RenderRefIssueTitle $.Context .Title}}
{{TimeSinceUnix .ClosedUnix ctx.Locale}}
{{end}}
@@ -210,7 +210,7 @@
{{range .Activity.OpenedIssues}}
{{ctx.Locale.Tr "repo.activity.new_issue_label"}}
- #{{.Index}} {{.Title | RenderEmoji $.Context | RenderCodeBlock}}
+ #{{.Index}} {{RenderRefIssueTitle $.Context .Title}}
{{TimeSinceUnix .CreatedUnix ctx.Locale}}
{{end}}
@@ -228,9 +228,9 @@
{{ctx.Locale.Tr "repo.activity.unresolved_conv_label"}}
#{{.Index}}
{{if .IsPull}}
-
{{.Title | RenderEmoji $.Context | RenderCodeBlock}}
+
{{RenderRefIssueTitle $.Context .Title}}
{{else}}
-
{{.Title | RenderEmoji $.Context | RenderCodeBlock}}
+
{{RenderRefIssueTitle $.Context .Title}}
{{end}}
{{TimeSinceUnix .UpdatedUnix ctx.Locale}}
diff --git a/templates/user/notification/notification_div.tmpl b/templates/user/notification/notification_div.tmpl
index 4329ffdb7a..5c27ba8b41 100644
--- a/templates/user/notification/notification_div.tmpl
+++ b/templates/user/notification/notification_div.tmpl
@@ -58,7 +58,7 @@
{{if .Issue}}
- {{.Issue.Title | RenderEmoji $.Context | RenderCodeBlock}}
+ {{RenderRefIssueTitle $.Context .Issue.Title}}
{{else}}
{{.Repository.FullName}}
{{end}}
diff --git a/tests/integration/repo_issue_title_test.go b/tests/integration/repo_issue_title_test.go
new file mode 100644
index 0000000000..5199be91bc
--- /dev/null
+++ b/tests/integration/repo_issue_title_test.go
@@ -0,0 +1,162 @@
+// Copyright 2024 The Forgejo Authors. All rights reserved.
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package integration
+
+import (
+ "net/http"
+ "net/url"
+ "strings"
+ "testing"
+ "time"
+
+ "code.gitea.io/gitea/models/db"
+ issues_model "code.gitea.io/gitea/models/issues"
+ repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/git"
+ issue_service "code.gitea.io/gitea/services/issue"
+ pull_service "code.gitea.io/gitea/services/pull"
+ files_service "code.gitea.io/gitea/services/repository/files"
+ "code.gitea.io/gitea/tests"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestIssueTitles(t *testing.T) {
+ onGiteaRun(t, func(t *testing.T, u *url.URL) {
+ user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+ repo, _, f := tests.CreateDeclarativeRepo(t, user, "issue-titles", nil, nil, nil)
+ defer f()
+
+ session := loginUser(t, user.LoginName)
+
+ title := "Title :+1: `code`"
+ issue1 := createIssue(t, user, repo, title, "Test issue")
+ issue2 := createIssue(t, user, repo, title, "Ref #1")
+
+ titleHTML := []string{
+ "Title",
+ `👍`,
+ `code
`,
+ }
+
+ t.Run("Main issue title", func(t *testing.T) {
+ defer tests.PrintCurrentTest(t)()
+
+ html := extractHTML(t, session, issue1, "div.issue-title-header > * > h1")
+ assertContainsAll(t, titleHTML, html)
+ })
+
+ t.Run("Referenced issue comment", func(t *testing.T) {
+ defer tests.PrintCurrentTest(t)()
+
+ html := extractHTML(t, session, issue1, "div.timeline > div.timeline-item:nth-child(3) > div.detail > * > a")
+ assertContainsAll(t, titleHTML, html)
+ })
+
+ t.Run("Dependent issue comment", func(t *testing.T) {
+ defer tests.PrintCurrentTest(t)()
+
+ err := issues_model.CreateIssueDependency(db.DefaultContext, user, issue1, issue2)
+ require.NoError(t, err)
+
+ html := extractHTML(t, session, issue1, "div.timeline > div:nth-child(3) > div.detail > * > a")
+ assertContainsAll(t, titleHTML, html)
+ })
+
+ t.Run("Dependent issue sidebar", func(t *testing.T) {
+ defer tests.PrintCurrentTest(t)()
+
+ html := extractHTML(t, session, issue1, "div.item.dependency > * > a.title")
+ assertContainsAll(t, titleHTML, html)
+ })
+
+ t.Run("Referenced pull comment", func(t *testing.T) {
+ _, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user, &files_service.ChangeRepoFilesOptions{
+ Files: []*files_service.ChangeRepoFile{
+ {
+ Operation: "update",
+ TreePath: "README.md",
+ ContentReader: strings.NewReader("Update README"),
+ },
+ },
+ Message: "Update README",
+ OldBranch: "main",
+ NewBranch: "branch",
+ Author: &files_service.IdentityOptions{
+ Name: user.Name,
+ Email: user.Email,
+ },
+ Committer: &files_service.IdentityOptions{
+ Name: user.Name,
+ Email: user.Email,
+ },
+ Dates: &files_service.CommitDateOptions{
+ Author: time.Now(),
+ Committer: time.Now(),
+ },
+ })
+
+ require.NoError(t, err)
+
+ pullIssue := &issues_model.Issue{
+ RepoID: repo.ID,
+ Title: title,
+ Content: "Closes #1",
+ PosterID: user.ID,
+ Poster: user,
+ IsPull: true,
+ }
+
+ pullRequest := &issues_model.PullRequest{
+ HeadRepoID: repo.ID,
+ BaseRepoID: repo.ID,
+ HeadBranch: "branch",
+ BaseBranch: "main",
+ HeadRepo: repo,
+ BaseRepo: repo,
+ Type: issues_model.PullRequestGitea,
+ }
+
+ err = pull_service.NewPullRequest(git.DefaultContext, repo, pullIssue, nil, nil, pullRequest, nil)
+ require.NoError(t, err)
+
+ html := extractHTML(t, session, issue1, "div.timeline > div:nth-child(4) > div.detail > * > a")
+ assertContainsAll(t, titleHTML, html)
+ })
+ })
+}
+
+func createIssue(t *testing.T, user *user_model.User, repo *repo_model.Repository, title, content string) *issues_model.Issue {
+ issue := &issues_model.Issue{
+ RepoID: repo.ID,
+ Title: title,
+ Content: content,
+ PosterID: user.ID,
+ Poster: user,
+ }
+
+ err := issue_service.NewIssue(db.DefaultContext, repo, issue, nil, nil, nil)
+ require.NoError(t, err)
+
+ return issue
+}
+
+func extractHTML(t *testing.T, session *TestSession, issue *issues_model.Issue, query string) string {
+ req := NewRequest(t, "GET", issue.HTMLURL())
+ resp := session.MakeRequest(t, req, http.StatusOK)
+ doc := NewHTMLParser(t, resp.Body)
+ res, err := doc.doc.Find(query).Html()
+ require.NoError(t, err)
+
+ return res
+}
+
+func assertContainsAll(t *testing.T, expected []string, actual string) {
+ for i := range expected {
+ assert.Contains(t, actual, expected[i])
+ }
+}
diff --git a/vitest.config.js b/vitest.config.js
index ea0fafeee8..776247c5ee 100644
--- a/vitest.config.js
+++ b/vitest.config.js
@@ -1,6 +1,7 @@
import {defineConfig} from 'vitest/config';
import vuePlugin from '@vitejs/plugin-vue';
import {stringPlugin} from 'vite-string-plugin';
+import {resolve} from 'node:path';
export default defineConfig({
test: {
@@ -13,6 +14,9 @@ export default defineConfig({
passWithNoTests: true,
globals: true,
watch: false,
+ alias: {
+ 'monaco-editor': resolve(import.meta.dirname, '/node_modules/monaco-editor/esm/vs/editor/editor.api'),
+ },
},
plugins: [
stringPlugin(),
diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js
index ff8faa94f6..feba449c16 100644
--- a/web_src/js/features/repo-issue.js
+++ b/web_src/js/features/repo-issue.js
@@ -8,6 +8,7 @@ import {toAbsoluteUrl} from '../utils.js';
import {initDropzone} from './common-global.js';
import {POST, GET} from '../modules/fetch.js';
import {showErrorToast} from '../modules/toast.js';
+import {emojiHTML} from './emoji.js';
const {appSubUrl} = window.config;
@@ -124,7 +125,7 @@ export function initRepoIssueSidebarList() {
return;
}
filteredResponse.results.push({
- name: `#${issue.number} ${htmlEscape(issue.title)
+ name: `#${issue.number} ${issueTitleHTML(htmlEscape(issue.title))
}${htmlEscape(issue.repository.full_name)}
`,
value: issue.id,
});
@@ -731,3 +732,9 @@ export function initArchivedLabelHandler() {
toggleElem(label, label.classList.contains('checked'));
}
}
+
+// Render the issue's title. It converts emojis and code blocks syntax into their respective HTML equivalent.
+export function issueTitleHTML(title) {
+ return title.replaceAll(/:[-+\w]+:/g, (emoji) => emojiHTML(emoji.substring(1, emoji.length - 1)))
+ .replaceAll(/`[^`]+`/g, (code) => `${code.substring(1, code.length - 1)}
`);
+}
diff --git a/web_src/js/features/repo-issue.test.js b/web_src/js/features/repo-issue.test.js
new file mode 100644
index 0000000000..8c9734b0c6
--- /dev/null
+++ b/web_src/js/features/repo-issue.test.js
@@ -0,0 +1,24 @@
+import {vi} from 'vitest';
+
+import {issueTitleHTML} from './repo-issue.js';
+
+// monaco-editor does not have any exports fields, which trips up vitest
+vi.mock('./comp/ComboMarkdownEditor.js', () => ({}));
+// jQuery is missing
+vi.mock('./common-global.js', () => ({}));
+
+test('Convert issue title to html', () => {
+ expect(issueTitleHTML('')).toEqual('');
+ expect(issueTitleHTML('issue title')).toEqual('issue title');
+
+ const expected_thumbs_up = `👍`;
+ expect(issueTitleHTML(':+1:')).toEqual(expected_thumbs_up);
+ expect(issueTitleHTML(':invalid emoji:')).toEqual(':invalid emoji:');
+
+ const expected_code_block = `code
`;
+ expect(issueTitleHTML('`code`')).toEqual(expected_code_block);
+ expect(issueTitleHTML('`invalid code')).toEqual('`invalid code');
+ expect(issueTitleHTML('invalid code`')).toEqual('invalid code`');
+
+ expect(issueTitleHTML('issue title :+1: `code`')).toEqual(`issue title ${expected_thumbs_up} ${expected_code_block}`);
+});