all repos — grayfriday @ 60ba757eaa560443041b4367c1519f0b6a7955e8

blackfriday fork with a few changes

Merge branch 'gihnius-master'
Vytautas Ĺ altenis vytas@rtfb.lt
Thu, 01 May 2014 21:46:51 +0300
commit

60ba757eaa560443041b4367c1519f0b6a7955e8

parent

b44be784594d37dce56d17a088791d4b4b8d7354

2 files changed, 58 insertions(+), 5 deletions(-)

jump to
M html.gohtml.go

@@ -32,6 +32,7 @@ HTML_SKIP_LINKS // skip all links

HTML_SANITIZE_OUTPUT // strip output of everything that's not known to be safe HTML_SAFELINK // only link to trusted protocols HTML_NOFOLLOW_LINKS // only link with rel="nofollow" + HTML_HREF_TARGET_BLANK // add a blank target HTML_TOC // generate a table of contents HTML_OMIT_CONTENTS // skip the main contents (for a standalone table of contents) HTML_COMPLETE_PAGE // generate a complete HTML page

@@ -411,6 +412,15 @@ if kind == LINK_TYPE_EMAIL {

out.WriteString("mailto:") } entityEscapeWithSkip(out, link, skipRanges) + + if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) { + out.WriteString("\" rel=\"nofollow") + } + // blank target only add to external link + if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) { + out.WriteString("\" target=\"_blank") + } + out.WriteString("\">") // Pretty print: if we get an email address as

@@ -498,9 +508,14 @@ if len(title) > 0 {

out.WriteString("\" title=\"") attrEscape(out, title) } - if options.flags&HTML_NOFOLLOW_LINKS != 0 { + if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) { out.WriteString("\" rel=\"nofollow") } + // blank target only add to external link + if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) { + out.WriteString("\" target=\"_blank") + } + out.WriteString("\">") out.Write(content) out.WriteString("</a>")

@@ -800,3 +815,23 @@ if out.Len() > 0 {

out.WriteByte('\n') } } + +func isRelativeLink(link []byte) (yes bool) { + yes = false + + // a tag begin with '#' + if link[0] == '#' { + yes = true + } + + // link begin with '/' but not '//', the second maybe a protocol relative link + if len(link) >= 2 && link[0] == '/' && link[1] != '/' { + yes = true + } + + // only the root '/' + if len(link) == 1 && link[0] == '/' { + yes = true + } + return +}
M inline_test.goinline_test.go

@@ -524,10 +524,28 @@ }

func TestNofollowLink(t *testing.T) { var tests = []string{ - "[foo](http://bar.baz/)\n", - "<p><a href=\"http://bar.baz/\" rel=\"nofollow\">foo</a></p>\n", + "[foo](http://bar.com/foo/)\n", + "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n", } doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_SANITIZE_OUTPUT) + // HTML_SANITIZE_OUTPUT won't allow relative links, so test that separately: + tests = []string{ + "[foo](/bar/)\n", + "<p><a href=\"/bar/\">foo</a></p>\n", + } + doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS) +} + +func TestHrefTargetBlank(t *testing.T) { + var tests = []string{ + // internal link + "[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", + } + doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK) } func TestSafeInlineLink(t *testing.T) {

@@ -733,9 +751,9 @@

[^b]: Paragraph 1 Paragraph 2 - + ` + "```\n\tsome code\n\t```" + ` - + Paragraph 3 No longer in the footnote