Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* <ul>
* <li>{@code ```json ... ```}</li>
* <li>{@code ``` ... ```}</li>
* <li>{@code ```json ...}</li>
* <li>{@code ``` ...}</li>
* </ul>
*
* @author liugddx
Expand All @@ -41,7 +43,7 @@ public class MarkdownCodeBlockCleaner implements ResponseTextCleaner {
text = text.trim();

// Check for and remove triple backticks
if (text.startsWith("```") && text.endsWith("```")) {
if (text.startsWith("```")) {
String[] lines = text.split("\n", 2);
String firstLine = lines[0].trim();
if (lines.length > 1) {
Expand All @@ -58,7 +60,9 @@ public class MarkdownCodeBlockCleaner implements ResponseTextCleaner {
}

// Remove trailing ```
text = text.substring(0, text.length() - 3);
if (text.endsWith("```")) {
text = text.substring(0, text.length() - 3);
}

// Trim again to remove any potential whitespace
text = text.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,56 @@ void shouldNotModifyTextWithoutCodeBlocks() {
assertThat(cleaner.clean(input)).isEqualTo(input);
}

@Test
void shouldRemoveMultilineCodeBlockWithLanguageIdentifierWithoutClosingFence() {
MarkdownCodeBlockCleaner cleaner = new MarkdownCodeBlockCleaner();
String input = "```json\n{\"key\": \"value\"}";
String result = cleaner.clean(input);
assertThat(result).isEqualTo("{\"key\": \"value\"}");
}

@Test
void shouldRemoveMultilineCodeBlockWithoutLanguageIdentifierWithoutClosingFence() {
MarkdownCodeBlockCleaner cleaner = new MarkdownCodeBlockCleaner();
String input = "```\n{\"key\": \"value\"}";
String result = cleaner.clean(input);
assertThat(result).isEqualTo("{\"key\": \"value\"}");
}

@Test
void shouldRemoveSingleLineCodeBlockWithoutClosingFence() {
MarkdownCodeBlockCleaner cleaner = new MarkdownCodeBlockCleaner();
String input = "```{\"key\": \"value\"}";
String result = cleaner.clean(input);
assertThat(result).isEqualTo("{\"key\": \"value\"}");
}

@Test
void shouldRemoveMultilineJsonObjectWithoutClosingFence() {
MarkdownCodeBlockCleaner cleaner = new MarkdownCodeBlockCleaner();
String input = "```json\n{\n \"name\": \"Alice\",\n \"age\": 30\n}";
String result = cleaner.clean(input);
assertThat(result).isEqualTo("{\n \"name\": \"Alice\",\n \"age\": 30\n}");
}

@Test
void shouldRemoveCodeBlockWithoutClosingFenceAndTrailingWhitespace() {
MarkdownCodeBlockCleaner cleaner = new MarkdownCodeBlockCleaner();
String input = "```json\n{\"key\": \"value\"} ";
String result = cleaner.clean(input);
assertThat(result).isEqualTo("{\"key\": \"value\"}");
}

@Test
void shouldHandleMalformedInput() {
MarkdownCodeBlockCleaner cleaner = new MarkdownCodeBlockCleaner();
assertThat(cleaner.clean("`")).isEqualTo("`");
assertThat(cleaner.clean("``")).isEqualTo("``");
assertThat(cleaner.clean("```")).isEmpty();
assertThat(cleaner.clean("````")).isEqualTo("`");
assertThat(cleaner.clean("`````")).isEqualTo("``");
assertThat(cleaner.clean("``````")).isEmpty();
assertThat(cleaner.clean("```````")).isEqualTo("`");
}

}
Loading