@@ -33,45 +33,22 @@ function wrapPromise<T>(func: promiseFunc<T>, time = 1000) {
3333 * @returns HTML formatted string compatible with Telegram
3434 */
3535async function markdownToHtml ( s : string ) {
36- // Configure marked to use GFM (GitHub Flavored Markdown)
37- marked . setOptions ( {
38- gfm : true ,
39- breaks : true ,
40- } ) ;
41-
4236 const parsed = await marked . parse ( s ) ;
43-
44- // Define HTML elements to remove (unsupported by Telegram)
45- const elementsToRemove = [ 'p' , 'ol' , 'ul' , 'li' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' , 'div' , 'span' , 'section' , 'article' ] ;
46-
47- let result = parsed ;
48-
49- // Remove opening and closing tags for each element
50- for ( const element of elementsToRemove ) {
51- // Handle tags with attributes (like <ol start="1">)
52- const openTagRegex = new RegExp ( `<${ element } [^>]*>` , 'g' ) ;
53- const closeTagRegex = new RegExp ( `</${ element } >` , 'g' ) ;
54-
55- result = result . replace ( openTagRegex , '' ) . replace ( closeTagRegex , '' ) ;
56- }
57-
58- // Properly format code blocks for Telegram
59- // First handle fenced code blocks with language specification
60- result = result . replace ( / < p r e > < c o d e c l a s s = " l a n g u a g e - ( [ ^ " ] + ) " > / g, '<pre><code>' ) ;
61-
62- // Ensure code blocks have proper spacing
63- result = result . replace ( / < \/ c o d e > < \/ p r e > / g, '</code></pre>\n' ) ;
64-
65- // Replace consecutive newlines with a single newline to avoid excessive spacing
66- result = result . replace ( / \n { 3 , } / g, '\n\n' ) ;
67-
68- // Convert links to Telegram-supported format
69- result = result . replace ( / < a h r e f = " ( [ ^ " ] + ) " [ ^ > ] * > ( [ ^ < ] + ) < \/ a > / g, '<a href="$1">$2</a>' ) ;
70-
71- // Handle special characters that Telegram might interpret incorrectly
72- result = result . replace ( / & l t ; / g, '<' ) . replace ( / & g t ; / g, '>' ) . replace ( / & a m p ; / g, '&' ) ;
73-
74- return result ;
37+ return parsed
38+ . replace ( / < p > / g, '' )
39+ . replace ( / < \/ p > / g, '' )
40+ . replace ( / < o l > / g, '' )
41+ . replace ( / < \/ o l > / g, '' )
42+ . replace ( / < u l > / g, '' )
43+ . replace ( / < \/ u l > / g, '' )
44+ . replace ( / < l i > / g, '' )
45+ . replace ( / < \/ l i > / g, '' )
46+ . replace ( / < h 1 > / g, '' )
47+ . replace ( / < \/ h 1 > / g, '' )
48+ . replace ( / < h 2 > / g, '' )
49+ . replace ( / < \/ h 2 > / g, '' )
50+ . replace ( / < h 3 > / g, '' )
51+ . replace ( / < \/ h 3 > / g, '' ) ;
7552}
7653
7754// Constants for system prompts
0 commit comments