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
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
var re_link = regexp.MustCompile(`@?https?://[^\s"]+[\w/)]`)
|
|
||||||
var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`)
|
var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`)
|
||||||
|
|
||||||
func herdofemus(noise string) []Emu {
|
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_bigcoder = regexp.MustCompile("```\n?((?s:.*?))\n?```\n?")
|
||||||
var re_coder = regexp.MustCompile("`([^`]*)`")
|
var re_coder = regexp.MustCompile("`([^`]*)`")
|
||||||
var re_quoter = regexp.MustCompile(`(?m:^> (.*)\n?)`)
|
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 {
|
func markitzero(s string) string {
|
||||||
// prepare the 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_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
|
||||||
s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>")
|
s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>")
|
||||||
s = re_link.ReplaceAllStringFunc(s, linkreplacer)
|
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
|
// now restore the code blocks
|
||||||
s = re_coder.ReplaceAllStringFunc(s, func(s string) string {
|
s = re_coder.ReplaceAllStringFunc(s, func(s string) string {
|
||||||
|
@ -76,9 +79,14 @@ func markitzero(s string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func linkreplacer(url string) string {
|
func linkreplacer(url string) string {
|
||||||
if url[0] == '@' {
|
if url[0:2] == "](" {
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
prefix := ""
|
||||||
|
for !strings.HasPrefix(url, "http") {
|
||||||
|
prefix += url[0:1]
|
||||||
|
url = url[1:]
|
||||||
|
}
|
||||||
addparen := false
|
addparen := false
|
||||||
adddot := false
|
adddot := false
|
||||||
if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
|
if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
|
||||||
|
@ -96,5 +104,5 @@ func linkreplacer(url string) string {
|
||||||
if addparen {
|
if addparen {
|
||||||
url += ")"
|
url += ")"
|
||||||
}
|
}
|
||||||
return url
|
return prefix + url
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,40 +4,51 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func onetest(t *testing.T, input, output string) {
|
func doonezerotest(t *testing.T, input, output string) {
|
||||||
result := markitzero(input)
|
result := markitzero(input)
|
||||||
if result != output {
|
if result != output {
|
||||||
t.Errorf("\nexpected:\n%s\noutput:\n%s", output, result)
|
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`
|
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`
|
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"
|
input := "hello\n> a quote\na comment"
|
||||||
output := "hello<blockquote>a quote</blockquote><p>a 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"
|
input := "hello\n\n> a quote\n\na comment"
|
||||||
output := "hello<br><blockquote>a quote</blockquote><p>a 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?"
|
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?"
|
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) {
|
func TestSimplelink(t *testing.T) {
|
||||||
basictest(t)
|
input := "This is a [link](https://example.com)."
|
||||||
linebreak1(t)
|
output := `This is a <a class="mention u-url" href="https://example.com">link</a>.`
|
||||||
linebreak2(t)
|
doonezerotest(t, input, output)
|
||||||
linebreak3(t)
|
}
|
||||||
|
|
||||||
|
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