Skip to content

Commit aaab9df

Browse files
zeripathmrsdizzie
authored andcommitted
Add per repository markdown renderers for better context
1 parent 8f4dd55 commit aaab9df

File tree

8 files changed

+24
-11
lines changed

8 files changed

+24
-11
lines changed

routers/api/v1/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ func RegisterRoutes(m *macaron.Macaron) {
573573
Patch(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.EditLabelOption{}), repo.EditLabel).
574574
Delete(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), repo.DeleteLabel)
575575
})
576+
m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
577+
m.Post("/markdown/raw", misc.MarkdownRaw)
576578
m.Group("/milestones", func() {
577579
m.Combo("").Get(repo.ListMilestones).
578580
Post(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)

routers/api/v1/misc/markdown.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package misc
66

77
import (
8+
"strings"
9+
810
api "code.gitea.io/sdk/gitea"
911

1012
"code.gitea.io/gitea/modules/context"
@@ -45,11 +47,20 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
4547
switch form.Mode {
4648
case "gfm":
4749
md := []byte(form.Text)
48-
context := util.URLJoin(setting.AppURL, form.Context)
50+
urlPrefix := form.Context
51+
var meta map[string]string
52+
if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
53+
// This is still incorrect...
54+
// Need to check if urlPrefix is an url - if so no join
55+
urlPrefix = util.URLJoin(setting.AppURL, form.Context)
56+
}
57+
if ctx.Repo != nil && ctx.Repo.Repository != nil {
58+
meta = ctx.Repo.Repository.ComposeMetas()
59+
}
4960
if form.Wiki {
50-
ctx.Write([]byte(markdown.RenderWiki(md, context, nil)))
61+
ctx.Write([]byte(markdown.RenderWiki(md, urlPrefix, meta)))
5162
} else {
52-
ctx.Write(markdown.Render(md, context, nil))
63+
ctx.Write(markdown.Render(md, urlPrefix, meta))
5364
}
5465
default:
5566
ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))

templates/repo/diff/box.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
<div class="ui comment form">
203203
<div class="ui top attached tabular menu">
204204
<a class="active write item">{{$.i18n.Tr "write"}}</a>
205-
<a class="preview item" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
205+
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
206206
</div>
207207
<div class="ui bottom attached active write tab segment">
208208
<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
@@ -12,7 +12,7 @@
1212
<input type="hidden" name="diff_base_cid">
1313
<div class="ui top attached tabular menu" {{if not $.hidden}}onload="assingMenuAttributes(this)" {{end}}data-write="write" data-preview="preview">
1414
<a class="active item" data-tab="write">{{$.root.i18n.Tr "write"}}</a>
15-
<a class="item" data-tab="preview" data-url="{{$.root.AppSubUrl}}/api/v1/markdown" data-context="{{$.root.RepoLink}}">{{$.root.i18n.Tr "preview"}}</a>
15+
<a class="item" data-tab="preview" data-url="{{$.root.Repository.APIURL}}/markdown" data-context="{{$.root.RepoLink}}">{{$.root.i18n.Tr "preview"}}</a>
1616
</div>
1717
<div class="ui bottom attached active tab segment" data-tab="write">
1818
<div class="field">

templates/repo/editor/edit.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
3131
<a class="active item" data-tab="write"><i class="octicon octicon-code"></i> {{if .IsNewFile}}{{.i18n.Tr "repo.editor.new_file"}}{{else}}{{.i18n.Tr "repo.editor.edit_file"}}{{end}}</a>
3232
{{if not .IsNewFile}}
33-
<a class="item" data-tab="preview" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL | EscapePound}}" data-preview-file-modes="{{.PreviewableFileModes}}"><i class="octicon octicon-eye"></i> {{.i18n.Tr "preview"}}</a>
33+
<a class="item" data-tab="preview" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL | EscapePound}}" data-preview-file-modes="{{.PreviewableFileModes}}"><i class="octicon octicon-eye"></i> {{.i18n.Tr "preview"}}</a>
3434
<a class="item" data-tab="diff" data-url="{{.RepoLink}}/_preview/{{.BranchName | EscapePound}}/{{.TreePath | EscapePound}}" data-context="{{.BranchLink}}"><i class="octicon octicon-diff"></i> {{.i18n.Tr "repo.editor.preview_changes"}}</a>
3535
{{end}}
3636
</div>
3737
<div class="ui bottom attached active tab segment" data-tab="write">
3838
<textarea id="edit_area" name="content" data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
39-
data-url="{{AppSubUrl}}/api/v1/markdown"
39+
data-url="{{.Repository.APIURL}}/markdown"
4040
data-context="{{.RepoLink}}"
4141
data-markdown-file-exts="{{.MarkdownFileExts}}"
4242
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="field">
22
<div class="ui top attached tabular menu" data-write="write" data-preview="preview">
33
<a class="active item" data-tab="write">{{.i18n.Tr "write"}}</a>
4-
<a class="item" data-tab="preview" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{.RepoLink}}">{{.i18n.Tr "preview"}}</a>
4+
<a class="item" data-tab="preview" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}">{{.i18n.Tr "preview"}}</a>
55
</div>
66
<div class="ui bottom attached active tab segment" data-tab="write">
7-
<textarea id="content" class="edit_area js-quick-submit" name="content" tabindex="4" data-id="issue-{{.RepoName}}" data-url="{{AppSubUrl}}/api/v1/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.APIURL}}/markdown" data-context="{{.Repo.RepoLink}}">
88
{{if .BodyQuery}}{{.BodyQuery}}{{else if .IssueTemplate}}{{.IssueTemplate}}{{else if .PullRequestTemplate}}{{.PullRequestTemplate}}{{else}}{{.content}}{{end}}</textarea>
99
</div>
1010
<div class="ui bottom attached tab segment markdown" data-tab="preview">

templates/repo/issue/view_content.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
<div class="ui comment form">
157157
<div class="ui top attached tabular menu">
158158
<a class="active write item">{{$.i18n.Tr "write"}}</a>
159-
<a class="preview item" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
159+
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
160160
</div>
161161
<div class="ui bottom attached active write tab segment">
162162
<textarea tabindex="1" name="content"></textarea>

templates/repo/wiki/new.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<input name="title" value="{{.title}}" autofocus required>
1818
</div>
1919
<div class="field">
20-
<textarea class="js-quick-submit" id="edit_area" name="content" data-id="wiki-{{.title}}" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{.RepoLink}}/wiki" required>{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea>
20+
<textarea class="js-quick-submit" id="edit_area" name="content" data-id="wiki-{{.title}}" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}/wiki" required>{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea>
2121
</div>
2222
<div class="field">
2323
<input name="message" placeholder="{{.i18n.Tr "repo.wiki.default_commit_message"}}">

0 commit comments

Comments
 (0)