diff options
author | Morgan Bazalgette <git@howl.moe> | 2018-02-27 08:09:18 +0100 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2018-02-27 09:09:18 +0200 |
commit | 535445c32ee730988033728b3b91c4d6f456e08c (patch) | |
tree | 34cd5b9807faf01018f47f74a34ed5b584df5158 /modules/markup/markdown/markdown.go | |
parent | 769ab1e4240f820efdb231832cb7957cb4902807 (diff) | |
download | gitea-535445c32ee730988033728b3b91c4d6f456e08c.tar.gz gitea-535445c32ee730988033728b3b91c4d6f456e08c.zip |
Rework special link parsing in the post-processing of markup (#3354)
* Get rid of autolink
* autolink in markdown
* Replace email addresses with mailto links
* better handling of links
* Remove autolink.js from footer
* Refactor entire html.go
* fix some bugs
* Make tests green, move what we can to html_internal_test, various other changes to processor logic
* Make markdown tests work again
This is just a description to allow me to force push in order to restart
the drone build.
* Fix failing markdown tests in routers/api/v1/misc
* Add license headers, log errors, future-proof <body>
* fix formatting
Diffstat (limited to 'modules/markup/markdown/markdown.go')
-rw-r--r-- | modules/markup/markdown/markdown.go | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/modules/markup/markdown/markdown.go b/modules/markup/markdown/markdown.go index 2e3d180c4e..901edb80a7 100644 --- a/modules/markup/markdown/markdown.go +++ b/modules/markup/markdown/markdown.go @@ -22,17 +22,20 @@ type Renderer struct { IsWiki bool } +var byteMailto = []byte("mailto:") + // Link defines how formal links should be processed to produce corresponding HTML elements. func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { - if len(link) > 0 && !markup.IsLink(link) { - if link[0] != '#' { - lnk := string(link) - if r.IsWiki { - lnk = util.URLJoin("wiki", lnk) - } - mLink := util.URLJoin(r.URLPrefix, lnk) - link = []byte(mLink) + // special case: this is not a link, a hash link or a mailto:, so it's a + // relative URL + if len(link) > 0 && !markup.IsLink(link) && + link[0] != '#' && !bytes.HasPrefix(link, byteMailto) { + lnk := string(link) + if r.IsWiki { + lnk = util.URLJoin("wiki", lnk) } + mLink := util.URLJoin(r.URLPrefix, lnk) + link = []byte(mLink) } r.Renderer.Link(out, link, title, content) @@ -124,30 +127,33 @@ func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byt out.WriteString("</a>") } +const ( + blackfridayExtensions = 0 | + blackfriday.EXTENSION_NO_INTRA_EMPHASIS | + blackfriday.EXTENSION_TABLES | + blackfriday.EXTENSION_FENCED_CODE | + blackfriday.EXTENSION_STRIKETHROUGH | + blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK + blackfridayHTMLFlags = 0 | + blackfriday.HTML_SKIP_STYLE | + blackfriday.HTML_OMIT_CONTENTS | + blackfriday.HTML_USE_SMARTYPANTS +) + // RenderRaw renders Markdown to HTML without handling special links. func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte { - htmlFlags := 0 - htmlFlags |= blackfriday.HTML_SKIP_STYLE - htmlFlags |= blackfriday.HTML_OMIT_CONTENTS renderer := &Renderer{ - Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""), + Renderer: blackfriday.HtmlRenderer(blackfridayHTMLFlags, "", ""), URLPrefix: urlPrefix, IsWiki: wikiMarkdown, } - // set up the parser - extensions := 0 - extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS - extensions |= blackfriday.EXTENSION_TABLES - extensions |= blackfriday.EXTENSION_FENCED_CODE - extensions |= blackfriday.EXTENSION_STRIKETHROUGH - extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK - + exts := blackfridayExtensions if setting.Markdown.EnableHardLineBreak { - extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK + exts |= blackfriday.EXTENSION_HARD_LINE_BREAK } - body = blackfriday.Markdown(body, renderer, extensions) + body = blackfriday.Markdown(body, renderer, exts) return body } |