this repo has no description
1/** 2 * Parse Slack mrkdwn formatting and convert to IRC-friendly plain text 3 */ 4export function parseSlackMarkdown(text: string): string { 5 let parsed = text; 6 7 // Replace channel mentions <#C123ABC|channel-name> or <#C123ABC> 8 parsed = parsed.replace(/<#[A-Z0-9]+\|([^>]+)>/g, "#$1"); 9 parsed = parsed.replace(/<#[A-Z0-9]+>/g, "#channel"); 10 11 // Replace links <http://example.com|text> or <http://example.com> 12 parsed = parsed.replace(/<(https?:\/\/[^|>]+)\|([^>]+)>/g, "$2 ($1)"); 13 parsed = parsed.replace(/<(https?:\/\/[^>]+)>/g, "$1"); 14 15 // Replace mailto links <mailto:email|text> 16 parsed = parsed.replace(/<mailto:([^|>]+)\|([^>]+)>/g, "$2 <$1>"); 17 parsed = parsed.replace(/<mailto:([^>]+)>/g, "$1"); 18 19 // Replace special mentions 20 parsed = parsed.replace(/<!here>/g, "@here"); 21 parsed = parsed.replace(/<!channel>/g, "@channel"); 22 parsed = parsed.replace(/<!everyone>/g, "@everyone"); 23 24 // Replace user group mentions <!subteam^GROUP_ID|handle> 25 parsed = parsed.replace(/<!subteam\^[A-Z0-9]+\|([^>]+)>/g, "@$1"); 26 parsed = parsed.replace(/<!subteam\^[A-Z0-9]+>/g, "@group"); 27 28 // Date formatting - just use fallback text 29 parsed = parsed.replace(/<!date\^[0-9]+\^[^|]+\|([^>]+)>/g, "$1"); 30 31 // Replace Slack bold *text* with IRC bold \x02text\x02 32 parsed = parsed.replace(/\*((?:[^\*]|\\\*)+)\*/g, "\x02$1\x02"); 33 34 // Replace Slack italic _text_ with IRC italic \x1Dtext\x1D 35 parsed = parsed.replace(/_((?:[^_]|\\_)+)_/g, "\x1D$1\x1D"); 36 37 // Replace Slack strikethrough ~text~ with plain text (IRC doesn't support strikethrough well) 38 parsed = parsed.replace(/~((?:[^~]|\\~)+)~/g, "$1"); 39 40 // Replace code blocks ```code``` with plain text 41 parsed = parsed.replace(/```([^`]+)```/g, "$1"); 42 43 // Replace inline code `code` with plain text 44 parsed = parsed.replace(/`([^`]+)`/g, "$1"); 45 46 // Handle block quotes - prefix with > 47 parsed = parsed.replace(/^>/gm, ">"); 48 49 // Unescape HTML entities 50 parsed = parsed.replace(/&amp;/g, "&"); 51 parsed = parsed.replace(/&lt;/g, "<"); 52 parsed = parsed.replace(/&gt;/g, ">"); 53 54 return parsed; 55} 56 57/** 58 * Parse IRC formatting codes and convert to Slack mrkdwn 59 */ 60export function parseIRCFormatting(text: string): string { 61 let parsed = text; 62 63 // IRC color codes - strip them (Slack doesn't support colors in the same way) 64 // \x03 followed by optional color codes 65 parsed = parsed.replace(/\x03(\d{1,2}(,\d{1,2})?)?/g, ""); 66 67 // IRC bold \x02text\x02 -> Slack bold *text* 68 parsed = parsed.replace(/\x02([^\x02]*)\x02/g, "*$1*"); 69 70 // IRC italic \x1D text\x1D -> Slack italic _text_ 71 parsed = parsed.replace(/\x1D([^\x1D]*)\x1D/g, "_$1_"); 72 73 // IRC underline \x1F text\x1F -> Slack doesn't have underline, use italic instead 74 parsed = parsed.replace(/\x1F([^\x1F]*)\x1F/g, "_$1_"); 75 76 // IRC reverse/inverse \x16 - strip it (Slack doesn't support) 77 parsed = parsed.replace(/\x16/g, ""); 78 79 // IRC reset \x0F - strip it 80 parsed = parsed.replace(/\x0F/g, ""); 81 82 // Escape special Slack characters that would be interpreted as formatting 83 parsed = parsed.replace(/&/g, "&amp;"); 84 parsed = parsed.replace(/</g, "&lt;"); 85 parsed = parsed.replace(/>/g, "&gt;"); 86 87 return parsed; 88}