-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
readme improve tests improve tests bump dependencies
- Loading branch information
1 parent
2be5a1a
commit 6fbbb3e
Showing
6 changed files
with
234 additions
and
20 deletions.
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,27 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"regexp" | ||
) | ||
|
||
var markdownLinkRegex = regexp.MustCompile(`\[\s*(\S.*?)\s*\]\(`) | ||
|
||
func CleanupMarkdownLinks(r io.Reader, w io.Writer) error { | ||
buf, err := io.ReadAll(r) | ||
if err != nil { | ||
return fmt.Errorf("CleanupMarkdownLinks: failed to read input: %w", err) | ||
} | ||
|
||
cleaned := markdownLinkRegex.ReplaceAllFunc(buf, func(match []byte) []byte { | ||
return markdownLinkRegex.ReplaceAll(match, []byte("[$1](")) | ||
}) | ||
|
||
_, err = w.Write(cleaned) | ||
if err != nil { | ||
return fmt.Errorf("CleanupMarkdownLinks: failed to write output: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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,131 @@ | ||
package core | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestCleanupMarkdownLinks(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "No links", | ||
input: ` | ||
This is some text. | ||
This more text. | ||
Here is a link https://google.com. | ||
`, | ||
expected: ` | ||
This is some text. | ||
This more text. | ||
Here is a link https://google.com. | ||
`, | ||
}, | ||
{ | ||
name: "Single link with extra space", | ||
input: ` | ||
[ Google](https://google.com) | ||
This is some text | ||
[ | ||
Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API") | ||
This more text. | ||
Here is a link https://google.com. | ||
`, | ||
expected: ` | ||
[Google](https://google.com) | ||
This is some text | ||
[Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API") | ||
This more text. | ||
Here is a link https://google.com. | ||
`, | ||
}, | ||
{ | ||
name: "Multiple links with extra space", | ||
input: ` | ||
[Google](https://google.com) | ||
This is some text | ||
[ | ||
Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API") | ||
This more text. | ||
[ | ||
Another link with space](https://example.com) | ||
Here is a link https://google.com. | ||
`, | ||
expected: ` | ||
[Google](https://google.com) | ||
This is some text | ||
[Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API") | ||
This more text. | ||
[Another link with space](https://example.com) | ||
Here is a link https://google.com. | ||
`, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
input := strings.NewReader(tc.input) | ||
var output bytes.Buffer | ||
err := CleanupMarkdownLinks(input, &output) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
result := output.String() | ||
if diff := cmp.Diff(tc.expected, result); diff != "" { | ||
t.Errorf("Unexpected result (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCleanupMarkdownLinksErrors(t *testing.T) { | ||
t.Run("Read error", func(t *testing.T) { | ||
errReader := &errorReader{err: errors.New("read error")} | ||
var output bytes.Buffer | ||
err := CleanupMarkdownLinks(errReader, &output) | ||
if err == nil { | ||
t.Fatal("Expected an error, got nil") | ||
} | ||
if !strings.Contains(err.Error(), "CleanupMarkdownLinks: failed to read input: read error") { | ||
t.Errorf("Unexpected error message: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Write error", func(t *testing.T) { | ||
input := strings.NewReader("some input") | ||
errWriter := &errorWriter{err: errors.New("write error")} | ||
err := CleanupMarkdownLinks(input, errWriter) | ||
if err == nil { | ||
t.Fatal("Expected an error, got nil") | ||
} | ||
if !strings.Contains(err.Error(), "CleanupMarkdownLinks: failed to write output: write error") { | ||
t.Errorf("Unexpected error message: %v", err) | ||
} | ||
}) | ||
} | ||
|
||
type errorReader struct { | ||
err error | ||
} | ||
|
||
func (e *errorReader) Read(p []byte) (n int, err error) { | ||
return 0, e.err | ||
} | ||
|
||
type errorWriter struct { | ||
err error | ||
} | ||
|
||
func (e *errorWriter) Write(p []byte) (n int, err error) { | ||
return 0, e.err | ||
} |
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