#!/usr/bin/env python3 # Required parameters: # @raycast.schemaVersion 1 # @raycast.title Convert Markdown to Telegram Format # @raycast.mode silent # Optional parameters: # @raycast.icon πŸ”„ # @raycast.packageName Conversions # @raycast.description Convert Markdown formatting to Telegram format, excluding processing inside code blocks or quotes # Documentation: # @raycast.author Maxim Borzov # @raycast.authorURL https://github.com/borzov import re import subprocess # Set environment variables and encoding env_vars = {'LANG': 'en_US.UTF-8'} encoding = 'utf-8' def paste(): """Read text from the clipboard.""" return subprocess.check_output('pbpaste', env=env_vars).decode(encoding) def copy(text): """Write text to the clipboard.""" process = subprocess.Popen('pbcopy', env=env_vars, stdin=subprocess.PIPE) process.communicate(text.encode(encoding)) def convert_markdown(text): """Convert Markdown formatting to Telegram format.""" lines = text.split('\n') converted_lines = [] in_code_block = False for line in lines: # Check if the line is a code block delimiter if line.startswith('```'): in_code_block = not in_code_block converted_lines.append(line) continue # Skip quotes and only format if not in a code block if not in_code_block and not line.startswith('>'): # Format headers with emojis and bold text if line.startswith('# '): line = f"⚫️ **{line[2:].strip()}**\n" elif line.startswith('## '): line = f"◾️ **{line[3:].strip()}**\n" elif line.startswith('### '): line = f"β–ͺ️ **{line[4:].strip()}**\n" elif line.startswith('#### '): line = f"πŸ”Ή **{line[5:].strip()}**\n" elif line.startswith('##### '): line = f"πŸ“Œ **{line[6:].strip()}**\n" elif line.startswith('###### '): line = f"πŸ”° **{line[7:].strip()}**\n" else: # Format bold text line = re.sub(r'(?')) and not next.startswith(('*', '-', '```', '|', '>')): result.append('') return '\n'.join(result) if __name__ == "__main__": try: markdown_text = paste() converted_text = convert_markdown(markdown_text) copy(converted_text) print("βœ… Markdown converted and copied to clipboard") except Exception as e: print(f"❌ Error during conversion: {str(e)}")