Skip to content

Commit 195b18d

Browse files
committed
fix
1 parent a0c0cb3 commit 195b18d

File tree

13 files changed

+68
-67
lines changed

13 files changed

+68
-67
lines changed

models/repo/repo.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"html/template"
10+
"maps"
1011
"net"
1112
"net/url"
1213
"path/filepath"
@@ -165,10 +166,10 @@ type Repository struct {
165166

166167
Status RepositoryStatus `xorm:"NOT NULL DEFAULT 0"`
167168

168-
RenderingMetas map[string]string `xorm:"-"`
169-
DocumentRenderingMetas map[string]string `xorm:"-"`
170-
Units []*RepoUnit `xorm:"-"`
171-
PrimaryLanguage *LanguageStat `xorm:"-"`
169+
commonRenderingMetas map[string]string `xorm:"-"`
170+
171+
Units []*RepoUnit `xorm:"-"`
172+
PrimaryLanguage *LanguageStat `xorm:"-"`
172173

173174
IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
174175
ForkID int64 `xorm:"INDEX"`
@@ -473,9 +474,8 @@ func (repo *Repository) MustOwner(ctx context.Context) *user_model.User {
473474
return repo.Owner
474475
}
475476

476-
// ComposeMetas composes a map of metas for properly rendering issue links and external issue trackers.
477-
func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
478-
if len(repo.RenderingMetas) == 0 {
477+
func (repo *Repository) composeCommonMetas(ctx context.Context) map[string]string {
478+
if len(repo.commonRenderingMetas) == 0 {
479479
metas := map[string]string{
480480
"user": repo.OwnerName,
481481
"repo": repo.Name,
@@ -508,21 +508,32 @@ func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
508508
metas["org"] = strings.ToLower(repo.OwnerName)
509509
}
510510

511-
repo.RenderingMetas = metas
511+
repo.commonRenderingMetas = metas
512512
}
513-
return repo.RenderingMetas
513+
return repo.commonRenderingMetas
514+
}
515+
516+
// ComposeMetas composes a map of metas for properly rendering comments or comment-like contents (commit message)
517+
func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
518+
metas := maps.Clone(repo.composeCommonMetas(ctx))
519+
metas["markdownLineBreakStyle"] = "comment"
520+
metas["markdownAllowShortIssuePattern"] = "true"
521+
return metas
514522
}
515523

516-
// ComposeDocumentMetas composes a map of metas for properly rendering documents
524+
// ComposeUncycloMetas composes a map of metas for properly rendering wikis
525+
func (repo *Repository) ComposeUncycloMetas(ctx context.Context) map[string]string {
526+
metas := maps.Clone(repo.composeCommonMetas(ctx))
527+
metas["markdownLineBreakStyle"] = "document"
528+
metas["markdownAllowShortIssuePattern"] = "true"
529+
return metas
530+
}
531+
532+
// ComposeDocumentMetas composes a map of metas for properly rendering documents (repo files)
517533
func (repo *Repository) ComposeDocumentMetas(ctx context.Context) map[string]string {
518-
if len(repo.DocumentRenderingMetas) == 0 {
519-
metas := map[string]string{}
520-
for k, v := range repo.ComposeMetas(ctx) {
521-
metas[k] = v
522-
}
523-
repo.DocumentRenderingMetas = metas
524-
}
525-
return repo.DocumentRenderingMetas
534+
metas := maps.Clone(repo.composeCommonMetas(ctx))
535+
metas["markdownLineBreakStyle"] = "document"
536+
return metas
526537
}
527538

528539
// GetBaseRepo populates repo.BaseRepo for a fork repository and

models/repo/repo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestMetas(t *testing.T) {
9999

100100
testSuccess := func(expectedStyle string) {
101101
repo.Units = []*repo_model.RepoUnit{&externalTracker}
102-
repo.RenderingMetas = nil
102+
repo.commonRenderingMetas = nil
103103
metas := repo.ComposeMetas(db.DefaultContext)
104104
assert.Equal(t, expectedStyle, metas["style"])
105105
assert.Equal(t, "testRepo", metas["repo"])

modules/markup/html.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,8 @@ func RenderIssueTitle(
249249
ctx *RenderContext,
250250
title string,
251251
) (string, error) {
252+
// do not render other issue/commit links in an issue's title - which in most cases is already a link.
252253
return renderProcessString(ctx, []processor{
253-
issueIndexPatternProcessor,
254-
commitCrossReferencePatternProcessor,
255-
hashCurrentPatternProcessor,
256254
emojiShortCodeProcessor,
257255
emojiProcessor,
258256
}, title)

modules/markup/html_internal_test.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
124124
}
125125
expectedNil := fmt.Sprintf(expectedFmt, links...)
126126
testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{
127-
Ctx: git.DefaultContext,
128-
Metas: localMetas,
129-
ContentMode: RenderContentAsComment,
127+
Ctx: git.DefaultContext,
128+
Metas: localMetas,
130129
})
131130

132131
class := "ref-issue"
@@ -139,9 +138,8 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
139138
}
140139
expectedNum := fmt.Sprintf(expectedFmt, links...)
141140
testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{
142-
Ctx: git.DefaultContext,
143-
Metas: numericMetas,
144-
ContentMode: RenderContentAsComment,
141+
Ctx: git.DefaultContext,
142+
Metas: numericMetas,
145143
})
146144
}
147145

@@ -270,19 +268,12 @@ func TestRender_IssueIndexPattern_Document(t *testing.T) {
270268
"repo": "someRepo",
271269
"style": IssueNameStyleNumeric,
272270
}
273-
274-
testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
275-
Ctx: git.DefaultContext,
276-
Metas: metas,
277-
})
278-
testRenderIssueIndexPattern(t, "#1312", "#1312", &RenderContext{
271+
actual, err := RenderIssueTitle(&RenderContext{
279272
Ctx: git.DefaultContext,
280273
Metas: metas,
281-
})
282-
testRenderIssueIndexPattern(t, "!1", "!1", &RenderContext{
283-
Ctx: git.DefaultContext,
284-
Metas: metas,
285-
})
274+
}, "#1")
275+
assert.NoError(t, err)
276+
assert.Equal(t, "#1", actual)
286277
}
287278

288279
func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) {

modules/markup/html_issue.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
6767
return
6868
}
6969

70-
// crossLinkOnly if not comment and not wiki
71-
crossLinkOnly := ctx.ContentMode != RenderContentAsTitle && ctx.ContentMode != RenderContentAsComment && ctx.ContentMode != RenderContentAsUncyclo
70+
// crossLinkOnly: do not parse "#123", only parse "owner/repo#123"
71+
// if there is no repo in the context, then the "#123" format can't be parsed
72+
// old logic: crossLinkOnly := ctx.Metas["mode"] == "document" && !ctx.IsUncyclo
73+
crossLinkOnly := ctx.Metas["markdownAllowShortIssuePattern"] != "true"
7274

7375
var (
7476
found bool

modules/markup/html_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func TestRender_RelativeMedias(t *testing.T) {
529529
Ctx: git.DefaultContext,
530530
Links: links,
531531
Metas: localMetas,
532-
ContentMode: util.Iif(isUncyclo, markup.RenderContentAsUncyclo, markup.RenderContentAsComment),
532+
ContentMode: util.Iif(isUncyclo, markup.RenderContentAsUncyclo, ""),
533533
}, input)
534534
assert.NoError(t, err)
535535
return strings.TrimSpace(string(buffer))

modules/markup/markdown/goldmark.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
7575
// TODO: this was a quite unclear part, old code: `if metas["mode"] != "document" { use comment link break setting }`
7676
// many places render non-comment contents with no mode=document, then these contents also use comment's hard line break setting
7777
// especially in many tests.
78+
markdownLineBreakStyle := ctx.Metas["markdownLineBreakStyle"]
7879
if markup.RenderBehaviorForTesting.ForceHardLineBreak {
7980
v.SetHardLineBreak(true)
80-
} else if ctx.ContentMode == markup.RenderContentAsComment {
81+
} else if markdownLineBreakStyle == "comment" {
8182
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
82-
} else {
83+
} else if markdownLineBreakStyle == "document" {
8384
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)
8485
}
8586
}

modules/markup/markdown/markdown_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ space</p>
10011001
result, err := markdown.RenderString(&markup.RenderContext{
10021002
Ctx: context.Background(),
10031003
Links: c.Links,
1004-
ContentMode: util.Iif(c.IsUncyclo, markup.RenderContentAsUncyclo, markup.RenderContentAsDefault),
1004+
ContentMode: util.Iif(c.IsUncyclo, markup.RenderContentAsUncyclo, ""),
10051005
}, input)
10061006
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
10071007
assert.Equal(t, c.Expected, string(result), "Unexpected result in testcase %v", i)

modules/markup/orgmode/orgmode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestRender_StandardLinks(t *testing.T) {
2727
Base: "/relative-path",
2828
BranchPath: "branch/main",
2929
},
30-
ContentMode: util.Iif(isUncyclo, markup.RenderContentAsUncyclo, markup.RenderContentAsDefault),
30+
ContentMode: util.Iif(isUncyclo, markup.RenderContentAsUncyclo, ""),
3131
}, input)
3232
assert.NoError(t, err)
3333
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))

modules/markup/render.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ const (
2929

3030
type RenderContentMode string
3131

32-
const (
33-
RenderContentAsDefault RenderContentMode = "" // empty means "default", no special handling, maybe just a simple "document"
34-
RenderContentAsComment RenderContentMode = "comment"
35-
RenderContentAsTitle RenderContentMode = "title"
36-
RenderContentAsUncyclo RenderContentMode = "wiki"
37-
)
32+
const RenderContentAsUncyclo RenderContentMode = "wiki"
3833

3934
var RenderBehaviorForTesting struct {
4035
// Markdown line break rendering has 2 default behaviors:
@@ -59,12 +54,16 @@ type RenderContext struct {
5954
// for file mode, it could be left as empty, and will be detected by file extension in RelativePath
6055
MarkupType string
6156

62-
// what the content will be used for: eg: for comment or for wiki? or just render a file?
57+
// what the content will be used for: eg: for wiki?
6358
ContentMode RenderContentMode
6459

65-
Links Links // special link references for rendering, especially when there is a branch/tree path
66-
Metas map[string]string // user&repo, format&style&regexp (for external issue pattern), teams&org (for mention), BranchNameSubURL(for iframe&asciicast)
67-
DefaultLink string // TODO: need to figure out
60+
Links Links // special link references for rendering, especially when there is a branch/tree path
61+
62+
// user&repo, format&style&regexp (for external issue pattern), teams&org (for mention), BranchNameSubURL(for iframe&asciicast)
63+
// markdownLineBreakStyle(comment or document)
64+
Metas map[string]string
65+
66+
DefaultLink string // TODO: need to figure out
6867
GitRepo *git.Repository
6968
Repo gitrepo.Repository
7069
ShaExistCache map[string]bool

modules/markup/render_links.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
type Links struct {
1212
AbsolutePrefix bool // add absolute URL prefix to auto-resolved links like "#issue", but not for pre-provided links and medias
13-
Base string // base prefix for pre-provided links and medias (images, videos)
13+
Base string // base prefix for pre-provided links and medias (images, videos), usually it is the path to the repo
1414
BranchPath string // actually it is the ref path, eg: "branch/features/feat-12", "tag/v1.0"
1515
TreePath string // the dir of the file, eg: "doc" if the file "doc/CHANGE.md" is being rendered
1616
}

modules/templates/util_render.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ func (ut *RenderUtils) RenderCommitBody(msg string, metas map[string]string) tem
9494
}
9595

9696
renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
97-
Ctx: ut.ctx,
98-
Metas: metas,
99-
ContentMode: markup.RenderContentAsComment,
97+
Ctx: ut.ctx,
98+
Metas: metas,
10099
}, template.HTMLEscapeString(msgLine))
101100
if err != nil {
102101
log.Error("RenderCommitMessage: %v", err)
@@ -117,9 +116,8 @@ func renderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
117116
// RenderIssueTitle renders issue/pull title with defined post processors
118117
func (ut *RenderUtils) RenderIssueTitle(text string, metas map[string]string) template.HTML {
119118
renderedText, err := markup.RenderIssueTitle(&markup.RenderContext{
120-
Ctx: ut.ctx,
121-
ContentMode: markup.RenderContentAsTitle,
122-
Metas: metas,
119+
Ctx: ut.ctx,
120+
Metas: metas,
123121
}, template.HTMLEscapeString(text))
124122
if err != nil {
125123
log.Error("RenderIssueTitle: %v", err)

routers/common/markup.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPa
4747
switch mode {
4848
case "gfm": // legacy mode, do nothing
4949
case "comment":
50-
renderCtx.ContentMode = markup.RenderContentAsComment
5150
case "wiki":
5251
renderCtx.ContentMode = markup.RenderContentAsUncyclo
5352
case "file":
@@ -74,10 +73,12 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPa
7473

7574
if repo != nil && repo.Repository != nil {
7675
renderCtx.Repo = repo.Repository
77-
if renderCtx.ContentMode == markup.RenderContentAsComment {
78-
renderCtx.Metas = repo.Repository.ComposeMetas(ctx)
79-
} else {
76+
if filePath != "" {
77+
renderCtx.Metas = repo.Repository.ComposeDocumentMetas(ctx)
78+
} else if renderCtx.ContentMode == markup.RenderContentAsUncyclo {
8079
renderCtx.Metas = repo.Repository.ComposeDocumentMetas(ctx)
80+
} else {
81+
renderCtx.Metas = repo.Repository.ComposeMetas(ctx)
8182
}
8283
}
8384
if err := markup.Render(renderCtx, strings.NewReader(text), ctx.Resp); err != nil {

0 commit comments

Comments
 (0)