From d1473997ce6a26074098bddf6334d1ca8418949c Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Wed, 9 Oct 2019 14:03:16 -0400 Subject: [PATCH] add [link](url) support to markdown --- fun.go | 1 - markitzero.go | 12 ++++++++++-- markitzero_test.go | 39 +++++++++++++++++++++++++-------------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/fun.go b/fun.go index 1e8ed17..08db677 100644 --- a/fun.go +++ b/fun.go @@ -256,7 +256,6 @@ type Emu struct { Name string } -var re_link = regexp.MustCompile(`@?https?://[^\s"]+[\w/)]`) var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`) func herdofemus(noise string) []Emu { diff --git a/markitzero.go b/markitzero.go index 7e0b668..71d1ba7 100644 --- a/markitzero.go +++ b/markitzero.go @@ -28,6 +28,8 @@ var re_italicer = regexp.MustCompile(`(^|\W)\*([\w\s,.!?':_-]+)\*($|\W)`) var re_bigcoder = regexp.MustCompile("```\n?((?s:.*?))\n?```\n?") var re_coder = regexp.MustCompile("`([^`]*)`") var re_quoter = regexp.MustCompile(`(?m:^> (.*)\n?)`) +var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)]`) +var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`) func markitzero(s string) string { // prepare the string @@ -52,6 +54,7 @@ func markitzero(s string) string { s = re_italicer.ReplaceAllString(s, "$1$2$3") s = re_quoter.ReplaceAllString(s, "
$1

") s = re_link.ReplaceAllStringFunc(s, linkreplacer) + s = re_zerolink.ReplaceAllString(s, `$1`) // now restore the code blocks s = re_coder.ReplaceAllStringFunc(s, func(s string) string { @@ -76,9 +79,14 @@ func markitzero(s string) string { } func linkreplacer(url string) string { - if url[0] == '@' { + if url[0:2] == "](" { return url } + prefix := "" + for !strings.HasPrefix(url, "http") { + prefix += url[0:1] + url = url[1:] + } addparen := false adddot := false if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 { @@ -96,5 +104,5 @@ func linkreplacer(url string) string { if addparen { url += ")" } - return url + return prefix + url } diff --git a/markitzero_test.go b/markitzero_test.go index e6874bd..ccef139 100644 --- a/markitzero_test.go +++ b/markitzero_test.go @@ -4,40 +4,51 @@ import ( "testing" ) -func onetest(t *testing.T, input, output string) { +func doonezerotest(t *testing.T, input, output string) { result := markitzero(input) if result != output { t.Errorf("\nexpected:\n%s\noutput:\n%s", output, result) } } -func basictest(t *testing.T) { +func TestBasictest(t *testing.T) { input := `link to https://example.com/ with **bold** text` output := `link to https://example.com/ with bold text` - onetest(t, input, output) + doonezerotest(t, input, output) } -func linebreak1(t *testing.T) { +func TestLinebreak1(t *testing.T) { input := "hello\n> a quote\na comment" output := "hello

a quote

a comment" - onetest(t, input, output) + doonezerotest(t, input, output) } -func linebreak2(t *testing.T) { +func TestLinebreak2(t *testing.T) { input := "hello\n\n> a quote\n\na comment" output := "hello

a quote

a comment" - onetest(t, input, output) + doonezerotest(t, input, output) } -func linebreak3(t *testing.T) { +func TestLinebreak3(t *testing.T) { input := "hello\n\n```\nfunc(s string)\n```\n\ndoes it go?" output := "hello

func(s string)

does it go?" - onetest(t, input, output) + doonezerotest(t, input, output) } -func TestMarkitzero(t *testing.T) { - basictest(t) - linebreak1(t) - linebreak2(t) - linebreak3(t) +func TestSimplelink(t *testing.T) { + input := "This is a [link](https://example.com)." + output := `This is a link.` + doonezerotest(t, input, output) +} + +func TestSimplelink2(t *testing.T) { + input := "See (http://example.com) for examples." + output := `See (http://example.com) for examples.` + doonezerotest(t, input, output) +} + +func TestWikilink(t *testing.T) { + input := "I watched [Hackers](https://en.wikipedia.org/wiki/Hackers_(film))" + output := `I watched Hackers` + doonezerotest(t, input, output) }