From b9cd6fb70370b97496e03c61be35e0d3ceedc52d Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 15 Oct 2022 21:24:41 +0300 Subject: 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 Co-authored-by: Gusted Co-authored-by: wxiaoguang Co-authored-by: 6543 <6543@obermui.de> --- modules/templates/helper.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'modules') 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 "" block. +// Intended for issue and PR titles, these containers should have styles for "" elements +func RenderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML { + htmlWithCodeTags := codeMatcher.ReplaceAllString(string(htmlEscapedTextToRender), "$1") // replace with HTML 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{ -- cgit v1.2.3