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