Skip to content

Commit c4ebfbc

Browse files
committed
fix missed api/v1
1 parent 709aa56 commit c4ebfbc

File tree

10 files changed

+110
-11
lines changed

10 files changed

+110
-11
lines changed

routers/web/misc/markdown.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2022 The Gitea Authors. All rights reserved.
3+
// Use of this source code is governed by a MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
package misc
7+
8+
import (
9+
"net/http"
10+
"strings"
11+
12+
"code.gitea.io/gitea/modules/context"
13+
"code.gitea.io/gitea/modules/markup"
14+
"code.gitea.io/gitea/modules/markup/markdown"
15+
"code.gitea.io/gitea/modules/setting"
16+
api "code.gitea.io/gitea/modules/structs"
17+
"code.gitea.io/gitea/modules/util"
18+
"code.gitea.io/gitea/modules/web"
19+
"mvdan.cc/xurls/v2"
20+
)
21+
22+
// Markdown render markdown document to HTML
23+
func Markdown(ctx *context.Context) {
24+
// swagger:operation POST /markdown miscellaneous renderMarkdown
25+
// ---
26+
// summary: Render a markdown document as HTML
27+
// parameters:
28+
// - name: body
29+
// in: body
30+
// schema:
31+
// "$ref": "#/definitions/MarkdownOption"
32+
// consumes:
33+
// - application/json
34+
// produces:
35+
// - text/html
36+
// responses:
37+
// "200":
38+
// "$ref": "#/responses/MarkdownRender"
39+
// "422":
40+
// "$ref": "#/responses/validationError"
41+
42+
form := web.GetForm(ctx).(*api.MarkdownOption)
43+
44+
if ctx.HasAPIError() {
45+
ctx.Error(http.StatusUnprocessableEntity, "", ctx.GetErrMsg())
46+
return
47+
}
48+
49+
if len(form.Text) == 0 {
50+
_, _ = ctx.Write([]byte(""))
51+
return
52+
}
53+
54+
switch form.Mode {
55+
case "comment":
56+
fallthrough
57+
case "gfm":
58+
urlPrefix := form.Context
59+
meta := map[string]string{}
60+
if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
61+
// check if urlPrefix is already set to a URL
62+
linkRegex, _ := xurls.StrictMatchingScheme("https?://")
63+
m := linkRegex.FindStringIndex(urlPrefix)
64+
if m == nil {
65+
urlPrefix = util.URLJoin(setting.AppURL, form.Context)
66+
}
67+
}
68+
if ctx.Repo != nil && ctx.Repo.Repository != nil {
69+
// "gfm" = Github Flavored Markdown - set this to render as a document
70+
if form.Mode == "gfm" {
71+
meta = ctx.Repo.Repository.ComposeDocumentMetas()
72+
} else {
73+
meta = ctx.Repo.Repository.ComposeMetas()
74+
}
75+
}
76+
if form.Mode == "gfm" {
77+
meta["mode"] = "document"
78+
}
79+
80+
if err := markdown.Render(&markup.RenderContext{
81+
Ctx: ctx,
82+
URLPrefix: urlPrefix,
83+
Metas: meta,
84+
IsWiki: form.Wiki,
85+
}, strings.NewReader(form.Text), ctx.Resp); err != nil {
86+
ctx.Error(http.StatusInternalServerError, err.Error())
87+
return
88+
}
89+
default:
90+
if err := markdown.RenderRaw(&markup.RenderContext{
91+
Ctx: ctx,
92+
URLPrefix: form.Context,
93+
}, strings.NewReader(form.Text), ctx.Resp); err != nil {
94+
ctx.Error(http.StatusInternalServerError, err.Error())
95+
return
96+
}
97+
}
98+
}
File renamed without changes.

routers/web/web.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import (
2525
"code.gitea.io/gitea/modules/validation"
2626
"code.gitea.io/gitea/modules/web"
2727
"code.gitea.io/gitea/modules/web/routing"
28-
"code.gitea.io/gitea/routers/api/v1/misc"
2928
"code.gitea.io/gitea/routers/web/admin"
3029
"code.gitea.io/gitea/routers/web/auth"
3130
"code.gitea.io/gitea/routers/web/dev"
3231
"code.gitea.io/gitea/routers/web/events"
3332
"code.gitea.io/gitea/routers/web/explore"
3433
"code.gitea.io/gitea/routers/web/feed"
34+
"code.gitea.io/gitea/routers/web/misc"
3535
"code.gitea.io/gitea/routers/web/org"
3636
"code.gitea.io/gitea/routers/web/repo"
3737
"code.gitea.io/gitea/routers/web/user"
@@ -890,6 +890,7 @@ func RegisterRoutes(m *web.Route) {
890890
m.Group("/comments/{id}", func() {
891891
m.Get("/attachments", repo.GetCommentAttachments)
892892
})
893+
m.Post("/markdown", bindIgnErr(structs.MarkdownOption{}), misc.Markdown)
893894
m.Group("/labels", func() {
894895
m.Post("/new", bindIgnErr(forms.CreateLabelForm{}), repo.NewLabel)
895896
m.Post("/edit", bindIgnErr(forms.CreateLabelForm{}), repo.UpdateLabel)

templates/repo/diff/box.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
<div class="ui comment form">
163163
<div class="ui top attached tabular menu">
164164
<a class="active write item">{{$.i18n.Tr "write"}}</a>
165-
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
165+
<a class="preview item" data-url="{{$.Repository.HTMLURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
166166
</div>
167167
<div class="ui bottom attached active write tab segment">
168168
<textarea class="review-textarea" tabindex="1" name="content"></textarea>

templates/repo/diff/comment_form.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<input type="hidden" name="diff_base_cid">
1212
<div class="ui top tabular menu" data-write="write" data-preview="preview">
1313
<a class="active item" data-tab="write">{{$.root.i18n.Tr "write"}}</a>
14-
<a class="item" data-tab="preview" data-url="{{$.root.Repository.APIURL}}/markdown" data-context="{{$.root.RepoLink}}">{{$.root.i18n.Tr "preview"}}</a>
14+
<a class="item" data-tab="preview" data-url="{{$.root.Repository.HTMLURL}}/markdown" data-context="{{$.root.RepoLink}}">{{$.root.i18n.Tr "preview"}}</a>
1515
</div>
1616
<div class="field">
1717
<div class="ui active tab" data-tab="write">

templates/repo/editor/edit.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
3232
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{.i18n.Tr "repo.editor.new_file"}}{{else}}{{.i18n.Tr "repo.editor.edit_file"}}{{end}}</a>
3333
{{if not .IsNewFile}}
34-
<a class="item" data-tab="preview" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-preview-file-modes="{{.PreviewableFileModes}}" data-markdown-mode="gfm">{{svg "octicon-eye"}} {{.i18n.Tr "preview"}}</a>
34+
<a class="item" data-tab="preview" data-url="{{.Repository.HTMLURL}}/markdown" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-preview-file-modes="{{.PreviewableFileModes}}" data-markdown-mode="gfm">{{svg "octicon-eye"}} {{.i18n.Tr "preview"}}</a>
3535
<a class="item" data-tab="diff" data-url="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}" data-context="{{.BranchLink}}">{{svg "octicon-diff"}} {{.i18n.Tr "repo.editor.preview_changes"}}</a>
3636
{{end}}
3737
</div>
3838
<div class="ui bottom attached active tab segment" data-tab="write">
3939
<textarea id="edit_area" name="content" class="hide" data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
40-
data-url="{{.Repository.APIURL}}/markdown"
40+
data-url="{{.Repository.HTMLURL}}/markdown"
4141
data-context="{{.RepoLink}}"
4242
data-markdown-file-exts="{{.MarkdownFileExts}}"
4343
data-line-wrap-extensions="{{.LineWrapExtensions}}">

templates/repo/issue/comment_tab.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<div class="ui top tabular menu" data-write="write" data-preview="preview">
22
<a class="active item" data-tab="write">{{.i18n.Tr "write"}}</a>
3-
<a class="item" data-tab="preview" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}">{{.i18n.Tr "preview"}}</a>
3+
<a class="item" data-tab="preview" data-url="{{.Repository.HTMLURL}}/markdown" data-context="{{.RepoLink}}">{{.i18n.Tr "preview"}}</a>
44
</div>
55
<div class="field">
66
<div class="ui bottom active tab" data-tab="write">
7-
<textarea id="content" class="edit_area js-quick-submit" name="content" tabindex="4" data-id="issue-{{.RepoName}}" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.Repo.RepoLink}}">
7+
<textarea id="content" class="edit_area js-quick-submit" name="content" tabindex="4" data-id="issue-{{.RepoName}}" data-url="{{.Repository.HTMLURL}}/markdown" data-context="{{.Repo.RepoLink}}">
88
{{- if .BodyQuery}}{{.BodyQuery}}{{else if .IssueTemplate}}{{.IssueTemplate}}{{else if .PullRequestTemplate}}{{.PullRequestTemplate}}{{else}}{{.content}}{{end -}}
99
</textarea>
1010
</div>

templates/repo/issue/view_content.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
<div class="ui comment form">
196196
<div class="ui top tabular menu">
197197
<a class="active write item">{{$.i18n.Tr "write"}}</a>
198-
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
198+
<a class="preview item" data-url="{{$.Repository.HTMLURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
199199
</div>
200200
<div class="field">
201201
<div class="ui bottom active tab write">

templates/repo/release/new.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<label>{{.i18n.Tr "repo.release.content"}}</label>
4949
<div class="ui top tabular menu" data-write="write" data-preview="preview">
5050
<a class="active write item" data-tab="write">{{$.i18n.Tr "write"}}</a>
51-
<a class="preview item" data-tab="preview" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
51+
<a class="preview item" data-tab="preview" data-url="{{$.Repository.HTMLURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
5252
</div>
5353
<div class="ui bottom active tab" data-tab="write">
5454
<textarea name="content">{{.content}}</textarea>

templates/repo/wiki/new.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
</div>
2222
<div class="ui top attached tabular menu previewtabs" data-write="write" data-preview="preview">
2323
<a class="active item" data-tab="write">{{.i18n.Tr "write"}}</a>
24-
<a class="item" data-tab="preview" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
24+
<a class="item" data-tab="preview" data-url="{{$.Repository.HTMLURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
2525
</div>
2626
<div class="field content" data-loading="{{.i18n.Tr "loading"}}">
2727
<div class="ui bottom active tab" data-tab="write">
28-
<textarea class="js-quick-submit" id="edit_area" name="content" data-id="wiki-{{.title}}" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}">{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea>
28+
<textarea class="js-quick-submit" id="edit_area" name="content" data-id="wiki-{{.title}}" data-url="{{.Repository.HTMLURL}}/markdown" data-context="{{.RepoLink}}">{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea>
2929
</div>
3030
</div>
3131
<div class="field">

0 commit comments

Comments
 (0)