Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add support to skip adding br tags to links or code #392

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
@@ -1,5 +1,5 @@
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/bb5dc10c1d2645c0894fa6774300639b)](https://app.codacy.com/gh/tj-actions/auto-doc?utm_source=github.com\&utm_medium=referral\&utm_content=tj-actions/auto-doc\&utm_campaign=Badge_Grade_Settings)
![Coverage](https://img.shields.io/badge/Coverage-84.1%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-84.4%25-brightgreen)
[![Go Reference](https://pkg.go.dev/badge/github.com/tj-actions/auto-doc.svg)](https://pkg.go.dev/github.com/tj-actions/auto-doc)
[![Go Report Card](https://goreportcard.com/badge/github.com/tj-actions/auto-doc)](https://goreportcard.com/report/github.com/tj-actions/auto-doc)
[![CI](https://github.com/tj-actions/auto-doc/workflows/CI/badge.svg)](https://github.com/tj-actions/auto-doc/actions?query=workflow%3ACI)
Expand Down
10 changes: 8 additions & 2 deletions cmd/root.go
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io/ioutil"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -402,6 +403,11 @@ func wordWrap(s string, limit int) string {
if strings.TrimSpace(s) == "" {
return s
}
// compile regular expressions for Markdown links and code blocks and code
linkRegex := regexp.MustCompile(`\[.*]\(.*\)`)
codeBlockRegex := regexp.MustCompile(`\` + "```" + `.*` + "```" + `\s*`)
codeRegex := regexp.MustCompile("`.*`")

// convert string to slice
strSlice := strings.Fields(s)
currentLimit := limit
Expand All @@ -411,11 +417,12 @@ func wordWrap(s string, limit int) string {
for len(strSlice) >= 1 {
// convert slice/array back to string
// but insert <br> at specified limit
// unless the current slice contains a Markdown link or code block or code

if len(strSlice) < currentLimit {
currentLimit = len(strSlice)
result = result + strings.Join(strSlice[:currentLimit], " ")
} else if currentLimit == limit {
} else if currentLimit == limit && !linkRegex.MatchString(strings.Join(strSlice[:currentLimit], " ")) && !codeBlockRegex.MatchString(strings.Join(strSlice[:currentLimit], " ")) && !codeRegex.MatchString(strings.Join(strSlice[:currentLimit], " ")) {
result = result + strings.Join(strSlice[:currentLimit], " ") + "<br>"
} else {
result = result + strings.Join(strSlice[:currentLimit], " ")
Expand All @@ -430,7 +437,6 @@ func wordWrap(s string, limit int) string {
if len(strSlice) < currentLimit {
currentLimit = len(strSlice)
}

}
return result
}