-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from systemli/Add-support-for-Links-in-Bluesk…
…y-Posts ✨ Add support for Links in Bluesky Posts
- Loading branch information
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package util | ||
|
||
import "regexp" | ||
|
||
// ExtractURLs extracts URLs from a text. | ||
func ExtractURLs(text string) []string { | ||
urlRegex := regexp.MustCompile(`(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)`) | ||
return urlRegex.FindAllString(text, -1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExtractURL(t *testing.T) { | ||
text := "This is a text with a URL https://example.com" | ||
urls := ExtractURLs(text) | ||
|
||
assert.Equal(t, 1, len(urls)) | ||
assert.Equal(t, "https://example.com", urls[0]) | ||
|
||
text = "This is a text with a URL https://example.com and another URL http://example.org" | ||
urls = ExtractURLs(text) | ||
|
||
assert.Equal(t, 2, len(urls)) | ||
assert.Equal(t, "https://example.com", urls[0]) | ||
assert.Equal(t, "http://example.org", urls[1]) | ||
|
||
text = "This is a text without a URL" | ||
urls = ExtractURLs(text) | ||
|
||
assert.Equal(t, 0, len(urls)) | ||
|
||
text = "This is a text with a URL https://www.systemli.org/en/contact/" | ||
urls = ExtractURLs(text) | ||
|
||
assert.Equal(t, 1, len(urls)) | ||
assert.Equal(t, "https://www.systemli.org/en/contact/", urls[0]) | ||
|
||
text = "This is a text with a URL https://www.systemli.org/en/contact/?key=value" | ||
urls = ExtractURLs(text) | ||
|
||
assert.Equal(t, 1, len(urls)) | ||
assert.Equal(t, "https://www.systemli.org/en/contact/?key=value", urls[0]) | ||
} |