stripDiffFormat implements a primitive conversion from a diff string to a plain text representation by removing diff-specific formatting.
(diff string)
| 331 | // stripDiffFormat implements a primitive conversion from a diff string to a |
| 332 | // plain text representation by removing diff-specific formatting. |
| 333 | func stripDiffFormat(diff string) string { |
| 334 | lines := strings.Split(diff, "\n") |
| 335 | |
| 336 | // Find where the hunk header ends. |
| 337 | hunkEndIndex := -1 |
| 338 | for i, line := range lines { |
| 339 | if strings.HasPrefix(line, "@@") { |
| 340 | hunkEndIndex = i |
| 341 | break |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // This isn't a diff. |
| 346 | if hunkEndIndex == -1 { |
| 347 | return diff |
| 348 | } |
| 349 | |
| 350 | // Removing hunk header. |
| 351 | lines = lines[hunkEndIndex+1:] |
| 352 | |
| 353 | // Strip the leading + and - from lines, if they exist. |
| 354 | var stripped []string |
| 355 | for _, line := range lines { |
| 356 | if strings.HasPrefix(line, "+") || strings.HasPrefix(line, "-") { |
| 357 | stripped = append(stripped, line[1:]) |
| 358 | } else { |
| 359 | stripped = append(stripped, line) |
| 360 | } |
| 361 | } |
| 362 | return strings.Join(stripped, "\n") |
| 363 | } |
| 364 | |
| 365 | // renderFileContentAsMarkdown renders the given content as markdown |
| 366 | // based on the file extension of the path. |