extractCodeBlock extracts the content of the first fenced code block from text. Returns empty string if no code block is found.
(text string)
| 80 | // extractCodeBlock extracts the content of the first fenced code block from text. |
| 81 | // Returns empty string if no code block is found. |
| 82 | func extractCodeBlock(text string) string { |
| 83 | text = strings.TrimSpace(text) |
| 84 | start := strings.Index(text, "```") |
| 85 | if start < 0 { |
| 86 | return "" |
| 87 | } |
| 88 | afterOpen := start + 3 |
| 89 | // Skip optional language tag on the opening fence line. |
| 90 | if nl := strings.IndexByte(text[afterOpen:], '\n'); nl >= 0 { |
| 91 | afterOpen += nl + 1 |
| 92 | } else { |
| 93 | return "" |
| 94 | } |
| 95 | end := strings.Index(text[afterOpen:], "```") |
| 96 | if end < 0 { |
| 97 | return "" |
| 98 | } |
| 99 | return strings.TrimSpace(text[afterOpen : afterOpen+end]) |
| 100 | } |
no outgoing calls