all repos — grayfriday @ ecf59d4a55448e1cfbc558e09a83a422e0167228

blackfriday fork with a few changes

add target blank attr
gihnius gihnius@gmail.com
Fri, 21 Mar 2014 10:52:46 +0800
commit

ecf59d4a55448e1cfbc558e09a83a422e0167228

parent

e078bb8ec34db7cc285f347d24f367b95c909ece

2 files changed, 49 insertions(+), 2 deletions(-)

jump to
M html.gohtml.go

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

HTML_SKIP_SCRIPT // skip embedded <script> elements 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

@@ -413,6 +414,15 @@ if kind == LINK_TYPE_EMAIL {

out.WriteString("mailto:") } attrEscape(out, link) + + if options.flags&HTML_NOFOLLOW_LINKS != 0 { + 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

@@ -503,6 +513,11 @@ }

if options.flags&HTML_NOFOLLOW_LINKS != 0 { 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>")

@@ -788,3 +803,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

@@ -429,6 +429,18 @@ }

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) { var tests = []string{ "[foo](/bar/)\n",

@@ -605,9 +617,9 @@

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