2020-11-10 10:04:13 +00:00
|
|
|
package changelog
|
|
|
|
|
|
|
|
import (
|
2020-11-10 10:36:50 +00:00
|
|
|
"strings"
|
2020-11-10 10:04:13 +00:00
|
|
|
"time"
|
|
|
|
|
2021-05-05 08:11:54 +00:00
|
|
|
"github.com/haunt98/changeloguru/internal/convention"
|
2021-05-05 09:12:59 +00:00
|
|
|
"github.com/haunt98/markdown-go"
|
2020-11-10 10:04:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-27 17:43:19 +00:00
|
|
|
firstLevel = 1
|
|
|
|
secondLevel = 2
|
|
|
|
thirdLevel = 3
|
2020-11-10 10:04:13 +00:00
|
|
|
)
|
|
|
|
|
2021-04-15 04:28:04 +00:00
|
|
|
func GenerateMarkdown(commits []convention.Commit, scopes map[string]struct{}, version string, when time.Time) []markdown.Node {
|
2021-06-16 17:56:10 +00:00
|
|
|
filteredCommits := filter(commits, scopes)
|
|
|
|
if filteredCommits == nil {
|
2020-11-10 17:06:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:56:10 +00:00
|
|
|
addedNodes := convertToListMarkdownNodes(filteredCommits[addedType])
|
|
|
|
fixedNodes := convertToListMarkdownNodes(filteredCommits[fixedType])
|
|
|
|
othersNodes := convertToListMarkdownNodes(filteredCommits[othersType])
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-06-16 17:56:10 +00:00
|
|
|
// 4 = 3 type header + 1 version header
|
|
|
|
nodes := make([]markdown.Node, 0, len(addedNodes)+len(fixedNodes)+len(othersNodes)+4)
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-06-16 17:56:10 +00:00
|
|
|
// Adding each type
|
2021-04-14 10:06:48 +00:00
|
|
|
|
2021-06-16 17:56:10 +00:00
|
|
|
if len(addedNodes) != 0 {
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append(nodes, markdown.NewHeader(thirdLevel, addedType))
|
2021-06-16 17:56:10 +00:00
|
|
|
nodes = append(nodes, addedNodes...)
|
2020-11-10 10:59:15 +00:00
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-06-16 17:56:10 +00:00
|
|
|
if len(fixedNodes) != 0 {
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append(nodes, markdown.NewHeader(thirdLevel, fixedType))
|
2021-06-16 17:56:10 +00:00
|
|
|
nodes = append(nodes, fixedNodes...)
|
2020-11-10 10:59:15 +00:00
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-06-16 17:56:10 +00:00
|
|
|
if len(othersNodes) != 0 {
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append(nodes, markdown.NewHeader(thirdLevel, othersType))
|
2021-06-16 17:56:10 +00:00
|
|
|
nodes = append(nodes, othersNodes...)
|
2020-11-10 10:59:15 +00:00
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-06-16 17:56:10 +00:00
|
|
|
// Adding title
|
|
|
|
versionHeader := generateVersionHeaderValue(version, when)
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append([]markdown.Node{
|
|
|
|
markdown.NewHeader(firstLevel, title),
|
2021-04-15 04:59:01 +00:00
|
|
|
markdown.NewHeader(secondLevel, versionHeader),
|
2021-04-14 10:06:48 +00:00
|
|
|
}, nodes...)
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
return nodes
|
|
|
|
}
|
2020-11-10 10:36:50 +00:00
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
func ParseMarkdown(data string) []markdown.Node {
|
2021-05-31 10:37:24 +00:00
|
|
|
lines := strings.Split(data, "\n\n")
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes := markdown.Parse(lines)
|
2020-11-10 10:36:50 +00:00
|
|
|
|
2021-04-26 02:54:31 +00:00
|
|
|
// Remove title
|
2021-04-14 10:06:48 +00:00
|
|
|
if len(nodes) > 0 && markdown.Equal(nodes[0], markdown.NewHeader(firstLevel, title)) {
|
|
|
|
nodes = nodes[1:]
|
2020-11-10 10:36:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
return nodes
|
2020-11-10 10:04:13 +00:00
|
|
|
}
|
2021-06-16 17:56:10 +00:00
|
|
|
|
|
|
|
func convertToListMarkdownNodes(commits []convention.Commit) []markdown.Node {
|
|
|
|
result := make([]markdown.Node, 0, len(commits))
|
|
|
|
|
|
|
|
for _, commit := range commits {
|
|
|
|
result = append(result, markdown.NewListItem(commit.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|