Skip to content

Commit

Permalink
refactor: looser fenced code block handling
Browse files Browse the repository at this point in the history
Fixes #602
  • Loading branch information
quantizor committed Nov 14, 2024
1 parent 69f815e commit 857809a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chilled-kings-retire.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3809,6 +3809,18 @@ Yeah boi
</pre>
`)
})

it('regression 602 - should treat anything following ``` as code until the closing pair', () => {
render(compiler('```\nfoo'))

expect(root.innerHTML).toMatchInlineSnapshot(`
<pre>
<code>
foo
</code>
</pre>
`)
})
})

describe('indented code blocks', () => {
Expand Down
2 changes: 1 addition & 1 deletion index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down

0 comments on commit 857809a

Please sign in to comment.