summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorYarden Shoham <hrsi88@gmail.com>2022-10-15 21:24:41 +0300
committerGitHub <noreply@github.com>2022-10-16 02:24:41 +0800
commitb9cd6fb70370b97496e03c61be35e0d3ceedc52d (patch)
tree954ec4f87da6f2de8405f2395cc2283ea49b722e /modules
parent671c609c465f02d7ac5023bb8491694a9f4768f5 (diff)
downloadgitea-b9cd6fb70370b97496e03c61be35e0d3ceedc52d.tar.gz
gitea-b9cd6fb70370b97496e03c61be35e0d3ceedc52d.zip
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 <hrsi88@gmail.com> Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'modules')
-rw-r--r--modules/templates/helper.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index a8e4075248..e10beae1d9 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -161,6 +161,7 @@ func NewFuncMap() []template.FuncMap {
"RenderCommitMessageLink": RenderCommitMessageLink,
"RenderCommitMessageLinkSubject": RenderCommitMessageLinkSubject,
"RenderCommitBody": RenderCommitBody,
+ "RenderCodeBlock": RenderCodeBlock,
"RenderIssueTitle": RenderIssueTitle,
"RenderEmoji": RenderEmoji,
"RenderEmojiPlain": emoji.ReplaceAliases,
@@ -795,6 +796,16 @@ func RenderCommitBody(ctx context.Context, msg, urlPrefix string, metas map[stri
return template.HTML(renderedMessage)
}
+// Match text that is between back ticks.
+var codeMatcher = regexp.MustCompile("`([^`]+)`")
+
+// RenderCodeBlock renders "`…`" as highlighted "<code>" block.
+// Intended for issue and PR titles, these containers should have styles for "<code>" elements
+func RenderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
+ htmlWithCodeTags := codeMatcher.ReplaceAllString(string(htmlEscapedTextToRender), "<code>$1</code>") // replace with HTML <code> tags
+ return template.HTML(htmlWithCodeTags)
+}
+
// RenderIssueTitle renders issue/pull title with defined post processors
func RenderIssueTitle(ctx context.Context, text, urlPrefix string, metas map[string]string) template.HTML {
renderedText, err := markup.RenderIssueTitle(&markup.RenderContext{