diff --git a/outputparser/makrdown_parser.go b/outputparser/makrdown_parser.go index ae047bdc..eaec5abe 100644 --- a/outputparser/makrdown_parser.go +++ b/outputparser/makrdown_parser.go @@ -25,18 +25,25 @@ var _ schema.OutputParser[any] = Markdown{} // parse extracts content from markdown code blocks in the given text. func (p Markdown) parse(text string) (string, error) { - // Extract content from ```markdown ... ``` blocks - _, afterStart, ok := strings.Cut(text, "```markdown\n") - if !ok { + // Find the starting ```markdown + startIdx := strings.Index(text, "```markdown\n") + if startIdx == -1 { return "", ParseError{Text: text, Reason: "no ```markdown at start of output"} } - content, _, ok := strings.Cut(afterStart, "```") - if !ok { + // Move to the content starting position + contentStart := startIdx + len("```markdown\n") + + // Find the last ``` + lastBacktickIdx := strings.LastIndex(text, "```") + if lastBacktickIdx <= contentStart { return "", ParseError{Text: text, Reason: "no closing ``` at end of output"} } - // Trim whitespace from content + // Extract the content + content := text[contentStart:lastBacktickIdx] + + // Trim whitespace content = strings.TrimSpace(content) return content, nil diff --git a/outputparser/markdown_parser_test.go b/outputparser/markdown_parser_test.go index 229ce564..8d3fd1d7 100644 --- a/outputparser/markdown_parser_test.go +++ b/outputparser/markdown_parser_test.go @@ -40,9 +40,9 @@ func TestMarkdown(t *testing.T) { hasError: false, }, { - name: "Multiple markdown blocks", - input: "First block:\n```markdown\nContent 1\n```\n\nSecond block:\n```markdown\nContent 2\n```", - expected: "Content 1", + name: "Markdown blocks and code blocks", + input: "First block:\n```markdown\nContent 1\n\nCode block:\n```csharp\nContent 2\n``` 13456```", + expected: "Content 1\n\nCode block:\n```csharp\nContent 2\n``` 13456", hasError: false, }, }