diff --git a/README.md b/README.md index 3e7d5e3..bcc1c44 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,6 @@ This seems intended by the creators of the language. #### TODO -- count number of headings for each heading level -(loop through all headings, find the highest heading level, get all used levels\ -then create a slice or slice of maps and append after each deeper level discovered...) -- print heading tree +- print header tree - delph deeper into different Markdown implementations (like CommonMark) - add support for "Setext-style headers" diff --git a/main.go b/main.go index 152a459..0c8712d 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,6 @@ package main // TODO: read a file line by line -// use heading struct with -// line content -// line number -// heading level import ( "fmt" @@ -38,11 +34,12 @@ func get_heading_level(heading_text string) int { func print_toc(headings []heading) { tab_count := 0 for index, _ := range headings { + cur_level := get_heading_level(headings[index].text) + + headings[index].level = cur_level + if index > 0 { prev_level := get_heading_level(headings[index-1].text) - cur_level := get_heading_level(headings[index].text) - - headings[index].level = cur_level if cur_level > prev_level { tab_count += (cur_level - prev_level) @@ -57,6 +54,36 @@ func print_toc(headings []heading) { } } +func count_levels(headings []heading) map[string]int { + level_count := map[string]int{ + "header1": 0, + "header2": 0, + "header3": 0, + "header4": 0, + "header5": 0, + "header6": 0, + } + + for index, _ := range headings { + switch headings[index].level { + case 1: + level_count["header1"]++ + case 2: + level_count["header2"]++ + case 3: + level_count["header3"]++ + case 4: + level_count["header4"]++ + case 5: + level_count["header5"]++ + case 6: + level_count["header6"]++ + } + } + + return level_count +} + func main() { file_content_raw, err := os.ReadFile(os.Args[1]) @@ -77,4 +104,8 @@ func main() { } print_toc(headings) + + header_levels := count_levels(headings) + + fmt.Printf("%v\n", header_levels) }