Skip to content

Commit 857809a

Browse files
committed
refactor: looser fenced code block handling
Fixes #602
1 parent 69f815e commit 857809a

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

.changeset/chilled-kings-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'markdown-to-jsx': patch
3+
---
4+
5+
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.

index.compiler.spec.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,18 @@ Yeah boi
38093809
</pre>
38103810
`)
38113811
})
3812+
3813+
it('regression 602 - should treat anything following ``` as code until the closing pair', () => {
3814+
render(compiler('```\nfoo'))
3815+
3816+
expect(root.innerHTML).toMatchInlineSnapshot(`
3817+
<pre>
3818+
<code>
3819+
foo
3820+
</code>
3821+
</pre>
3822+
`)
3823+
})
38123824
})
38133825

38143826
describe('indented code blocks', () => {

index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const BLOCKQUOTE_TRIM_LEFT_MULTILINE_R = /^ *> ?/gm
183183
const BREAK_LINE_R = /^ {2,}\n/
184184
const BREAK_THEMATIC_R = /^(?:( *[-*_])){3,} *(?:\n *)+\n/
185185
const CODE_BLOCK_FENCED_R =
186-
/^\s*(`{3,}|~{3,}) *(\S+)?([^\n]*?)?\n([\s\S]+?)\s*\1 *(?:\n *)*\n?/
186+
/^(?: {1,3})?(`{3,}|~{3,}) *(\S+)? *([^\n]*?)?\n([\s\S]*?)(?:\1\n?|$)/
187187
const CODE_BLOCK_R = /^(?: {4}[^\n]+\n*)+(?:\n *)+\n?/
188188
const CODE_INLINE_R = /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/
189189
const CONSECUTIVE_NEWLINE_R = /^(?:\n *)*\n/

0 commit comments

Comments
 (0)