summaryrefslogtreecommitdiffstats
path: root/modules/markup/markdown
diff options
context:
space:
mode:
authorMorgan Bazalgette <git@howl.moe>2018-02-27 08:09:18 +0100
committerLauris BH <lauris@nix.lv>2018-02-27 09:09:18 +0200
commit535445c32ee730988033728b3b91c4d6f456e08c (patch)
tree34cd5b9807faf01018f47f74a34ed5b584df5158 /modules/markup/markdown
parent769ab1e4240f820efdb231832cb7957cb4902807 (diff)
downloadgitea-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')
-rw-r--r--modules/markup/markdown/markdown.go50
-rw-r--r--modules/markup/markdown/markdown_test.go102
2 files changed, 35 insertions, 117 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
}
diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go
index c19037f629..605094df46 100644
--- a/modules/markup/markdown/markdown_test.go
+++ b/modules/markup/markdown/markdown_test.go
@@ -8,7 +8,6 @@ import (
"strings"
"testing"
- "code.gitea.io/gitea/modules/markup"
. "code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
@@ -41,69 +40,6 @@ func TestRender_StandardLinks(t *testing.T) {
`<p><a href="`+lnkWiki+`" rel="nofollow">WikiPage</a></p>`)
}
-func TestRender_ShortLinks(t *testing.T) {
- setting.AppURL = AppURL
- setting.AppSubURL = AppSubURL
- tree := util.URLJoin(AppSubURL, "src", "master")
-
- test := func(input, expected, expectedWiki string) {
- buffer := RenderString(input, tree, nil)
- assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
- buffer = RenderWiki([]byte(input), setting.AppSubURL, nil)
- assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
- }
-
- rawtree := util.URLJoin(AppSubURL, "raw", "master")
- url := util.URLJoin(tree, "Link")
- otherUrl := util.URLJoin(tree, "OtherLink")
- imgurl := util.URLJoin(rawtree, "Link.jpg")
- urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
- otherUrlWiki := util.URLJoin(AppSubURL, "wiki", "OtherLink")
- imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
- favicon := "http://google.com/favicon.ico"
-
- test(
- "[[Link]]",
- `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
- `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
- test(
- "[[Link.jpg]]",
- `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Link.jpg" title="Link.jpg"/></a></p>`,
- `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Link.jpg" title="Link.jpg"/></a></p>`)
- test(
- "[["+favicon+"]]",
- `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
- `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
- test(
- "[[Name|Link]]",
- `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
- `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
- test(
- "[[Name|Link.jpg]]",
- `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Name" title="Name"/></a></p>`,
- `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Name" title="Name"/></a></p>`)
- test(
- "[[Name|Link.jpg|alt=AltName]]",
- `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="AltName"/></a></p>`,
- `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="AltName"/></a></p>`)
- test(
- "[[Name|Link.jpg|title=Title]]",
- `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Title" title="Title"/></a></p>`,
- `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Title" title="Title"/></a></p>`)
- test(
- "[[Name|Link.jpg|alt=AltName|title=Title]]",
- `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
- `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
- test(
- "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
- `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
- `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
- test(
- "[[Link]] [[OtherLink]]",
- `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherUrl+`" rel="nofollow">OtherLink</a></p>`,
- `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherUrlWiki+`" rel="nofollow">OtherLink</a></p>`)
-}
-
func TestMisc_IsMarkdownFile(t *testing.T) {
setting.Markdown.FileExtensions = []string{".md", ".markdown", ".mdown", ".mkd"}
trueTestCases := []string{
@@ -141,35 +77,11 @@ func TestRender_Images(t *testing.T) {
test(
"!["+title+"]("+url+")",
- `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"></a></p>`)
+ `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
test(
"[["+title+"|"+url+"]]",
- `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`" title="`+title+`"/></a></p>`)
-}
-
-func TestRegExp_ShortLinkPattern(t *testing.T) {
- trueTestCases := []string{
- "[[stuff]]",
- "[[]]",
- "[[stuff|title=Difficult name with spaces*!]]",
- }
- falseTestCases := []string{
- "test",
- "abcdefg",
- "[[]",
- "[[",
- "[]",
- "]]",
- "abcdefghijklmnopqrstuvwxyz",
- }
-
- for _, testCase := range trueTestCases {
- assert.True(t, markup.ShortLinkPattern.MatchString(testCase))
- }
- for _, testCase := range falseTestCases {
- assert.False(t, markup.ShortLinkPattern.MatchString(testCase))
- }
+ `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" title="`+title+`" alt="`+title+`"/></a></p>`)
}
func testAnswers(baseURLContent, baseURLImages string) []string {
@@ -185,7 +97,7 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
<ul>
<li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" rel="nofollow">#786</a></li>
-<li>Node graph editors https://github.com/ocornut/imgui/issues/306</li>
+<li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
<li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
<li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
</ul>
@@ -201,14 +113,14 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
<table>
<thead>
<tr>
-<th><a href="` + baseURLImages + `/images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-install.png" alt="images/icon-install.png" title="icon-install.png"/></a></th>
+<th><a href="` + baseURLImages + `/images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-install.png" title="icon-install.png" alt="images/icon-install.png"/></a></th>
<th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
</tr>
</thead>
<tbody>
<tr>
-<td><a href="` + baseURLImages + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-usage.png" alt="images/icon-usage.png" title="icon-usage.png"/></a></td>
+<td><a href="` + baseURLImages + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-usage.png" title="icon-usage.png" alt="images/icon-usage.png"/></a></td>
<td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
</tr>
</tbody>
@@ -218,9 +130,9 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
<ol>
<li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
-<a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" alt="images/1.png" title="1.png"/></a></li>
+<a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" title="1.png" alt="images/1.png"/></a></li>
<li>Perform a test run by hitting the Run! button.
-<a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" alt="images/2.png" title="2.png"/></a></li>
+<a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" title="2.png" alt="images/2.png"/></a></li>
</ol>
`,
}