1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-03 09:07:19 +00:00

Render embedded code preview by permlink in markdown (#30234) (#30249)

Backport #30234 by wxiaoguang

The permlink in markdown will be rendered as a code preview block, like GitHub

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Giteabot
2024-04-03 02:20:16 +08:00
committed by GitHub
parent deaabf66d9
commit 6bf6fa8de0
22 changed files with 450 additions and 21 deletions

View File

@ -63,6 +63,7 @@ func MockContext(t *testing.T, reqPath string, opts ...MockContextOption) (*cont
base.Locale = &translation.MockLocale{}
ctx := context.NewWebContext(base, opt.Render, nil)
ctx.AppendContextValue(context.WebContextKey, ctx)
ctx.PageData = map[string]any{}
ctx.Data["PageStartTime"] = time.Now()
chiCtx := chi.NewRouteContext()

View File

@ -11,6 +11,6 @@ import (
func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
FixtureFiles: []string{"user.yml"},
FixtureFiles: []string{"user.yml", "repository.yml", "access.yml", "repo_unit.yml"},
})
}

View File

@ -14,6 +14,8 @@ import (
func ProcessorHelper() *markup.ProcessorHelper {
return &markup.ProcessorHelper{
ElementDir: "auto", // set dir="auto" for necessary (eg: <p>, <h?>, etc) tags
RenderRepoFileCodePreview: renderRepoFileCodePreview,
IsUsernameMentionable: func(ctx context.Context, username string) bool {
mentionedUser, err := user.GetUserByName(ctx, username)
if err != nil {

View File

@ -0,0 +1,117 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
import (
"bufio"
"context"
"fmt"
"html/template"
"strings"
"code.gitea.io/gitea/models/perm/access"
"code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
gitea_context "code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/repository/files"
)
func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePreviewOptions) (template.HTML, error) {
opts.LineStop = max(opts.LineStop, opts.LineStart)
lineCount := opts.LineStop - opts.LineStart + 1
if lineCount <= 0 || lineCount > 140 /* GitHub at most show 140 lines */ {
lineCount = 10
opts.LineStop = opts.LineStart + lineCount
}
dbRepo, err := repo.GetRepositoryByOwnerAndName(ctx, opts.OwnerName, opts.RepoName)
if err != nil {
return "", err
}
webCtx, ok := ctx.Value(gitea_context.WebContextKey).(*gitea_context.Context)
if !ok {
return "", fmt.Errorf("context is not a web context")
}
doer := webCtx.Doer
perms, err := access.GetUserRepoPermission(ctx, dbRepo, doer)
if err != nil {
return "", err
}
if !perms.CanRead(unit.TypeCode) {
return "", fmt.Errorf("no permission")
}
gitRepo, err := gitrepo.OpenRepository(ctx, dbRepo)
if err != nil {
return "", err
}
defer gitRepo.Close()
commit, err := gitRepo.GetCommit(opts.CommitID)
if err != nil {
return "", err
}
language, _ := files.TryGetContentLanguage(gitRepo, opts.CommitID, opts.FilePath)
blob, err := commit.GetBlobByPath(opts.FilePath)
if err != nil {
return "", err
}
if blob.Size() > setting.UI.MaxDisplayFileSize {
return "", fmt.Errorf("file is too large")
}
dataRc, err := blob.DataAsync()
if err != nil {
return "", err
}
defer dataRc.Close()
reader := bufio.NewReader(dataRc)
for i := 1; i < opts.LineStart; i++ {
if _, err = reader.ReadBytes('\n'); err != nil {
return "", err
}
}
lineNums := make([]int, 0, lineCount)
lineCodes := make([]string, 0, lineCount)
for i := opts.LineStart; i <= opts.LineStop; i++ {
if line, err := reader.ReadString('\n'); err != nil && line == "" {
break
} else {
lineNums = append(lineNums, i)
lineCodes = append(lineCodes, line)
}
}
realLineStop := max(opts.LineStart, opts.LineStart+len(lineNums)-1)
highlightLines := code.HighlightSearchResultCode(opts.FilePath, language, lineNums, strings.Join(lineCodes, ""))
escapeStatus := &charset.EscapeStatus{}
lineEscapeStatus := make([]*charset.EscapeStatus, len(highlightLines))
for i, hl := range highlightLines {
lineEscapeStatus[i], hl.FormattedContent = charset.EscapeControlHTML(hl.FormattedContent, webCtx.Base.Locale, charset.RuneNBSP)
escapeStatus = escapeStatus.Or(lineEscapeStatus[i])
}
return webCtx.RenderToHTML("base/markup_codepreview", map[string]any{
"FullURL": opts.FullURL,
"FilePath": opts.FilePath,
"LineStart": opts.LineStart,
"LineStop": realLineStop,
"RepoLink": dbRepo.Link(),
"CommitID": opts.CommitID,
"HighlightLines": highlightLines,
"EscapeStatus": escapeStatus,
"LineEscapeStatus": lineEscapeStatus,
})
}

View File

@ -0,0 +1,83 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
import (
"testing"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/services/contexttest"
"github.com/stretchr/testify/assert"
)
func TestProcessorHelperCodePreview(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
ctx, _ := contexttest.MockContext(t, "/", contexttest.MockContextOption{Render: templates.HTMLRenderer()})
htm, err := renderRepoFileCodePreview(ctx, markup.RenderCodePreviewOptions{
FullURL: "http://full",
OwnerName: "user2",
RepoName: "repo1",
CommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
FilePath: "/README.md",
LineStart: 1,
LineStop: 2,
})
assert.NoError(t, err)
assert.Equal(t, `<div class="code-preview-container file-content">
<div class="code-preview-header">
<a href="http://full" class="muted" rel="nofollow">/README.md</a>
repo.code_preview_line_from_to:1,2,<a href="/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow">65f1bf27bc</a>
</div>
<table class="file-view">
<tbody><tr>
<td class="lines-num"><span data-line-number="1"></span></td>
<td class="lines-code chroma"><code class="code-inner"><span class="gh"># repo1</code></td>
</tr><tr>
<td class="lines-num"><span data-line-number="2"></span></td>
<td class="lines-code chroma"><code class="code-inner"></span><span class="gh"></span></code></td>
</tr></tbody>
</table>
</div>
`, string(htm))
ctx, _ = contexttest.MockContext(t, "/", contexttest.MockContextOption{Render: templates.HTMLRenderer()})
htm, err = renderRepoFileCodePreview(ctx, markup.RenderCodePreviewOptions{
FullURL: "http://full",
OwnerName: "user2",
RepoName: "repo1",
CommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
FilePath: "/README.md",
LineStart: 1,
})
assert.NoError(t, err)
assert.Equal(t, `<div class="code-preview-container file-content">
<div class="code-preview-header">
<a href="http://full" class="muted" rel="nofollow">/README.md</a>
repo.code_preview_line_in:1,<a href="/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow">65f1bf27bc</a>
</div>
<table class="file-view">
<tbody><tr>
<td class="lines-num"><span data-line-number="1"></span></td>
<td class="lines-code chroma"><code class="code-inner"><span class="gh"># repo1</code></td>
</tr></tbody>
</table>
</div>
`, string(htm))
ctx, _ = contexttest.MockContext(t, "/", contexttest.MockContextOption{Render: templates.HTMLRenderer()})
_, err = renderRepoFileCodePreview(ctx, markup.RenderCodePreviewOptions{
FullURL: "http://full",
OwnerName: "user15",
RepoName: "big_test_private_1",
CommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
FilePath: "/README.md",
LineStart: 1,
LineStop: 10,
})
assert.ErrorContains(t, err, "no permission")
}