extractCodeBlock extracts the content of the first fenced code block from text. Returns empty string if no code block is found.
(text string)
| 97 | // extractCodeBlock extracts the content of the first fenced code block from text. |
| 98 | // Returns empty string if no code block is found. |
| 99 | func extractCodeBlock(text string) string { |
| 100 | text = strings.TrimSpace(text) |
| 101 | start := strings.Index(text, "```") |
| 102 | if start < 0 { |
| 103 | return "" |
| 104 | } |
| 105 | afterOpen := start + 3 |
| 106 | // Skip optional language tag on the opening fence line. |
| 107 | if nl := strings.IndexByte(text[afterOpen:], '\n'); nl >= 0 { |
| 108 | afterOpen += nl + 1 |
| 109 | } else { |
| 110 | return "" |
| 111 | } |
| 112 | end := strings.Index(text[afterOpen:], "```") |
| 113 | if end < 0 { |
| 114 | return "" |
| 115 | } |
| 116 | return strings.TrimSpace(text[afterOpen : afterOpen+end]) |
| 117 | } |
no outgoing calls