Skip to content

Commit e20ce64

Browse files
makrusalibvisness
andauthored
Add custom linkify regex to exclude parenthesis at the end of link (#42)
* add linkify custom regex to excluding parenthesis in the end of link * Update src/parsing/parsing.go Co-authored-by: Ben Visness <bvisness@users.noreply.github.com> * Update src/parsing/parsing_test.go Co-authored-by: Ben Visness <bvisness@users.noreply.github.com> * Update src/parsing/parsing_test.go Co-authored-by: Ben Visness <bvisness@users.noreply.github.com> * remove unecesary test in the plaintext markdown test * Remove the new unnecessary `NotContains` test --------- Co-authored-by: Ben Visness <bvisness@users.noreply.github.com>
1 parent a6f79d2 commit e20ce64

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/parsing/parsing.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package parsing
22

33
import (
44
"bytes"
5+
"regexp"
56

67
"github.com/yuin/goldmark/parser"
78
"github.com/yuin/goldmark/renderer/html"
@@ -106,6 +107,9 @@ type MarkdownOptions struct {
106107
Education bool
107108
}
108109

110+
// modified from https://github.com/yuin/goldmark/blob/master/extension/linkify.go (urlRegexp)
111+
var customLinkifyURLRegex = regexp.MustCompile(`^(?:http|https|ftp)://[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]+(?::\d+)?(?:[/#?](?:[-a-zA-Z0-9@:%_+.~#$!?&/=;,'">\^{}\[\]` + "`" + `]|\([-a-zA-Z0-9@:%_+~#$!?&/=;'">\^{}\[\]` + "`" + `]|\)[-a-zA-Z0-9@:%_+~#$!?&/=;'">\^{}\[\]` + "`" + `])*)?`)
112+
109113
func makeGoldmark(rawHTML bool, opts ...goldmark.Option) goldmark.Markdown {
110114
// We need to re-create Goldmark's default parsers to disable HTML parsing.
111115
// Or enable it again. yay
@@ -131,6 +135,9 @@ func makeGoldmark(rawHTML bool, opts ...goldmark.Option) goldmark.Markdown {
131135
util.Prioritized(parser.NewAutoLinkParser(), 300),
132136
// util.Prioritized(parser.NewRawHTMLParser(), 400),
133137
util.Prioritized(parser.NewEmphasisParser(), 500),
138+
util.Prioritized(extension.NewLinkifyParser(
139+
extension.WithLinkifyURLRegexp(customLinkifyURLRegex),
140+
), 600),
134141
}
135142

136143
if rawHTML {

src/parsing/parsing_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,31 @@ And here's my favorite YouTube video:
3838
https://youtu.be/dQw4w9WgXcQ
3939
4040
I hope you like it as much as I do.
41+
42+
If you paste a link in parentheses (like https://handmade.network/), the site should not pick up the closing parenthesis as part of the link.
4143
`
4244
t.Run("Real post Markdown", func(t *testing.T) {
4345
html := ParseMarkdown(md, PostMarkdown)
4446
t.Log(html)
4547
assert.Contains(t, html, `<img src="coolimage.png"`)
4648
assert.Contains(t, html, "<iframe")
49+
assert.Contains(t, html, `<a href="https://handmade.network/">https://handmade.network/</a>`)
4750
})
4851
t.Run("Post edit preview Markdown", func(t *testing.T) {
4952
html := ParseMarkdown(md, PostEditPreviewMarkdown)
5053
t.Log(html)
5154
assert.Contains(t, html, `<img src="coolimage.png"`)
5255
assert.Contains(t, html, `<img src="https://img.youtube.com/vi/dQw4w9WgXcQ/hqdefault.jpg"`)
5356
assert.NotContains(t, html, "<iframe")
57+
assert.Contains(t, html, `<a href="https://handmade.network/">https://handmade.network/</a>`)
5458
})
5559
t.Run("Post preview Markdown", func(t *testing.T) {
5660
html := ParseMarkdown(md, PostPreviewMarkdown)
5761
t.Log(html)
5862
assert.Contains(t, html, `<img src="coolimage.png"`)
5963
assert.Contains(t, html, `<a href="https://youtu.be/dQw4w9WgXcQ"`)
6064
assert.NotContains(t, html, "<iframe")
65+
assert.Contains(t, html, `<a href="https://handmade.network/">https://handmade.network/</a>`)
6166
})
6267
t.Run("Plaintext Markdown", func(t *testing.T) {
6368
html := ParseMarkdown(md, PlaintextMarkdown)

0 commit comments

Comments
 (0)