Skip to content

Commit 00e9824

Browse files
committed
fix
1 parent ecbb03d commit 00e9824

File tree

18 files changed

+111
-106
lines changed

18 files changed

+111
-106
lines changed

models/repo/repo.go

Lines changed: 31 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,34 @@ 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+
// does wiki need the "teams" and "org" from common metas?
527+
metas := maps.Clone(repo.composeCommonMetas(ctx))
528+
metas["markdownLineBreakStyle"] = "document"
529+
metas["markdownAllowShortIssuePattern"] = "true"
530+
return metas
531+
}
532+
533+
// ComposeDocumentMetas composes a map of metas for properly rendering documents (repo files)
517534
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
535+
// does document(file) need the "teams" and "org" from common metas?
536+
metas := maps.Clone(repo.composeCommonMetas(ctx))
537+
metas["markdownLineBreakStyle"] = "document"
538+
return metas
526539
}
527540

528541
// 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: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ var localMetas = map[string]string{
6666
"repo": "test-repo",
6767
}
6868

69+
var localUncycloMetas = map[string]string{
70+
"user": "test-owner",
71+
"repo": "test-repo",
72+
"renderContentMode": "wiki",
73+
}
74+
6975
func TestRender_IssueIndexPattern(t *testing.T) {
7076
// numeric: render inputs without valid mentions
7177
test := func(s string) {
@@ -124,9 +130,8 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
124130
}
125131
expectedNil := fmt.Sprintf(expectedFmt, links...)
126132
testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{
127-
Ctx: git.DefaultContext,
128-
Metas: localMetas,
129-
ContentMode: RenderContentAsComment,
133+
Ctx: git.DefaultContext,
134+
Metas: localMetas,
130135
})
131136

132137
class := "ref-issue"
@@ -139,9 +144,8 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
139144
}
140145
expectedNum := fmt.Sprintf(expectedFmt, links...)
141146
testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{
142-
Ctx: git.DefaultContext,
143-
Metas: numericMetas,
144-
ContentMode: RenderContentAsComment,
147+
Ctx: git.DefaultContext,
148+
Metas: numericMetas,
145149
})
146150
}
147151

@@ -270,19 +274,12 @@ func TestRender_IssueIndexPattern_Document(t *testing.T) {
270274
"repo": "someRepo",
271275
"style": IssueNameStyleNumeric,
272276
}
273-
274-
testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
277+
actual, err := RenderIssueTitle(&RenderContext{
275278
Ctx: git.DefaultContext,
276279
Metas: metas,
277-
})
278-
testRenderIssueIndexPattern(t, "#1312", "#1312", &RenderContext{
279-
Ctx: git.DefaultContext,
280-
Metas: metas,
281-
})
282-
testRenderIssueIndexPattern(t, "!1", "!1", &RenderContext{
283-
Ctx: git.DefaultContext,
284-
Metas: metas,
285-
})
280+
}, "#1")
281+
assert.NoError(t, err)
282+
assert.Equal(t, "#1", actual)
286283
}
287284

288285
func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) {
@@ -318,8 +315,7 @@ func TestRender_AutoLink(t *testing.T) {
318315
Links: Links{
319316
Base: TestRepoURL,
320317
},
321-
Metas: localMetas,
322-
ContentMode: RenderContentAsUncyclo,
318+
Metas: localUncycloMetas,
323319
}, strings.NewReader(input), &buffer)
324320
assert.Equal(t, err, nil)
325321
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))

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_link.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ func ResolveLink(ctx *RenderContext, link, userContentAnchorPrefix string) (resu
2020
isAnchorFragment := link != "" && link[0] == '#'
2121
if !isAnchorFragment && !IsFullURLString(link) {
2222
linkBase := ctx.Links.Base
23-
if ctx.ContentMode == RenderContentAsUncyclo {
23+
if ctx.IsContentModeUncyclo() {
2424
// no need to check if the link should be resolved as a wiki link or a wiki raw link
25-
// just use wiki link here and it will be redirected to a wiki raw link if necessary
25+
// just use wiki link here, and it will be redirected to a wiki raw link if necessary
2626
linkBase = ctx.Links.UncycloLink()
2727
} else if ctx.Links.BranchPath != "" || ctx.Links.TreePath != "" {
2828
// if there is no BranchPath, then the link will be something like "/owner/repo/src/{the-file-path}"
@@ -147,7 +147,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
147147
}
148148
if image {
149149
if !absoluteLink {
150-
link = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsUncyclo), link)
150+
link = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsContentModeUncyclo()), link)
151151
}
152152
title := props["title"]
153153
if title == "" {

modules/markup/html_node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func visitNodeImg(ctx *RenderContext, img *html.Node) (next *html.Node) {
1717
}
1818

1919
if IsNonEmptyRelativePath(attr.Val) {
20-
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsUncyclo), attr.Val)
20+
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsContentModeUncyclo()), attr.Val)
2121

2222
// By default, the "<img>" tag should also be clickable,
2323
// because frontend use `<img>` to paste the re-scaled image into the markdown,
@@ -53,7 +53,7 @@ func visitNodeVideo(ctx *RenderContext, node *html.Node) (next *html.Node) {
5353
continue
5454
}
5555
if IsNonEmptyRelativePath(attr.Val) {
56-
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsUncyclo), attr.Val)
56+
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsContentModeUncyclo()), attr.Val)
5757
}
5858
attr.Val = camoHandleLink(attr.Val)
5959
node.Attr[i] = attr

modules/markup/html_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ var (
2727
"user": testRepoOwnerName,
2828
"repo": testRepoName,
2929
}
30+
localUncycloMetas = map[string]string{
31+
"user": testRepoOwnerName,
32+
"repo": testRepoName,
33+
"renderContentMode": "wiki",
34+
}
3035
)
3136

3237
type mockRepo struct {
@@ -413,8 +418,7 @@ func TestRender_ShortLinks(t *testing.T) {
413418
Links: markup.Links{
414419
Base: markup.TestRepoURL,
415420
},
416-
Metas: localMetas,
417-
ContentMode: markup.RenderContentAsUncyclo,
421+
Metas: localUncycloMetas,
418422
}, input)
419423
assert.NoError(t, err)
420424
assert.Equal(t, strings.TrimSpace(expectedUncyclo), strings.TrimSpace(string(buffer)))
@@ -526,10 +530,9 @@ func TestRender_ShortLinks(t *testing.T) {
526530
func TestRender_RelativeMedias(t *testing.T) {
527531
render := func(input string, isUncyclo bool, links markup.Links) string {
528532
buffer, err := markdown.RenderString(&markup.RenderContext{
529-
Ctx: git.DefaultContext,
530-
Links: links,
531-
Metas: localMetas,
532-
ContentMode: util.Iif(isUncyclo, markup.RenderContentAsUncyclo, markup.RenderContentAsComment),
533+
Ctx: git.DefaultContext,
534+
Links: links,
535+
Metas: util.Iif(isUncyclo, localMetas, localUncycloMetas),
533536
}, input)
534537
assert.NoError(t, err)
535538
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: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ var localMetas = map[string]string{
3737
"repo": testRepoName,
3838
}
3939

40+
var localUncycloMetas = map[string]string{
41+
"user": testRepoOwnerName,
42+
"repo": testRepoName,
43+
"renderContentMode": "wiki",
44+
}
45+
4046
type mockRepo struct {
4147
OwnerName string
4248
RepoName string
@@ -75,7 +81,7 @@ func TestRender_StandardLinks(t *testing.T) {
7581
Links: markup.Links{
7682
Base: FullURL,
7783
},
78-
ContentMode: markup.RenderContentAsUncyclo,
84+
Metas: localUncycloMetas,
7985
}, input)
8086
assert.NoError(t, err)
8187
assert.Equal(t, strings.TrimSpace(expectedUncyclo), strings.TrimSpace(string(buffer)))
@@ -307,9 +313,8 @@ func TestTotal_RenderUncyclo(t *testing.T) {
307313
Links: markup.Links{
308314
Base: FullURL,
309315
},
310-
Repo: newMockRepo(testRepoOwnerName, testRepoName),
311-
Metas: localMetas,
312-
ContentMode: markup.RenderContentAsUncyclo,
316+
Repo: newMockRepo(testRepoOwnerName, testRepoName),
317+
Metas: localUncycloMetas,
313318
}, sameCases[i])
314319
assert.NoError(t, err)
315320
assert.Equal(t, answers[i], string(line))
@@ -334,7 +339,7 @@ func TestTotal_RenderUncyclo(t *testing.T) {
334339
Links: markup.Links{
335340
Base: FullURL,
336341
},
337-
ContentMode: markup.RenderContentAsUncyclo,
342+
Metas: localUncycloMetas,
338343
}, testCases[i])
339344
assert.NoError(t, err)
340345
assert.EqualValues(t, testCases[i+1], string(line))
@@ -999,9 +1004,9 @@ space</p>
9991004
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
10001005
for i, c := range cases {
10011006
result, err := markdown.RenderString(&markup.RenderContext{
1002-
Ctx: context.Background(),
1003-
Links: c.Links,
1004-
ContentMode: util.Iif(c.IsUncyclo, markup.RenderContentAsUncyclo, markup.RenderContentAsDefault),
1007+
Ctx: context.Background(),
1008+
Links: c.Links,
1009+
Metas: util.Iif(c.IsUncyclo, localUncycloMetas, localMetas),
10051010
}, input)
10061011
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
10071012
assert.Equal(t, c.Expected, string(result), "Unexpected result in testcase %v", i)

modules/markup/markdown/transform_image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (g *ASTTransformer) transformImage(ctx *markup.RenderContext, v *ast.Image)
2121
// Check if the destination is a real link
2222
if len(v.Destination) > 0 && !markup.IsFullURLBytes(v.Destination) {
2323
v.Destination = []byte(giteautil.URLJoin(
24-
ctx.Links.ResolveMediaLink(ctx.ContentMode == markup.RenderContentAsUncyclo),
24+
ctx.Links.ResolveMediaLink(ctx.IsContentModeUncyclo()),
2525
strings.TrimLeft(string(v.Destination), "/"),
2626
))
2727
}

modules/markup/orgmode/orgmode.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,14 @@ func (r *Writer) resolveLink(kind, link string) string {
144144
}
145145

146146
base := r.Ctx.Links.Base
147-
isUncyclo := r.Ctx.ContentMode == markup.RenderContentAsUncyclo
148-
if isUncyclo {
147+
if r.Ctx.IsContentModeUncyclo() {
149148
base = r.Ctx.Links.UncycloLink()
150149
} else if r.Ctx.Links.HasBranchInfo() {
151150
base = r.Ctx.Links.SrcLink()
152151
}
153152

154153
if kind == "image" || kind == "video" {
155-
base = r.Ctx.Links.ResolveMediaLink(isUncyclo)
154+
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsContentModeUncyclo())
156155
}
157156

158157
link = util.URLJoin(base, link)

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+
Metas: map[string]string{"renderContentMode": util.Iif(isUncyclo, "wiki", "")},
3131
}, input)
3232
assert.NoError(t, err)
3333
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))

0 commit comments

Comments
 (0)