From 27ba4cebef7f37e0bb5685e23cb7213cd809f9e8 Mon Sep 17 00:00:00 2001 From: elian0211 Date: Fri, 20 Feb 2015 17:06:55 +0800 Subject: [PATCH] update about links when link to current directory or parent directory --- html.go | 13 ++++++++++++- inline.go | 9 +++++++-- inline_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/html.go b/html.go index 666d0e3..7d74be4 100644 --- a/html.go +++ b/html.go @@ -476,7 +476,7 @@ func (options *Html) Emphasis(out *bytes.Buffer, text []byte) { } 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 @@ func isRelativeLink(link []byte) (yes bool) { 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 } diff --git a/inline.go b/inline.go index f76d7b3..4715b9e 100644 --- a/inline.go +++ b/inline.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)]) { diff --git a/inline_test.go b/inline_test.go index fb3bdec..4faa8cb 100644 --- a/inline_test.go +++ b/inline_test.go @@ -72,6 +72,7 @@ func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int, 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 @@ func TestInlineLink(t *testing.T) { "[[t]](/t)\n", "

[t]

\n", + + "[link]()\n", + "

link

\n", + + "[link](<./>)\n", + "

link

\n", + + "[link](<../>)\n", + "

link

\n", } doLinkTestsInline(t, tests) @@ -452,6 +462,18 @@ func TestNofollowLink(t *testing.T) { "[foo](/bar/)\n", "

foo

\n", + + "[foo](/)\n", + "

foo

\n", + + "[foo](./)\n", + "

foo

\n", + + "[foo](../)\n", + "

foo

\n", + + "[foo](../bar)\n", + "

foo

\n", } doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, HtmlRendererParameters{}) @@ -463,6 +485,21 @@ func TestHrefTargetBlank(t *testing.T) { "[foo](/bar/)\n", "

foo

\n", + "[foo](/)\n", + "

foo

\n", + + "[foo](./)\n", + "

foo

\n", + + "[foo](./bar)\n", + "

foo

\n", + + "[foo](../)\n", + "

foo

\n", + + "[foo](../bar)\n", + "

foo

\n", + "[foo](http://example.com)\n", "

foo

\n", } @@ -474,6 +511,15 @@ func TestSafeInlineLink(t *testing.T) { "[foo](/bar/)\n", "

foo

\n", + "[foo](/)\n", + "

foo

\n", + + "[foo](./)\n", + "

foo

\n", + + "[foo](../)\n", + "

foo

\n", + "[foo](http://bar/)\n", "

foo

\n", @@ -521,6 +567,9 @@ func TestReferenceLink(t *testing.T) { "[ref]\n [ref]: /url/ \"title\"\n", "

ref

\n", + + "[ref]\n [ref]: ../url/ \"title\"\n", + "

ref

\n", } doLinkTestsInline(t, tests) }