Skip to content

Commit 36501dc

Browse files
committed
fix
1 parent a0c0cb3 commit 36501dc

File tree

12 files changed

+42
-53
lines changed

12 files changed

+42
-53
lines changed

models/repo/repo.go

Lines changed: 5 additions & 5 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"
@@ -479,6 +480,8 @@ func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
479480
metas := map[string]string{
480481
"user": repo.OwnerName,
481482
"repo": repo.Name,
483+
484+
"markdownLineBreakStyle": "comment",
482485
}
483486

484487
unit, err := repo.GetUnit(ctx, unit.TypeExternalTracker)
@@ -516,11 +519,8 @@ func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
516519
// ComposeDocumentMetas composes a map of metas for properly rendering documents
517520
func (repo *Repository) ComposeDocumentMetas(ctx context.Context) map[string]string {
518521
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
522+
repo.DocumentRenderingMetas = maps.Clone(repo.ComposeMetas(ctx))
523+
repo.DocumentRenderingMetas["markdownLineBreakStyle"] = "document"
524524
}
525525
return repo.DocumentRenderingMetas
526526
}

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ 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+
crossLinkOnly := ctx.Metas["repo"] == ""
7273

7374
var (
7475
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)