Skip to content

Commit 2bf1454

Browse files
committed
fix markdownToHtml, use the old one
1 parent 73ecf49 commit 2bf1454

File tree

1 file changed

+15
-38
lines changed

1 file changed

+15
-38
lines changed

packages/worker/src/worker.ts

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,45 +33,22 @@ function wrapPromise<T>(func: promiseFunc<T>, time = 1000) {
3333
* @returns HTML formatted string compatible with Telegram
3434
*/
3535
async 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(/<pre><code class="language-([^"]+)">/g, '<pre><code>');
61-
62-
// Ensure code blocks have proper spacing
63-
result = result.replace(/<\/code><\/pre>/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 href="([^"]+)"[^>]*>([^<]+)<\/a>/g, '<a href="$1">$2</a>');
70-
71-
// Handle special characters that Telegram might interpret incorrectly
72-
result = result.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&');
73-
74-
return result;
37+
return parsed
38+
.replace(/<p>/g, '')
39+
.replace(/<\/p>/g, '')
40+
.replace(/<ol>/g, '')
41+
.replace(/<\/ol>/g, '')
42+
.replace(/<ul>/g, '')
43+
.replace(/<\/ul>/g, '')
44+
.replace(/<li>/g, '')
45+
.replace(/<\/li>/g, '')
46+
.replace(/<h1>/g, '')
47+
.replace(/<\/h1>/g, '')
48+
.replace(/<h2>/g, '')
49+
.replace(/<\/h2>/g, '')
50+
.replace(/<h3>/g, '')
51+
.replace(/<\/h3>/g, '');
7552
}
7653

7754
// Constants for system prompts

0 commit comments

Comments
 (0)