Take the Markdown-format body of an issue and break it down by section header and the content directly below it. We can reasonably expect the correct format here if someone files an issue using the application template, but it will also gracefully handle when this format is not present. Note that th
(body string)
| 180 | // create an entry when there is content to be added; in other words, a section |
| 181 | // header without any content will not be added. |
| 182 | func (a *Application) extractSections(body string) map[string]string { |
| 183 | sections := make(map[string]string) |
| 184 | |
| 185 | lines := strings.Split(body, "\n") |
| 186 | var currentHeader string |
| 187 | contentBuilder := strings.Builder{} |
| 188 | |
| 189 | // For each line of the body content, it can either be a section's |
| 190 | // header or the content associated with that section's header. |
| 191 | for _, line := range lines { |
| 192 | trimmedLine := strings.TrimSpace(line) |
| 193 | |
| 194 | // If we're in a section and the content doesn't start with |
| 195 | // a header marker, append it to our content builder |
| 196 | if !strings.HasPrefix(trimmedLine, "### ") { |
| 197 | if currentHeader == "" { |
| 198 | continue |
| 199 | } |
| 200 | |
| 201 | contentBuilder.WriteString(line + "\n") |
| 202 | continue |
| 203 | } |
| 204 | |
| 205 | // The content has a header marker, so create a new |
| 206 | // section entry and prepare the content builder |
| 207 | if currentHeader != "" && contentBuilder.Len() > 0 { |
| 208 | sections[currentHeader] = strings.TrimSpace(contentBuilder.String()) |
| 209 | contentBuilder.Reset() |
| 210 | } |
| 211 | |
| 212 | currentHeader = strings.TrimSpace(trimmedLine[4:]) |
| 213 | } |
| 214 | |
| 215 | // Once the loop has completed check if there's a |
| 216 | // trailing section needing to be closed |
| 217 | if currentHeader != "" && contentBuilder.Len() > 0 { |
| 218 | sections[currentHeader] = strings.TrimSpace(contentBuilder.String()) |
| 219 | } |
| 220 | |
| 221 | return sections |
| 222 | } |
| 223 | |
| 224 | func (a *Application) stringSection(sectionName string, required bool, callbacks ...ValidatorCallback) string { |
| 225 | value, exists := a.sections[sectionName] |