renderContentAsJSONMarkdown tries to unmarshal the given content as JSON, wrap that content in a markdown JSON code block, and render it as markdown. If label is non-empty, it is rendered as leading text before and outside of the JSON block.
(label, content string, w io.Writer, io *iostreams.IOStreams)
| 273 | // If label is non-empty, it is rendered as leading text before and outside of |
| 274 | // the JSON block. |
| 275 | func renderContentAsJSONMarkdown(label, content string, w io.Writer, io *iostreams.IOStreams) error { |
| 276 | var contentAsJSON any |
| 277 | if err := json.Unmarshal([]byte(content), &contentAsJSON); err == nil { |
| 278 | marshaled, err := json.MarshalIndent(contentAsJSON, "", " ") |
| 279 | if err != nil { |
| 280 | return fmt.Errorf("failed to marshal JSON: %w", err) |
| 281 | } |
| 282 | |
| 283 | if label != "" { |
| 284 | if err := renderRawMarkdown(label, w, io); err != nil { |
| 285 | return fmt.Errorf("failed to render label: %w", err) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if err := renderFileContentAsMarkdown("output.json", string(marshaled), w, io); err != nil { |
| 290 | return fmt.Errorf("failed to render JSON: %w", err) |
| 291 | } |
| 292 | } |
| 293 | return nil |
| 294 | } |
| 295 | |
| 296 | // renderRawMarkdown renders the given raw markdown string to the given writer. |
| 297 | // Use for complete markdown content from tool calls that need no conversion. |
no test coverage detected