all repos — grayfriday @ 27ba4cebef7f37e0bb5685e23cb7213cd809f9e8

blackfriday fork with a few changes

update about links
when link to current directory or parent directory
elian0211 elian0211@gmail.com
Fri, 20 Feb 2015 17:06:55 +0800
commit

27ba4cebef7f37e0bb5685e23cb7213cd809f9e8

parent

77efab57b2f74dd3f9051c79752b2e8995c8b789

3 files changed, 68 insertions(+), 3 deletions(-)

jump to
M html.gohtml.go

@@ -476,7 +476,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('/')

@@ -873,6 +873,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,10 +757,15 @@ 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("/"), []byte("./"), []byte("../"), []byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")} func isSafeLink(link []byte) bool { - for _, prefix := range validUris { + for index, prefix := range validUris { + if index <= 2 { + if len(link) == len(prefix) && bytes.Equal(bytes.ToLower(link[:len(prefix)]), prefix) { + return true + } + } // TODO: handle unicode here // case-insensitive prefix test if len(link) > len(prefix) && bytes.Equal(bytes.ToLower(link[:len(prefix)]), prefix) && isalnum(link[len(prefix)]) {
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, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, HtmlRendererParameters{})

@@ -463,6 +485,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", }

@@ -473,6 +510,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",

@@ -521,6 +567,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) }