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.
This commit is contained in:
2021-08-08 20:26:55 +01:00
parent 99a28a0346
commit ce4b06f67c
8 changed files with 1771 additions and 0 deletions

30
paragraph.go Normal file
View File

@@ -0,0 +1,30 @@
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
}