get_child_indices func works

This commit is contained in:
bjt-user 2024-05-05 19:54:43 +02:00
parent 5125b7bc0a
commit 5776805400
2 changed files with 32 additions and 1 deletions

View File

@ -131,6 +131,6 @@ func main() {
} }
} }
my_children := get_children(0, headings) my_children := get_child_indices(0, headings)
fmt.Printf("%v\n", my_children) fmt.Printf("%v\n", my_children)
} }

31
tree.go
View File

@ -31,3 +31,34 @@ func get_children(header_index int, headings []heading) []heading {
return children return children
} }
func get_child_indices(header_index int, headings []heading) []int {
var children []int
parent_level := get_heading_level(headings[header_index].text)
next_level := get_heading_level(headings[header_index+1].text)
child_level := 0
if next_level <= parent_level {
return nil
} else {
child_level = next_level
}
cur_level := 0
for i := (header_index + 1); i < len(headings); i++ {
cur_level = get_heading_level(headings[i].text)
if cur_level == child_level {
children = append(children, i)
}
if cur_level < child_level {
break
}
}
return children
}