diff --git a/.changeset/chilled-kings-retire.md b/.changeset/chilled-kings-retire.md new file mode 100644 index 00000000..da53c0dc --- /dev/null +++ b/.changeset/chilled-kings-retire.md @@ -0,0 +1,5 @@ +--- +'markdown-to-jsx': patch +--- + +Fenced code blocks are now tolerant to a missing closing sequence; this improves use in LLM scenarios where the code block markdown is being streamed into the editor in chunks. diff --git a/index.compiler.spec.tsx b/index.compiler.spec.tsx index dc70e9ba..d7296c8e 100644 --- a/index.compiler.spec.tsx +++ b/index.compiler.spec.tsx @@ -3809,6 +3809,18 @@ Yeah boi `) }) + + it('regression 602 - should treat anything following ``` as code until the closing pair', () => { + render(compiler('```\nfoo')) + + expect(root.innerHTML).toMatchInlineSnapshot(` +
+
+ foo
+
+
+ `)
+ })
})
describe('indented code blocks', () => {
diff --git a/index.tsx b/index.tsx
index f75f5394..b238a630 100644
--- a/index.tsx
+++ b/index.tsx
@@ -183,7 +183,7 @@ const BLOCKQUOTE_TRIM_LEFT_MULTILINE_R = /^ *> ?/gm
const BREAK_LINE_R = /^ {2,}\n/
const BREAK_THEMATIC_R = /^(?:( *[-*_])){3,} *(?:\n *)+\n/
const CODE_BLOCK_FENCED_R =
- /^\s*(`{3,}|~{3,}) *(\S+)?([^\n]*?)?\n([\s\S]+?)\s*\1 *(?:\n *)*\n?/
+ /^(?: {1,3})?(`{3,}|~{3,}) *(\S+)? *([^\n]*?)?\n([\s\S]*?)(?:\1\n?|$)/
const CODE_BLOCK_R = /^(?: {4}[^\n]+\n*)+(?:\n *)+\n?/
const CODE_INLINE_R = /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/
const CONSECUTIVE_NEWLINE_R = /^(?:\n *)*\n/