fix:Markdown blocks and code blocks

This commit is contained in:
arrowliu
2025-03-17 19:22:54 +08:00
parent a4f671a623
commit e94080281d
2 changed files with 16 additions and 9 deletions
+13 -6
View File
@@ -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
+3 -3
View File
@@ -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,
},
}