all repos — grayfriday @ b3137e7c8fdcaacce02eee9aaf15b50ec559bb34

blackfriday fork with a few changes

Merge pull request #152 from elian0211/about_links

update about links
Vytautas Ĺ altenis vytas@rtfb.lt
Thu, 09 Apr 2015 20:41:45 +0300
commit

b3137e7c8fdcaacce02eee9aaf15b50ec559bb34

parent

3a90da10e373e33f9d809d0324741aade90b7299

3 files changed, 73 insertions(+), 2 deletions(-)

jump to
M html.gohtml.go

@@ -485,7 +485,7 @@ out.WriteString("</em>")

} func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) { - if options.parameters.AbsolutePrefix != "" && isRelativeLink(link) { + if options.parameters.AbsolutePrefix != "" && isRelativeLink(link) && link[0] != '.' { out.WriteString(options.parameters.AbsolutePrefix) if link[0] != '/' { out.WriteByte('/')

@@ -890,6 +890,17 @@ // only the root '/'

if len(link) == 1 && link[0] == '/' { yes = true } + + // current directory : begin with "./" + if len(link) >= 2 && link[0] == '.' && link[1] == '/' { + yes = true + } + + // parent directory : begin with "../" + if len(link) >= 3 && link[0] == '.' && link[1] == '.' && link[2] == '/' { + yes = true + } + return }
M inline.goinline.go

@@ -757,9 +757,20 @@ func isEndOfLink(char byte) bool {

return isspace(char) || char == '<' } -var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://"), []byte("/")} +var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")} +var validPaths = [][]byte{[]byte("/"), []byte("./"), []byte("../")} func isSafeLink(link []byte) bool { + for _, path := range validPaths { + if len(link) >= len(path) && bytes.Equal(link[:len(path)], path) { + if len(link) == len(path) { + return true + } else if isalnum(link[len(path)]) { + return true + } + } + } + for _, prefix := range validUris { // TODO: handle unicode here // case-insensitive prefix test
M inline_test.goinline_test.go

@@ -72,6 +72,7 @@ for i := 0; i+1 < len(tests); i += 2 {

input := tests[i] candidate = input expected := tests[i+1] + actual := runMarkdownInline(candidate, extensions, htmlFlags, params) if actual != expected { t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",

@@ -440,6 +441,15 @@ "<p><a href=\"/url/&amp;query\">link</a></p>\n",

"[[t]](/t)\n", "<p><a href=\"/t\">[t]</a></p>\n", + + "[link](</>)\n", + "<p><a href=\"/\">link</a></p>\n", + + "[link](<./>)\n", + "<p><a href=\"./\">link</a></p>\n", + + "[link](<../>)\n", + "<p><a href=\"../\">link</a></p>\n", } doLinkTestsInline(t, tests)

@@ -452,6 +462,18 @@ "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n",

"[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", + + "[foo](/)\n", + "<p><a href=\"/\">foo</a></p>\n", + + "[foo](./)\n", + "<p><a href=\"./\">foo</a></p>\n", + + "[foo](../)\n", + "<p><a href=\"../\">foo</a></p>\n", + + "[foo](../bar)\n", + "<p><a href=\"../bar\">foo</a></p>\n", } doTestsInlineParam(t, nofollowTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, HtmlRendererParameters{})

@@ -483,6 +505,21 @@ // internal link

"[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", + "[foo](/)\n", + "<p><a href=\"/\">foo</a></p>\n", + + "[foo](./)\n", + "<p><a href=\"./\">foo</a></p>\n", + + "[foo](./bar)\n", + "<p><a href=\"./bar\">foo</a></p>\n", + + "[foo](../)\n", + "<p><a href=\"../\">foo</a></p>\n", + + "[foo](../bar)\n", + "<p><a href=\"../bar\">foo</a></p>\n", + "[foo](http://example.com)\n", "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n", }

@@ -493,6 +530,15 @@ func TestSafeInlineLink(t *testing.T) {

var tests = []string{ "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", + + "[foo](/)\n", + "<p><a href=\"/\">foo</a></p>\n", + + "[foo](./)\n", + "<p><a href=\"./\">foo</a></p>\n", + + "[foo](../)\n", + "<p><a href=\"../\">foo</a></p>\n", "[foo](http://bar/)\n", "<p><a href=\"http://bar/\">foo</a></p>\n",

@@ -541,6 +587,9 @@ "<p>[ref]</p>\n",

"[ref]\n [ref]: /url/ \"title\"\n", "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n", + + "[ref]\n [ref]: ../url/ \"title\"\n", + "<p><a href=\"../url/\" title=\"title\">ref</a></p>\n", } doLinkTestsInline(t, tests) }