From 31798e0eab52ebb57725b92dec2608c6d15ae533 Mon Sep 17 00:00:00 2001 From: athom Date: Fri, 9 Aug 2013 17:24:26 +0800 Subject: [PATCH] add testcase for GFM autolink --- inline.go | 2 +- inline_test.go | 39 +++++++++++++++++++++++++++++++++++++++ markdown.go | 7 ++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/inline.go b/inline.go index 1070762..db02091 100644 --- a/inline.go +++ b/inline.go @@ -620,7 +620,7 @@ func autoLink(p *parser, out *bytes.Buffer, data []byte, offset int) int { // scan backward for a word boundary rewind := 0 - for offset-rewind > 0 && rewind <= 7 && isalnum(data[offset-rewind-1]) { + for offset-rewind > 0 && rewind <= 7 && isletter(data[offset-rewind-1]) { rewind++ } if rewind > 6 { // longest supported protocol is "mailto" which has 6 letters diff --git a/inline_test.go b/inline_test.go index f621529..60f764c 100644 --- a/inline_test.go +++ b/inline_test.go @@ -462,6 +462,45 @@ func TestTags(t *testing.T) { func TestAutoLink(t *testing.T) { var tests = []string{ + "http://foo.com/\n", + "

http://foo.com/

\n", + + "1 http://foo.com/\n", + "

1 http://foo.com/

\n", + + "1http://foo.com/\n", + "

1http://foo.com/

\n", + + "1.http://foo.com/\n", + "

1.http://foo.com/

\n", + + "1. http://foo.com/\n", + "
    \n
  1. http://foo.com/
  2. \n
\n", + + "-http://foo.com/\n", + "

-http://foo.com/

\n", + + "- http://foo.com/\n", + "\n", + + "_http://foo.com/\n", + "

_http://foo.com/

\n", + + "令狐http://foo.com/\n", + "

令狐http://foo.com/

\n", + + "令狐 http://foo.com/\n", + "

令狐 http://foo.com/

\n", + + "ahttp://foo.com/\n", + "

ahttp://foo.com/

\n", + + ">http://foo.com/\n", + "
\n

http://foo.com/

\n
\n", + + "> http://foo.com/\n", + "
\n

http://foo.com/

\n
\n", + "go to \n", "

go to http://foo.com/

\n", diff --git a/markdown.go b/markdown.go index 77e93e8..322300f 100644 --- a/markdown.go +++ b/markdown.go @@ -685,10 +685,15 @@ func isspace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v' } +// Test if a character is letter. +func isletter(c byte) bool { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') +} + // Test if a character is a letter or a digit. // TODO: check when this is looking for ASCII alnum and when it should use unicode func isalnum(c byte) bool { - return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') + return (c >= '0' && c <= '9') || isletter(c) } // Replace tab characters with spaces, aligning to the next TAB_SIZE column.