@@ -14,7 +14,10 @@ export default function Markdown({ text, className }: MarkdownProps) {
1414 function renderTextSegments ( text : string ) : ReactNode [ ] {
1515 let result : ReactNode [ ] = [ ]
1616
17- // Parse images: 
17+ // Process in order: image links, images, regular links, and then formatting
18+ const imageInsideLinkRegex = / \[ ! \[ ( [ ^ \] ] * ) \] \( ( [ ^ ) ] + ) \) \] \( ( [ ^ ) ] + ) \) / g
19+ // Handle mixed content within links: [text  more text](link_url)
20+ const mixedContentLinkRegex = / \[ ( [ ^ \] ] * ?! \[ [ ^ \] ] * ?\] \( [ ^ ) ] + ?\) [ ^ \] ] * ?) \] \( ( [ ^ ) ] + ) \) / g
1821 const imageRegex = / ! \[ ( [ ^ \] ] * ) \] \( ( [ ^ ) ] + ) \) / g
1922 const linkRegex = / \[ ( [ ^ \] ] + ) \] \( ( [ ^ ) ] + ) \) / g
2023 const codeRegex = / ` ( [ ^ ` ] + ) ` / g
@@ -33,6 +36,7 @@ export default function Markdown({ text, className }: MarkdownProps) {
3336 const str = segment
3437 let lastIndex = 0
3538 let match : RegExpExecArray | null
39+ regex . lastIndex = 0 // Reset regex for safety
3640 while ( ( match = regex . exec ( str ) ) !== null ) {
3741 // Add text before match
3842 if ( match . index > lastIndex ) {
@@ -56,13 +60,24 @@ export default function Markdown({ text, className }: MarkdownProps) {
5660 // Start with entire text as a single segment
5761 result = [ text ]
5862
59- // Apply in a certain order:
60- result = applyRegex ( result , imageRegex , ( m ) => < img alt = { m [ 1 ] } src = { m [ 2 ] } /> )
61- result = applyRegex ( result , linkRegex , ( m ) => < a href = { m [ 2 ] } > { m [ 1 ] } </ a > )
62- result = applyRegex ( result , codeRegex , ( m ) => < code > { m [ 1 ] } </ code > )
63- result = applyRegex ( result , boldRegex , ( m ) => < strong > { m [ 1 ] } </ strong > )
64- result = applyRegex ( result , italicRegex , ( m ) => < em > { m [ 1 ] } </ em > )
65- result = applyRegex ( result , underlineRegex , ( m ) => < u > { m [ 1 ] } </ u > )
63+ // Apply in a specific order to handle nested elements:
64+ // First handle image-inside-link pattern
65+ result = applyRegex ( result , imageInsideLinkRegex , ( m ) => < a href = { m [ 3 ] } key = { `imglink-${ m [ 3 ] } ` } >
66+ < img alt = { m [ 1 ] } src = { m [ 2 ] } key = { `img-in-link-${ m [ 2 ] } ` } />
67+ </ a > )
68+
69+ // Then handle mixed content links (with images and text)
70+ result = applyRegex ( result , mixedContentLinkRegex , ( m ) => < a href = { m [ 2 ] } key = { `mixed-${ m [ 2 ] } ` } > { parseInline ( m [ 1 ] ) } </ a > )
71+
72+ // Then handle regular images and links
73+ result = applyRegex ( result , imageRegex , ( m ) => < img key = { `img-${ m [ 2 ] } ` } alt = { m [ 1 ] } src = { m [ 2 ] } /> )
74+ result = applyRegex ( result , linkRegex , ( m ) => < a href = { m [ 2 ] } key = { `link-${ m [ 2 ] } ` } > { parseInline ( m [ 1 ] ) } </ a > )
75+
76+ // Finally handle text formatting
77+ result = applyRegex ( result , codeRegex , ( m ) => < code key = { `code-${ m . index } ` } > { m [ 1 ] } </ code > )
78+ result = applyRegex ( result , boldRegex , ( m ) => < strong key = { `bold-${ m . index } ` } > { m [ 1 ] } </ strong > )
79+ result = applyRegex ( result , italicRegex , ( m ) => < em key = { `italic-${ m . index } ` } > { m [ 1 ] } </ em > )
80+ result = applyRegex ( result , underlineRegex , ( m ) => < u key = { `underline-${ m . index } ` } > { m [ 1 ] } </ u > )
6681
6782 return result
6883 }
0 commit comments