all repos — honk @ ffd9736c4c5b4bd97cd3ad07d26079b7edd46687

my fork of honk

add [link](url) support to markdown
Ted Unangst tedu@tedunangst.com
Wed, 09 Oct 2019 14:03:16 -0400
commit

ffd9736c4c5b4bd97cd3ad07d26079b7edd46687

parent

137eb139f3cca9fe2d14967e79a9fe77b8cbf09a

3 files changed, 35 insertions(+), 17 deletions(-)

jump to
M fun.gofun.go

@@ -256,7 +256,6 @@ ID string

Name string } -var re_link = regexp.MustCompile(`@?https?://[^\s"]+[\w/)]`) var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`) func herdofemus(noise string) []Emu {
M markitzero.gomarkitzero.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 @@ s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")

s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3") s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>") s = re_link.ReplaceAllStringFunc(s, linkreplacer) + s = re_zerolink.ReplaceAllString(s, `<a class="mention u-url" href="$2">$1</a>`) // now restore the code blocks s = re_coder.ReplaceAllStringFunc(s, func(s string) string {

@@ -76,9 +79,14 @@ return s

} 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 @@ }

if addparen { url += ")" } - return url + return prefix + url }
M markitzero_test.gomarkitzero_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 <a class="mention u-url" href="https://example.com/">https://example.com/</a> with <b>bold</b> 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<blockquote>a quote</blockquote><p>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<br><blockquote>a quote</blockquote><p>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<br><pre><code>func(s string)</code></pre><p>does it go?" - onetest(t, input, output) + doonezerotest(t, input, output) +} + +func TestSimplelink(t *testing.T) { + input := "This is a [link](https://example.com)." + output := `This is a <a class="mention u-url" href="https://example.com">link</a>.` + doonezerotest(t, input, output) +} + +func TestSimplelink2(t *testing.T) { + input := "See (http://example.com) for examples." + output := `See (<a class="mention u-url" href="http://example.com">http://example.com</a>) for examples.` + doonezerotest(t, input, output) } -func TestMarkitzero(t *testing.T) { - basictest(t) - linebreak1(t) - linebreak2(t) - linebreak3(t) +func TestWikilink(t *testing.T) { + input := "I watched [Hackers](https://en.wikipedia.org/wiki/Hackers_(film))" + output := `I watched <a class="mention u-url" href="https://en.wikipedia.org/wiki/Hackers_(film)">Hackers</a>` + doonezerotest(t, input, output) }