@@ -68,8 +68,8 @@ func separateFrontmatter(readmeText string) (string, string, error) {
6868
6969var readmeHeaderRe = regexp .MustCompile ("^(#{1,})(\\ s*)" )
7070
71- // Todo: This is a little chaotic, and might have some risks of false positives.
72- // Might be better to bring in an
71+ // Todo: This seems to work okay for now, but the really proper way of doing
72+ // this is by parsing this as an AST, and then checking the resulting nodes
7373func validateReadmeBody (body string ) []error {
7474 trimmed := strings .TrimSpace (body )
7575 var errs []error
@@ -82,29 +82,39 @@ func validateReadmeBody(body string) []error {
8282 return errs
8383 }
8484
85- lineNum := 0
86- lastHeaderLevel := 0
85+ latestHeaderLevel := 0
8786 foundFirstH1 := false
87+ isInCodeBlock := false
8888
8989 lineScanner := bufio .NewScanner (strings .NewReader (trimmed ))
9090 for lineScanner .Scan () {
91- lineNum ++
9291 nextLine := lineScanner .Text ()
9392
93+ // Have to check this because a lot of programming languages support #
94+ // comments (including Terraform), and without any context, there's no
95+ // way to tell the difference between a markdown header and code comment
96+ if strings .HasPrefix (nextLine , "```" ) {
97+ isInCodeBlock = ! isInCodeBlock
98+ continue
99+ }
100+ if isInCodeBlock {
101+ continue
102+ }
103+
94104 headerGroups := readmeHeaderRe .FindStringSubmatch (nextLine )
95105 if headerGroups == nil {
96106 continue
97107 }
98108
99109 spaceAfterHeader := headerGroups [2 ]
100110 if spaceAfterHeader == "" {
101- errs = append (errs , fmt . Errorf ( "line %d: header does not have space between header characters and main header text", lineNum ))
111+ errs = append (errs , errors . New ( " header does not have space between header characters and main header text" ))
102112 }
103113
104114 nextHeaderLevel := len (headerGroups [1 ])
105115 if nextHeaderLevel == 1 && ! foundFirstH1 {
106116 foundFirstH1 = true
107- lastHeaderLevel = 1
117+ latestHeaderLevel = 1
108118 continue
109119 }
110120
@@ -115,21 +125,21 @@ func validateReadmeBody(body string) []error {
115125 break
116126 }
117127 if nextHeaderLevel > 6 {
118- errs = append (errs , fmt .Errorf ("line %d: README/HTML files cannot have headers exceed level 6 (found level %d)" , lineNum , nextHeaderLevel ))
128+ errs = append (errs , fmt .Errorf ("README/HTML files cannot have headers exceed level 6 (found level %d)" , nextHeaderLevel ))
119129 break
120130 }
121131
122132 // This is something we need to enforce for accessibility, not just for
123133 // the Registry website, but also when users are viewing the README
124134 // files in the GitHub web view
125- if nextHeaderLevel > lastHeaderLevel && nextHeaderLevel != (lastHeaderLevel + 1 ) {
126- errs = append (errs , fmt .Errorf ("line %d: headers are not allowed to increase more than 1 level at a time" , lineNum ))
135+ if nextHeaderLevel > latestHeaderLevel && nextHeaderLevel != (latestHeaderLevel + 1 ) {
136+ errs = append (errs , fmt .Errorf ("headers are not allowed to increase more than 1 level at a time" ))
127137 continue
128138 }
129139
130140 // As long as the above condition passes, there's no problems with
131141 // going up a header level or going down 1+ header levels
132- lastHeaderLevel = nextHeaderLevel
142+ latestHeaderLevel = nextHeaderLevel
133143 }
134144
135145 return errs
0 commit comments