add [link](url) support to markdown
This commit is contained in:
parent
d0606f1b42
commit
d1473997ce
1
fun.go
1
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 {
|
||||
|
|
|
@ -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<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 @@ 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
|
||||
}
|
||||
|
|
|
@ -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 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 <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 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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue