Files
go-conventionalcommit/paragraph.go
Jim Myhrberg ce4b06f67c feat(parser): implement RawMessage
The NewRawMessage function returns a RawMessage struct, which has broken
the given commit message down into separate lines, and also grouped the
lines into paragraphs.

This should make it easier to implement proper conventional commit
parser, linter, and formatter.
2021-08-09 21:03:22 +01:00

31 lines
738 B
Go

package conventionalcommit
import "bytes"
// Paragraph represents a textual paragraph defined as; A continuous sequence of
// textual lines which are not empty or and do not consist of only whitespace.
type Paragraph struct {
// Lines is a list of lines which collectively form a paragraph.
Lines Lines
}
func NewParagraphs(lines Lines) []*Paragraph {
r := []*Paragraph{}
paragraph := &Paragraph{Lines: Lines{}}
for _, line := range lines {
if len(bytes.TrimSpace(line.Content)) > 0 {
paragraph.Lines = append(paragraph.Lines, line)
} else if len(paragraph.Lines) > 0 {
r = append(r, paragraph)
paragraph = &Paragraph{Lines: Lines{}}
}
}
if len(paragraph.Lines) > 0 {
r = append(r, paragraph)
}
return r
}