code blocks will be ignored, added unit test
This commit is contained in:
parent
e9abea6b73
commit
779bd103c6
8
Makefile
8
Makefile
@ -1,5 +1,11 @@
|
||||
all:
|
||||
go run *.go tests/test.md
|
||||
go run main.go tree.go test_files/test.md
|
||||
|
||||
test:
|
||||
go run main.go tree.go test_files/test.md
|
||||
go run main.go tree.go test_files/README.md
|
||||
go run main.go tree.go test_files/weird_headers.md
|
||||
go run main.go tree.go test_files/audio.md
|
||||
|
||||
clean:
|
||||
rm -vf main
|
||||
|
19
main.go
19
main.go
@ -24,29 +24,38 @@ func is_heading(line string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func get_heading_level(heading_text string) int {
|
||||
func Get_heading_level(heading_text string) int {
|
||||
level := 0
|
||||
for i := 0; heading_text[i] == '#'; i++ {
|
||||
level += 1
|
||||
if i == (len(heading_text) - 1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return level
|
||||
}
|
||||
|
||||
func parse_file(file_name string) []heading {
|
||||
file_content_raw, err := os.ReadFile(file_name)
|
||||
|
||||
var headings []heading
|
||||
|
||||
file_content_raw, err := os.ReadFile(file_name)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
file_lines := strings.Split(string(file_content_raw), "\n")
|
||||
|
||||
is_codeblock := false
|
||||
|
||||
for index, value := range file_lines {
|
||||
if is_heading(value) {
|
||||
if strings.HasPrefix(value, "```") {
|
||||
is_codeblock = !is_codeblock
|
||||
}
|
||||
|
||||
if !is_codeblock && is_heading(value) {
|
||||
headings = append(
|
||||
headings, heading{value, get_heading_level(value), (index + 1)})
|
||||
headings, heading{value, Get_heading_level(value), (index + 1)})
|
||||
}
|
||||
}
|
||||
return headings
|
||||
|
26
main_test.go
Normal file
26
main_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGet_heading_level(t *testing.T) {
|
||||
level := Get_heading_level("# foo")
|
||||
|
||||
if level < 0 || level > 6 {
|
||||
log.Fatal("Level is not between 0 and 6.")
|
||||
}
|
||||
|
||||
level = Get_heading_level("not a header")
|
||||
|
||||
if level != 0 {
|
||||
log.Fatal("A non header should be level 0!")
|
||||
}
|
||||
|
||||
level = Get_heading_level("#")
|
||||
|
||||
if level < 0 || level > 6 {
|
||||
log.Fatal("Level is not between 0 and 6.")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user