1
1
import { marked } from "marked" ;
2
2
import hljs from "highlight.js" ;
3
3
import { markedHighlight } from "marked-highlight" ;
4
+ import type { Link } from "marked" ;
4
5
5
6
marked . use ( markedHighlight ( {
6
7
highlight : ( code , lang ) => {
@@ -21,10 +22,10 @@ const renderer = new marked.Renderer();
21
22
const originalLinkRenderer = renderer . link . bind ( renderer ) ;
22
23
23
24
// Customize the link renderer to add icons
24
- renderer . link = ( href : string , title : string | null | undefined , text : string ) => {
25
- const originalLink = originalLinkRenderer ( href , title , text ) ;
25
+ renderer . link = ( { href , title, text } : Link ) => {
26
+ if ( ! href ) return text ;
26
27
27
- if ( ! href ) return originalLink ;
28
+ const linkHtml = originalLinkRenderer ( { href, title , text } ) ;
28
29
29
30
let iconSvg = '' ;
30
31
@@ -40,9 +41,35 @@ renderer.link = (href: string, title: string | null | undefined, text: string) =
40
41
}
41
42
42
43
// Insert the icon after the link
43
- return originalLink . slice ( 0 , - 4 ) + iconSvg + '</a>' ;
44
+ return linkHtml . slice ( 0 , - 4 ) + iconSvg + '</a>' ;
44
45
} ;
45
46
46
- export function parseMarkdownContent ( content : string ) : string {
47
- return marked ( content ) ;
47
+ export function processNestedCodeBlocks ( content : string ) : { processedContent : string ; langtags : string [ ] } {
48
+ const langtags : string [ ] = [ ] ;
49
+ const codeBlockRegex = / ` ` ` ( \w + ) ? \n ( [ \s \S ] * ?) ` ` ` / g;
50
+ let match ;
51
+ let lastIndex = 0 ;
52
+ let processedContent = '' ;
53
+
54
+ while ( ( match = codeBlockRegex . exec ( content ) ) !== null ) {
55
+ const [ fullMatch , lang ] = match ;
56
+ if ( lang ) langtags . push ( lang ) ;
57
+ processedContent += content . slice ( lastIndex , match . index ) + fullMatch ;
58
+ lastIndex = match . index + fullMatch . length ;
59
+ }
60
+
61
+ processedContent += content . slice ( lastIndex ) ;
62
+ return { processedContent, langtags } ;
48
63
}
64
+
65
+ export function transformThinkingTags ( content : string ) : string {
66
+ return content . replace (
67
+ / < t h i n k i n g > ( [ \s \S ] * ?) < \/ t h i n k i n g > / g,
68
+ '<details><summary>💭 Thinking</summary>\n\n$1\n\n</details>'
69
+ ) ;
70
+ }
71
+
72
+ export function parseMarkdownContent ( content : string ) : string {
73
+ const transformedContent = transformThinkingTags ( content ) ;
74
+ return marked ( transformedContent , { renderer } ) ;
75
+ }
0 commit comments