parent
77efab57b2
commit
27ba4cebef
13
html.go
13
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)]) {
|
||||
|
|
|
@ -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",
|
||||
"<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 @@ func TestNofollowLink(t *testing.T) {
|
|||
|
||||
"[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 @@ func TestHrefTargetBlank(t *testing.T) {
|
|||
"[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",
|
||||
}
|
||||
|
@ -474,6 +511,15 @@ func TestSafeInlineLink(t *testing.T) {
|
|||
"[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 @@ func TestReferenceLink(t *testing.T) {
|
|||
|
||||
"[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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue