Skip to content

Commit b9cd6fb

Browse files
yardenshohamGustedwxiaoguang6543
authored
Add code highlighting in issue titles (#21432)
This changes the rendering logic of issue titles. If a substring in an issue title is enclosed with a pair of backticks, it'll be rendered with a monospace font (HTML `code` tag). * Closes #20887 Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: Gusted <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 671c609 commit b9cd6fb

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed

modules/templates/helper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func NewFuncMap() []template.FuncMap {
161161
"RenderCommitMessageLink": RenderCommitMessageLink,
162162
"RenderCommitMessageLinkSubject": RenderCommitMessageLinkSubject,
163163
"RenderCommitBody": RenderCommitBody,
164+
"RenderCodeBlock": RenderCodeBlock,
164165
"RenderIssueTitle": RenderIssueTitle,
165166
"RenderEmoji": RenderEmoji,
166167
"RenderEmojiPlain": emoji.ReplaceAliases,
@@ -795,6 +796,16 @@ func RenderCommitBody(ctx context.Context, msg, urlPrefix string, metas map[stri
795796
return template.HTML(renderedMessage)
796797
}
797798

799+
// Match text that is between back ticks.
800+
var codeMatcher = regexp.MustCompile("`([^`]+)`")
801+
802+
// RenderCodeBlock renders "`…`" as highlighted "<code>" block.
803+
// Intended for issue and PR titles, these containers should have styles for "<code>" elements
804+
func RenderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
805+
htmlWithCodeTags := codeMatcher.ReplaceAllString(string(htmlEscapedTextToRender), "<code>$1</code>") // replace with HTML <code> tags
806+
return template.HTML(htmlWithCodeTags)
807+
}
808+
798809
// RenderIssueTitle renders issue/pull title with defined post processors
799810
func RenderIssueTitle(ctx context.Context, text, urlPrefix string, metas map[string]string) template.HTML {
800811
renderedText, err := markup.RenderIssueTitle(&markup.RenderContext{

templates/repo/issue/view_title.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</div>
77
{{end}}
88
<h1>
9-
<span id="issue-title">{{RenderIssueTitle $.Context .Issue.Title $.RepoLink $.Repository.ComposeMetas}}</span>
9+
<span id="issue-title">{{RenderIssueTitle $.Context .Issue.Title $.RepoLink $.Repository.ComposeMetas | RenderCodeBlock}}</span>
1010
<span class="index">#{{.Issue.Index}}</span>
1111
<div id="edit-title-input" class="ui input" style="display: none">
1212
<input value="{{.Issue.Title}}" maxlength="255" autocomplete="off">

templates/shared/issuelist.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</div>
3636
<div class="issue-item-main f1 fc df">
3737
<div class="issue-item-top-row">
38-
<a class="title tdn" href="{{if .HTMLURL}}{{.HTMLURL}}{{else}}{{$.Link}}/{{.Index}}{{end}}">{{RenderEmoji .Title}}</a>
38+
<a class="title tdn issue-title" href="{{if .HTMLURL}}{{.HTMLURL}}{{else}}{{$.Link}}/{{.Index}}{{end}}">{{RenderEmoji .Title | RenderCodeBlock}}</a>
3939
{{if .IsPull}}
4040
{{if (index $.CommitStatuses .PullRequest.ID)}}
4141
{{template "repo/commit_statuses" dict "Status" (index $.CommitLastStatus .PullRequest.ID) "Statuses" (index $.CommitStatuses .PullRequest.ID) "root" $}}

templates/user/dashboard/feeds.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@
9999
</ul>
100100
</div>
101101
{{else if eq .GetOpType 6}}
102-
<span class="text truncate issue title">{{index .GetIssueInfos 1 | RenderEmoji}}</span>
102+
<span class="text truncate issue title">{{index .GetIssueInfos 1 | RenderEmoji | RenderCodeBlock}}</span>
103103
{{else if eq .GetOpType 7}}
104-
<span class="text truncate issue title">{{index .GetIssueInfos 1 | RenderEmoji}}</span>
104+
<span class="text truncate issue title">{{index .GetIssueInfos 1 | RenderEmoji | RenderCodeBlock}}</span>
105105
{{else if or (eq .GetOpType 10) (eq .GetOpType 21) (eq .GetOpType 22) (eq .GetOpType 23)}}
106-
<a href="{{.GetCommentLink}}" class="text truncate issue title">{{.GetIssueTitle | RenderEmoji}}</a>
106+
<a href="{{.GetCommentLink}}" class="text truncate issue title">{{.GetIssueTitle | RenderEmoji | RenderCodeBlock}}</a>
107107
{{$comment := index .GetIssueInfos 1}}
108108
{{if gt (len $comment) 0}}<p class="text light grey">{{$comment | RenderEmoji}}</p>{{end}}
109109
{{else if eq .GetOpType 11}}
110110
<p class="text light grey">{{index .GetIssueInfos 1}}</p>
111111
{{else if or (eq .GetOpType 12) (eq .GetOpType 13) (eq .GetOpType 14) (eq .GetOpType 15)}}
112-
<span class="text truncate issue title">{{.GetIssueTitle | RenderEmoji}}</span>
112+
<span class="text truncate issue title">{{.GetIssueTitle | RenderEmoji | RenderCodeBlock}}</span>
113113
{{else if eq .GetOpType 25}}
114114
<p class="text light grey">{{$.locale.Tr "action.review_dismissed_reason"}}</p>
115115
<p class="text light grey">{{index .GetIssueInfos 2 | RenderEmoji}}</p>

web_src/less/_base.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ a.commit-statuses-trigger {
332332
&:extend(.unselectable);
333333
}
334334

335+
.issue-title code {
336+
padding: 2px 4px;
337+
border-radius: 6px;
338+
background-color: var(--color-markup-code-block);
339+
}
335340
/* try to match button with no icons in height */
336341
.icon-button {
337342
padding-top: 7.42px !important;

web_src/less/_dashboard.less

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@
141141
}
142142

143143
code {
144-
padding: 1px;
145-
font-size: 85%;
146-
background-color: rgba(0, 0, 0, .04);
144+
padding: 2px 4px;
147145
border-radius: 3px;
146+
background-color: var(--color-markup-code-block);
148147
word-break: break-all;
149148
}
150149

0 commit comments

Comments
 (0)