Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-22 01:44:24 +01:00

Extend API MarkupOptions to contain branch and relative path

The api.MarkupOptions{} struct will have a RelativePath which depicts
the relative path to the repository root + BranchPath which contains the
current branch. The RenderMarkup function utilizes a struct since there
are too many variables passed as arguments and that is not a good sign
for readability.
This commit is contained in:
Nirmal Kumar R 2024-11-02 21:40:27 +00:00
parent 59bc82f1da
commit 1527ece6cc
6 changed files with 73 additions and 19 deletions

View file

@ -37,6 +37,14 @@ type MarkupOption struct {
// //
// in: body // in: body
FilePath string FilePath string
// The current branch path where the form gets posted
//
// in: body
BranchPath string
// Relative path to the repository
//
// in: body
RelativePath string
} }
// MarkupRender is a rendered markup document // MarkupRender is a rendered markup document

View file

@ -41,7 +41,17 @@ func Markup(ctx *context.APIContext) {
return return
} }
common.RenderMarkup(ctx.Base, ctx.Repo, form.Mode, form.Text, form.Context, form.FilePath, form.Wiki) re := common.Renderer{
Mode: form.Mode,
Text: form.Text,
UrlPrefix: form.Context,
FilePath: form.FilePath,
BranchPath: form.BranchPath,
RelativePath: form.RelativePath,
IsWiki: form.Wiki,
}
re.RenderMarkup(ctx.Base, ctx.Repo)
} }
// Markdown render markdown document to HTML // Markdown render markdown document to HTML
@ -76,7 +86,14 @@ func Markdown(ctx *context.APIContext) {
mode = form.Mode mode = form.Mode
} }
common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, "", form.Wiki) re := common.Renderer{
Mode: mode,
Text: form.Text,
UrlPrefix: form.Context,
IsWiki: form.Wiki,
}
re.RenderMarkup(ctx.Base, ctx.Repo)
} }
// MarkdownRaw render raw markdown HTML // MarkdownRaw render raw markdown HTML

View file

@ -18,26 +18,31 @@ import (
"mvdan.cc/xurls/v2" "mvdan.cc/xurls/v2"
) )
type Renderer struct {
Mode, Text, UrlPrefix, FilePath, BranchPath, RelativePath string
IsWiki bool
}
// RenderMarkup renders markup text for the /markup and /markdown endpoints // RenderMarkup renders markup text for the /markup and /markdown endpoints
func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPrefix, filePath string, wiki bool) { func (re *Renderer) RenderMarkup(ctx *context.Base, repo *context.Repository) {
var markupType string var markupType string
relativePath := "" relativePath := ""
if len(text) == 0 { if len(re.Text) == 0 {
_, _ = ctx.Write([]byte("")) _, _ = ctx.Write([]byte(""))
return return
} }
switch mode { switch re.Mode {
case "markdown": case "markdown":
// Raw markdown // Raw markdown
if err := markdown.RenderRaw(&markup.RenderContext{ if err := markdown.RenderRaw(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
Links: markup.Links{ Links: markup.Links{
AbsolutePrefix: true, AbsolutePrefix: true,
Base: urlPrefix, Base: re.UrlPrefix,
}, },
}, strings.NewReader(text), ctx.Resp); err != nil { }, strings.NewReader(re.Text), ctx.Resp); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
} }
return return
@ -50,30 +55,31 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
case "file": case "file":
// File as document based on file extension // File as document based on file extension
markupType = "" markupType = ""
relativePath = filePath re.UrlPrefix = re.RelativePath
relativePath = re.FilePath
default: default:
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode)) ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", re.Mode))
return return
} }
if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) { if !strings.HasPrefix(setting.AppSubURL+"/", re.UrlPrefix) {
// check if urlPrefix is already set to a URL // check if urlPrefix is already set to a URL
linkRegex, _ := xurls.StrictMatchingScheme("https?://") linkRegex, _ := xurls.StrictMatchingScheme("https?://")
m := linkRegex.FindStringIndex(urlPrefix) m := linkRegex.FindStringIndex(re.UrlPrefix)
if m == nil { if m == nil {
urlPrefix = util.URLJoin(setting.AppURL, urlPrefix) re.UrlPrefix = util.URLJoin(setting.AppURL, re.UrlPrefix)
} }
} }
meta := map[string]string{} meta := map[string]string{}
if repo != nil && repo.Repository != nil { if repo != nil && repo.Repository != nil {
if mode == "comment" { if re.Mode == "comment" {
meta = repo.Repository.ComposeMetas(ctx) meta = repo.Repository.ComposeMetas(ctx)
} else { } else {
meta = repo.Repository.ComposeDocumentMetas(ctx) meta = repo.Repository.ComposeDocumentMetas(ctx)
} }
} }
if mode != "comment" { if re.Mode != "comment" {
meta["mode"] = "document" meta["mode"] = "document"
} }
@ -81,13 +87,14 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
Ctx: ctx, Ctx: ctx,
Links: markup.Links{ Links: markup.Links{
AbsolutePrefix: true, AbsolutePrefix: true,
Base: urlPrefix, Base: re.UrlPrefix,
BranchPath: re.BranchPath,
}, },
Metas: meta, Metas: meta,
IsWiki: wiki, IsWiki: re.IsWiki,
Type: markupType, Type: markupType,
RelativePath: relativePath, RelativePath: relativePath,
}, strings.NewReader(text), ctx.Resp); err != nil { }, strings.NewReader(re.Text), ctx.Resp); err != nil {
if markup.IsErrUnsupportedRenderExtension(err) { if markup.IsErrUnsupportedRenderExtension(err) {
ctx.Error(http.StatusUnprocessableEntity, err.Error()) ctx.Error(http.StatusUnprocessableEntity, err.Error())
} else { } else {

View file

@ -14,5 +14,16 @@ import (
// Markup render markup document to HTML // Markup render markup document to HTML
func Markup(ctx *context.Context) { func Markup(ctx *context.Context) {
form := web.GetForm(ctx).(*api.MarkupOption) form := web.GetForm(ctx).(*api.MarkupOption)
common.RenderMarkup(ctx.Base, ctx.Repo, form.Mode, form.Text, form.Context, form.FilePath, form.Wiki)
re := common.Renderer{
Mode: form.Mode,
Text: form.Text,
UrlPrefix: form.Context,
FilePath: form.FilePath,
BranchPath: form.BranchPath,
RelativePath: form.RelativePath,
IsWiki: form.Wiki,
}
re.RenderMarkup(ctx.Base, ctx.Repo)
} }

View file

@ -28,7 +28,7 @@
<div class="field"> <div class="field">
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff"> <div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.new_file"}}{{else}}{{ctx.Locale.Tr "repo.editor.edit_file"}}{{end}}</a> <a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.new_file"}}{{else}}{{ctx.Locale.Tr "repo.editor.edit_file"}}{{end}}</a>
<a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}/raw/{{.BranchNameSubURL}}" data-markup-mode="file">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a> <a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-markup-mode="file">{{svg "octicon-eye"}} {{ctx.Locale.Tr "preview"}}</a>
{{if not .IsNewFile}} {{if not .IsNewFile}}
<a class="item" data-tab="diff" hx-params="context,content" hx-vals='{"context":"{{.BranchLink}}"}' hx-include="#edit_area" hx-swap="innerHTML" hx-target=".tab[data-tab='diff']" hx-indicator=".tab[data-tab='diff']" hx-post="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">{{svg "octicon-diff"}} {{ctx.Locale.Tr "repo.editor.preview_changes"}}</a> <a class="item" data-tab="diff" hx-params="context,content" hx-vals='{"context":"{{.BranchLink}}"}' hx-include="#edit_area" hx-swap="innerHTML" hx-target=".tab[data-tab='diff']" hx-indicator=".tab[data-tab='diff']" hx-post="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">{{svg "octicon-diff"}} {{ctx.Locale.Tr "repo.editor.preview_changes"}}</a>
{{end}} {{end}}

View file

@ -23,9 +23,20 @@ function initEditPreviewTab($form) {
} }
context = context.substring(0, context.lastIndexOf('/')); context = context.substring(0, context.lastIndexOf('/'));
// fetch branch path, Eg: `/branch/main`
const pathSegments = context.split("/").filter(segment => segment !== "");
const last2Seg = pathSegments.slice(-2);
const branchPath = "/" + last2Seg.join("/")
// fetch relative path to the current repository as `/<username>/<repository>`
// Eg: `/jon-doe/repo1`
const relativePath = context.split("/src"+branchPath)[0]
const formData = new FormData(); const formData = new FormData();
formData.append('mode', mode); formData.append('mode', mode);
formData.append('context', context); formData.append('context', context);
formData.append('branch_path', branchPath)
formData.append('relative_path', relativePath)
formData.append( formData.append(
'text', 'text',
$form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(), $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val(),