From 8da16ac302b78c4c6bad90cd5c8765de110c42af Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 31 Mar 2017 16:37:30 -0400 Subject: [PATCH] modules/markup: rename Markdown render fucntions The unified function 'Markdown' accepts both string or []byte type input and renders to HTML with []byte type. --- modules/markup/markdown.go | 19 ++++++++++++------- routers/api/v1/misc/markdown.go | 2 +- routers/repo/issue.go | 12 +++++------- routers/repo/release.go | 4 ++-- routers/repo/view.go | 4 ++-- routers/repo/wiki.go | 2 +- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/modules/markup/markdown.go b/modules/markup/markdown.go index 51afe48ec..e59ae9a60 100644 --- a/modules/markup/markdown.go +++ b/modules/markup/markdown.go @@ -458,16 +458,21 @@ OUTER_LOOP: return rawHTML } -// Render renders Markdown to HTML with special links. -func Render(rawBytes []byte, urlPrefix string, metas map[string]string) []byte { +// Markdown takes a string or []byte and renders to HTML in Markdown syntax with special links. +func Markdown(input interface{}, urlPrefix string, metas map[string]string) []byte { + var rawBytes []byte + switch v := input.(type) { + case []byte: + rawBytes = v + case string: + rawBytes = []byte(v) + default: + panic(fmt.Sprintf("unexpected input content type: %T", input)) + } + urlPrefix = strings.Replace(urlPrefix, space, spaceEncoded, -1) result := RenderRaw(rawBytes, urlPrefix) result = PostProcess(result, urlPrefix, metas) result = SanitizeBytes(result) return result } - -// RenderString renders Markdown to HTML with special links and returns string type. -func RenderString(raw, urlPrefix string, metas map[string]string) string { - return string(Render([]byte(raw), urlPrefix, metas)) -} diff --git a/routers/api/v1/misc/markdown.go b/routers/api/v1/misc/markdown.go index 58ff93f50..4c6251b93 100644 --- a/routers/api/v1/misc/markdown.go +++ b/routers/api/v1/misc/markdown.go @@ -25,7 +25,7 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) { switch form.Mode { case "gfm": - ctx.Write(markup.Render([]byte(form.Text), form.Context, nil)) + ctx.Write(markup.Markdown([]byte(form.Text), form.Context, nil)) default: ctx.Write(markup.RenderRaw([]byte(form.Text), "")) } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 917bcad5e..337e49487 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -541,8 +541,7 @@ func viewIssue(ctx *context.Context, isPullList bool) { ctx.Data["PageIsIssueList"] = true } - issue.RenderedContent = string(markup.Render([]byte(issue.Content), ctx.Repo.RepoLink, - ctx.Repo.Repository.ComposeMetas())) + issue.RenderedContent = string(markup.Markdown(issue.Content, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) repo := ctx.Repo.Repository @@ -608,8 +607,7 @@ func viewIssue(ctx *context.Context, isPullList bool) { participants[0] = issue.Poster for _, comment = range issue.Comments { if comment.Type == models.COMMENT_TYPE_COMMENT { - comment.RenderedContent = string(markup.Render([]byte(comment.Content), ctx.Repo.RepoLink, - ctx.Repo.Repository.ComposeMetas())) + comment.RenderedContent = string(markup.Markdown(comment.Content, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) // Check tag. tag, ok = marked[comment.PosterID] @@ -728,7 +726,7 @@ func UpdateIssueContent(ctx *context.Context) { } ctx.JSON(200, map[string]interface{}{ - "content": string(markup.Render([]byte(issue.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())), + "content": markup.Markdown(issue.Content, ctx.Query("context"), ctx.Repo.Repository.ComposeMetas()), }) } @@ -939,7 +937,7 @@ func UpdateCommentContent(ctx *context.Context) { } ctx.JSON(200, map[string]interface{}{ - "content": string(markup.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())), + "content": markup.Markdown(comment.Content, ctx.Query("context"), ctx.Repo.Repository.ComposeMetas()), }) } @@ -1092,7 +1090,7 @@ func Milestones(ctx *context.Context) { if m.NumOpenIssues+m.NumClosedIssues > 0 { m.Completeness = m.NumClosedIssues * 100 / (m.NumOpenIssues + m.NumClosedIssues) } - m.RenderedContent = string(markup.Render([]byte(m.Content), ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) + m.RenderedContent = string(markup.Markdown(m.Content, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) } ctx.Data["Milestones"] = miles diff --git a/routers/repo/release.go b/routers/repo/release.go index b25add772..4d2011810 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -83,7 +83,7 @@ func Releases(ctx *context.Context) { return } - r.Note = markup.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) + r.Note = string(markup.Markdown(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) results[i] = r break } @@ -132,7 +132,7 @@ func Releases(ctx *context.Context) { return } - r.Note = markup.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) + r.Note = string(markup.Markdown(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) } if len(drafts) > 0 { diff --git a/routers/repo/view.go b/routers/repo/view.go index a97c67056..6785b7596 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -88,7 +88,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { switch { case markup.IsMarkdownFile(readmeFile.Name()): ctx.Data["IsMarkdown"] = true - buf = markup.Render(buf, treeLink, ctx.Repo.Repository.ComposeMetas()) + buf = markup.Markdown(buf, treeLink, ctx.Repo.Repository.ComposeMetas()) default: buf = bytes.Replace(buf, []byte("\n"), []byte(`
`), -1) } @@ -160,7 +160,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st ctx.Data["IsIPythonNotebook"] = strings.HasSuffix(blob.Name(), ".ipynb") if isMarkdown { - ctx.Data["FileContent"] = string(markup.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())) + ctx.Data["FileContent"] = string(markup.Markdown(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())) } else { // Building code view blocks with line number on server side. var fileContent string diff --git a/routers/repo/wiki.go b/routers/repo/wiki.go index e0c5fdc59..44b5079a6 100644 --- a/routers/repo/wiki.go +++ b/routers/repo/wiki.go @@ -107,7 +107,7 @@ func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, str return nil, "" } if isViewPage { - ctx.Data["content"] = string(markup.Render(data, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) + ctx.Data["content"] = string(markup.Markdown(data, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) } else { ctx.Data["content"] = string(data) }