changeloguru/pkg/markdown/node.go

68 lines
1.1 KiB
Go
Raw Normal View History

2020-11-27 09:50:32 +00:00
package markdown
import "strings"
//https://guides.github.com/features/mastering-markdown/
const (
headerToken = '#'
defaultListToken = '-'
alternativeListToken = '*'
spaceToken = ' '
NewlineToken = '\n'
2020-11-27 09:50:32 +00:00
)
// Node is single markdown syntax representation
2020-11-27 09:50:32 +00:00
// Example: header, list, ...
type Node interface {
2020-11-27 11:02:33 +00:00
String() string
2020-11-27 09:50:32 +00:00
}
type header struct {
level int
text string
}
func NewHeader(level int, text string) Node {
2020-11-27 11:02:33 +00:00
return header{
level: level,
text: text,
}
}
func (h header) String() string {
2020-11-27 09:50:32 +00:00
var builder strings.Builder
for i := 0; i < h.level; i++ {
builder.WriteString(string(headerToken))
}
builder.WriteString(string(spaceToken))
text := strings.TrimSpace(h.text)
builder.WriteString(text)
return builder.String()
}
type listItem struct {
text string
}
func NewListItem(text string) Node {
2020-11-27 11:02:33 +00:00
return listItem{
text: text,
}
}
func (i listItem) String() string {
2020-11-27 09:50:32 +00:00
text := strings.TrimSpace(i.text)
return string(defaultListToken) + string(spaceToken) + text
}
func Equal(base1, base2 Node) bool {
return base1.String() == base2.String()
}