Repository navigation
Expand file tree
/
Copy pathmarkdown.ts
More file actions
98 lines (86 loc) · 2.64 KB
/
Copy pathmarkdown.ts
File metadata and controls
98 lines (86 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Convert markdown to Telegram-compatible HTML
* Supports: bold, italic, code, code blocks, links
*/
// Escape HTML special characters
function escapeHtml(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}
// Convert markdown to Telegram HTML
export function markdownToHtml(text: string): string {
// First, extract and preserve code blocks to prevent processing inside them
const codeBlocks: string[] = [];
let processed = text.replace(
/```(\w*)\n?([\s\S]*?)```/g,
(_, _lang, code) => {
const index = codeBlocks.length;
// Escape HTML inside code blocks
codeBlocks.push(`<pre>${escapeHtml(code.trim())}</pre>`);
return `__CODE_BLOCK_${index}__`;
},
);
// Extract inline code
const inlineCodes: string[] = [];
processed = processed.replace(/`([^`]+)`/g, (_, code) => {
const index = inlineCodes.length;
inlineCodes.push(`<code>${escapeHtml(code)}</code>`);
return `__INLINE_CODE_${index}__`;
});
// Escape HTML in remaining text
processed = escapeHtml(processed);
// Convert markdown formatting
// Bold: **text** or __text__
processed = processed.replace(/\*\*([^*]+)\*\*/g, "<b>$1</b>");
processed = processed.replace(/__([^_]+)__/g, "<b>$1</b>");
// Italic: *text* or _text_ (but not inside words)
processed = processed.replace(
/(?<![a-zA-Z])\*([^*]+)\*(?![a-zA-Z])/g,
"<i>$1</i>",
);
processed = processed.replace(
/(?<![a-zA-Z])_([^_]+)_(?![a-zA-Z])/g,
"<i>$1</i>",
);
// Strikethrough: ~~text~~
processed = processed.replace(/~~([^~]+)~~/g, "<s>$1</s>");
// Links: [text](url)
processed = processed.replace(
/\[([^\]]+)\]\(([^)]+)\)/g,
'<a href="$2">$1</a>',
);
// Restore code blocks
for (let i = 0; i < codeBlocks.length; i++) {
processed = processed.replace(`__CODE_BLOCK_${i}__`, codeBlocks[i]);
}
// Restore inline code
for (let i = 0; i < inlineCodes.length; i++) {
processed = processed.replace(`__INLINE_CODE_${i}__`, inlineCodes[i]);
}
return processed;
}
// Strip all markdown formatting to plain text
export function stripMarkdown(text: string): string {
return (
text
// Remove code blocks
.replace(/```[\s\S]*?```/g, (match) => {
const code = match.replace(/```\w*\n?/, "").replace(/```$/, "");
return code.trim();
})
// Remove inline code backticks
.replace(/`([^`]+)`/g, "$1")
// Remove bold
.replace(/\*\*([^*]+)\*\*/g, "$1")
.replace(/__([^_]+)__/g, "$1")
// Remove italic
.replace(/\*([^*]+)\*/g, "$1")
.replace(/_([^_]+)_/g, "$1")
// Remove strikethrough
.replace(/~~([^~]+)~~/g, "$1")
// Convert links to just text
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
);
}