all repos — grayfriday @ 6928e11ecd2bf32f22ba9a46b542257fa76bdc34

blackfriday fork with a few changes

Merge pull request #155 from beyang/master

add rel="noreferrer" option
Vytautas Ĺ altenis vytas@rtfb.lt
Sun, 15 Mar 2015 19:31:50 +0200
commit

6928e11ecd2bf32f22ba9a46b542257fa76bdc34

parent

77efab57b2f74dd3f9051c79752b2e8995c8b789

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

jump to
M html.gohtml.go

@@ -31,6 +31,7 @@ HTML_SKIP_IMAGES // skip embedded images

HTML_SKIP_LINKS // skip all links HTML_SAFELINK // only link to trusted protocols HTML_NOFOLLOW_LINKS // only link with rel="nofollow" + HTML_NOREFERRER_LINKS // only link with rel="noreferrer" 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)

@@ -429,9 +430,17 @@ }

entityEscapeWithSkip(out, link, skipRanges) + var relAttrs []string if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) { - out.WriteString("\" rel=\"nofollow") + relAttrs = append(relAttrs, "nofollow") } + if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) { + relAttrs = append(relAttrs, "noreferrer") + } + if len(relAttrs) > 0 { + out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " "))) + } + // blank target only add to external link if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) { out.WriteString("\" target=\"_blank")

@@ -535,9 +544,17 @@ if len(title) > 0 {

out.WriteString("\" title=\"") attrEscape(out, title) } + var relAttrs []string if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) { - out.WriteString("\" rel=\"nofollow") + relAttrs = append(relAttrs, "nofollow") } + if options.flags&HTML_NOREFERRER_LINKS != 0 && !isRelativeLink(link) { + relAttrs = append(relAttrs, "noreferrer") + } + if len(relAttrs) > 0 { + out.WriteString(fmt.Sprintf("\" rel=\"%s", strings.Join(relAttrs, " "))) + } + // blank target only add to external link if options.flags&HTML_HREF_TARGET_BLANK != 0 && !isRelativeLink(link) { out.WriteString("\" target=\"_blank")
M inline_test.goinline_test.go

@@ -445,15 +445,35 @@ doLinkTestsInline(t, tests)

} -func TestNofollowLink(t *testing.T) { - var tests = []string{ +func TestRelAttrLink(t *testing.T) { + var nofollowTests = []string{ "[foo](http://bar.com/foo/)\n", "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n", "[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", } - doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, + doTestsInlineParam(t, nofollowTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, + HtmlRendererParameters{}) + + var noreferrerTests = []string{ + "[foo](http://bar.com/foo/)\n", + "<p><a href=\"http://bar.com/foo/\" rel=\"noreferrer\">foo</a></p>\n", + + "[foo](/bar/)\n", + "<p><a href=\"/bar/\">foo</a></p>\n", + } + doTestsInlineParam(t, noreferrerTests, 0, HTML_SAFELINK|HTML_NOREFERRER_LINKS, + HtmlRendererParameters{}) + + var nofollownoreferrerTests = []string{ + "[foo](http://bar.com/foo/)\n", + "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow noreferrer\">foo</a></p>\n", + + "[foo](/bar/)\n", + "<p><a href=\"/bar/\">foo</a></p>\n", + } + doTestsInlineParam(t, nofollownoreferrerTests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_NOREFERRER_LINKS, HtmlRendererParameters{}) }