in cli/bpmetadata/markdown.go [32:115]
func getMdContent(content []byte, headLevel int, headOrder int, headTitle string, getContent bool) (*mdContent, error) {
mdDocument := markdown.Parse(content, nil)
orderCtr := 0
mdSections := mdDocument.GetChildren()
var foundHead bool
for _, section := range mdSections {
// if the first child is nil, it's a comment and we don't
// need to evaluate it
if ast.GetFirstChild(section) == nil {
continue
}
currLeaf := ast.GetFirstChild(section).AsLeaf()
switch sectionType := section.(type) {
case *ast.Heading:
foundHead = false
if headTitle == string(currLeaf.Literal) {
foundHead = true
}
if headLevel == sectionType.Level {
orderCtr++
}
if !getContent && (headOrder == orderCtr || foundHead) {
return &mdContent{
literal: string(currLeaf.Literal),
}, nil
}
case *ast.Paragraph:
if getContent && (headOrder == orderCtr || foundHead) {
// check if the content is a link
l := ast.GetLastChild(currLeaf.Parent)
lNode, isLink := l.(*ast.Link)
if isLink {
return &mdContent{
literal: string(ast.GetFirstChild(lNode).AsLeaf().Literal),
url: string(lNode.Destination),
}, nil
}
return &mdContent{
literal: string(currLeaf.Literal),
}, nil
}
case *ast.List:
if getContent && (headOrder == orderCtr || foundHead) {
var mdListItems []mdListItem
for _, c := range sectionType.Children {
var listItem mdListItem
// each item is a list with data and metadata about the list item
itemConfigs := ast.GetFirstChild(c).AsContainer().Children
// if the length of the child node is 1, it is a plain text list item
// if the length is greater the 1, it is a list item with a link
if len(itemConfigs) == 1 {
listItemText := string(itemConfigs[0].AsLeaf().Literal)
listItem = mdListItem{
text: listItemText,
}
} else if len(itemConfigs) > 1 {
// the second child node has the link data and metadata
listItemLink := itemConfigs[1].(*ast.Link)
listItemText := string(ast.GetFirstChild(listItemLink).AsLeaf().Literal)
listItem = mdListItem{
text: listItemText,
url: string(listItemLink.Destination),
}
}
mdListItems = append(mdListItems, listItem)
}
return &mdContent{
listItems: mdListItems,
}, nil
}
}
}
return nil, fmt.Errorf("unable to find md content")
}