diff options
author | Andrew Boyarshin <andrew.boyarshin@gmail.com> | 2017-02-14 08:13:59 +0700 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-14 09:13:59 +0800 |
commit | dc8248f8a49e4801e119008a32b28cd2ad6e1a57 (patch) | |
tree | f3749cdfb1b4766e6f9d7d601f2de7654b747087 /routers/api | |
parent | 5cc275b1defc56d54bec23d1a5740c3fadcff2b0 (diff) | |
download | gitea-dc8248f8a49e4801e119008a32b28cd2ad6e1a57.tar.gz gitea-dc8248f8a49e4801e119008a32b28cd2ad6e1a57.zip |
Markdown rendering overhaul (#186)
* Markdown rendering overhaul
Cleaned up and squashed commits into single one.
Signed-off-by: Andrew Boyarshin <boyarshinand@gmail.com>
* Fix markdown API, add markdown module and API tests, improve code coverage
Signed-off-by: Andrew Boyarshin <boyarshinand@gmail.com>
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/misc/markdown.go | 7 | ||||
-rw-r--r-- | routers/api/v1/misc/markdown_test.go | 184 |
2 files changed, 188 insertions, 3 deletions
diff --git a/routers/api/v1/misc/markdown.go b/routers/api/v1/misc/markdown.go index 1a0c003e7f..947924dbed 100644 --- a/routers/api/v1/misc/markdown.go +++ b/routers/api/v1/misc/markdown.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/markdown" + "code.gitea.io/gitea/modules/setting" ) // Markdown render markdown document to HTML @@ -26,9 +27,9 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) { switch form.Mode { case "gfm": - ctx.Write(markdown.Render([]byte(form.Text), form.Context, nil)) + ctx.Write(markdown.Render([]byte(form.Text), markdown.URLJoin(setting.AppURL, form.Context), nil)) default: - ctx.Write(markdown.RenderRaw([]byte(form.Text), "")) + ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false)) } } @@ -40,5 +41,5 @@ func MarkdownRaw(ctx *context.APIContext) { ctx.Error(422, "", err) return } - ctx.Write(markdown.RenderRaw(body, "")) + ctx.Write(markdown.RenderRaw(body, "", false)) } diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go new file mode 100644 index 0000000000..398e652d21 --- /dev/null +++ b/routers/api/v1/misc/markdown_test.go @@ -0,0 +1,184 @@ +package misc + +import ( + "net/http" + "net/http/httptest" + "testing" + + macaron "gopkg.in/macaron.v1" + + "net/url" + + "io/ioutil" + "strings" + + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/markdown" + "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/sdk/gitea" + "github.com/go-macaron/inject" + "github.com/stretchr/testify/assert" +) + +const AppURL = "http://localhost:3000/" +const Repo = "gogits/gogs" +const AppSubURL = AppURL + Repo + "/" + +func createContext(req *http.Request) (*macaron.Context, *httptest.ResponseRecorder) { + resp := httptest.NewRecorder() + c := &macaron.Context{ + Injector: inject.New(), + Req: macaron.Request{req}, + Resp: macaron.NewResponseWriter(resp), + Render: &macaron.DummyRender{resp}, + Data: make(map[string]interface{}), + } + c.Map(c) + c.Map(req) + return c, resp +} + +func wrap(ctx *macaron.Context) *context.APIContext { + return &context.APIContext{ + Context: &context.Context{ + Context: ctx, + }, + } +} + +func TestAPI_RenderGFM(t *testing.T) { + setting.AppURL = AppURL + + options := api.MarkdownOption{ + Mode: "gfm", + Text: "", + Context: Repo, + } + requrl, _ := url.Parse(markdown.URLJoin(AppURL, "api", "v1", "markdown")) + req := &http.Request{ + Method: "POST", + URL: requrl, + } + m, resp := createContext(req) + ctx := wrap(m) + + testCases := []string{ + // dear imgui wiki markdown extract: special wiki syntax + `Wiki! Enjoy :) +- [[Links, Language bindings, Engine bindings|Links]] +- [[Tips]] +- Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`, + // rendered + `<p>Wiki! Enjoy :)</p> + +<ul> +<li><a href="` + AppSubURL + `wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li> +<li><a href="` + AppSubURL + `wiki/Tips" rel="nofollow">Tips</a></li> +<li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>)<a href="` + AppSubURL + `issues/786" rel="nofollow">#786</a></li> +</ul> +`, + // wine-staging wiki home extract: special wiki syntax, images + `## What is Wine Staging? +**Wine Staging** on website [wine-staging.com](http://wine-staging.com). + +## Quick Links +Here are some links to the most important topics. You can find the full list of pages at the sidebar. + +[[Configuration]] +[[images/icon-bug.png]] +`, + // rendered + `<h2>What is Wine Staging?</h2> + +<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p> + +<h2>Quick Links</h2> + +<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p> + +<p><a href="` + AppSubURL + `wiki/Configuration" rel="nofollow">Configuration</a> +<a href="` + AppSubURL + `wiki/raw/images%2Ficon-bug.png" rel="nofollow"><img src="` + AppSubURL + `wiki/raw/images%2Ficon-bug.png" alt="images/icon-bug.png" title="icon-bug.png"/></a></p> +`, + // Guard wiki sidebar: special syntax + `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`, + // rendered + `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p> +`, + // special syntax + `[[Name|Link]]`, + // rendered + `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p> +`, + // empty + ``, + // rendered + ``, + } + + for i := 0; i < len(testCases); i += 2 { + options.Text = testCases[i] + Markdown(ctx, options) + assert.Equal(t, testCases[i+1], resp.Body.String()) + resp.Body.Reset() + } +} + +var simpleCases = []string{ + // Guard wiki sidebar: special syntax + `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`, + // rendered + `<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p> +`, + // special syntax + `[[Name|Link]]`, + // rendered + `<p>[[Name|Link]]</p> +`, + // empty + ``, + // rendered + ``, +} + +func TestAPI_RenderSimple(t *testing.T) { + setting.AppURL = AppURL + + options := api.MarkdownOption{ + Mode: "markdown", + Text: "", + Context: Repo, + } + requrl, _ := url.Parse(markdown.URLJoin(AppURL, "api", "v1", "markdown")) + req := &http.Request{ + Method: "POST", + URL: requrl, + } + m, resp := createContext(req) + ctx := wrap(m) + + for i := 0; i < len(simpleCases); i += 2 { + options.Text = simpleCases[i] + Markdown(ctx, options) + assert.Equal(t, simpleCases[i+1], resp.Body.String()) + resp.Body.Reset() + } +} + +func TestAPI_RenderRaw(t *testing.T) { + setting.AppURL = AppURL + + requrl, _ := url.Parse(markdown.URLJoin(AppURL, "api", "v1", "markdown")) + req := &http.Request{ + Method: "POST", + URL: requrl, + } + m, resp := createContext(req) + ctx := wrap(m) + + for i := 0; i < len(simpleCases); i += 2 { + ctx.Req.Request.Body = ioutil.NopCloser(strings.NewReader(simpleCases[i])) + MarkdownRaw(ctx) + assert.Equal(t, simpleCases[i+1], resp.Body.String()) + resp.Body.Reset() + } +} |