fix section parsing logic
This commit is contained in:
@@ -237,51 +237,47 @@ func parseSections(source []byte) []Section {
|
|||||||
reader := text.NewReader(source)
|
reader := text.NewReader(source)
|
||||||
doc := md.Parser().Parse(reader)
|
doc := md.Parser().Parse(reader)
|
||||||
|
|
||||||
var sections []Section
|
title := "no title"
|
||||||
var currentTitle string
|
sections := make([]Section, 0)
|
||||||
var currentStart int = -1
|
lastPos := 0
|
||||||
|
|
||||||
for child := doc.FirstChild(); child != nil; child = child.NextSibling() {
|
for child := doc.FirstChild(); child != nil; child = child.NextSibling() {
|
||||||
if child.Kind() == ast.KindHeading {
|
if child.Kind() == ast.KindHeading {
|
||||||
heading := child.(*ast.Heading)
|
if child.Lines().Len() <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if currentStart != -1 {
|
level := child.(*ast.Heading).Level
|
||||||
headingStart := heading.Lines().At(0).Start
|
headingStart := child.Lines().At(0).Start - level - 1
|
||||||
if headingStart > currentStart {
|
headingEnd := child.Lines().At(child.Lines().Len() - 1).Stop
|
||||||
sectionContentRaw := string(source[currentStart:headingStart])
|
|
||||||
sectionContent := strings.TrimSpace(sectionContentRaw)
|
contentEnd := headingStart
|
||||||
|
if lastPos < contentEnd {
|
||||||
|
// you have a heading and a content to accumulte over
|
||||||
|
contentsRaw := source[lastPos:contentEnd]
|
||||||
|
contents := strings.TrimSpace(string(contentsRaw))
|
||||||
|
hints := extractHints(contents)
|
||||||
sections = append(sections, Section{
|
sections = append(sections, Section{
|
||||||
Title: currentTitle,
|
Title: title,
|
||||||
Content: sectionContent,
|
Content: contents,
|
||||||
Hints: extractHints(sectionContent),
|
Hints: hints,
|
||||||
})
|
})
|
||||||
|
lastPos = headingEnd
|
||||||
|
}
|
||||||
|
// for the next heading
|
||||||
|
title = strings.TrimSpace(string(source[headingStart:headingEnd]))
|
||||||
|
lastPos = headingEnd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// last section
|
||||||
if heading.Lines().Len() > 0 {
|
if lastPos < len(source) {
|
||||||
start := heading.Lines().At(0).Start
|
contentsRaw := source[lastPos:]
|
||||||
stop := heading.Lines().At(heading.Lines().Len() - 1).Stop
|
contents := strings.TrimSpace(string(contentsRaw))
|
||||||
currentTitle = string(source[start:stop])
|
hints := extractHints(contents)
|
||||||
currentStart = stop
|
|
||||||
} else {
|
|
||||||
currentTitle = string(heading.Text(source))
|
|
||||||
currentStart = -1
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if currentStart == -1 {
|
|
||||||
currentStart = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if currentStart != -1 && currentStart < len(source) {
|
|
||||||
sectionContentRaw := string(source[currentStart:])
|
|
||||||
sectionContent := strings.TrimSpace(sectionContentRaw)
|
|
||||||
sections = append(sections, Section{
|
sections = append(sections, Section{
|
||||||
Title: currentTitle,
|
Title: title,
|
||||||
Content: sectionContent,
|
Content: contents,
|
||||||
Hints: extractHints(sectionContent),
|
Hints: hints,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user