Skip to content

Commit

Permalink
Refactor convertToSlackMarkdown function to simplify markdown conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
schobele committed Dec 13, 2024
1 parent 8ce800f commit fabc8a7
Showing 1 changed file with 2 additions and 45 deletions.
47 changes: 2 additions & 45 deletions src/slackbot/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,21 @@ export function convertToSlackMarkdown(markdown: string) {

let slackText = markdown;

// Replace HTML entities for special characters
const htmlEntities = {
"&": "&",
"<": "&lt;",
">": "&gt;",
};

// Escape special characters first
slackText = slackText.replace(/[&<>]/g, (char) => htmlEntities[char]);

// Convert code blocks
// Multi-line code blocks
slackText = slackText.replace(/```([^`]+)```/g, "```$1```");

// Inline code - need to handle this after multi-line to avoid conflicts
slackText = slackText.replace(/`([^`]+)`/g, "`$1`");

// Convert basic formatting
// Bold: Convert both ** and __ to *
// Bold: Convert **text** to *text* and __text__ to *text*
slackText = slackText.replace(/(\*\*|__)(.*?)\1/g, "*$2*");

// Italic: Convert both * and _ to _
// We need to handle this carefully to not conflict with bold
slackText = slackText.replace(/(\*|_)(.*?)\1/g, (match, wrapper, content) => {
// Skip if it's part of a bold pattern
if (wrapper === "*" && (content.startsWith("*") || content.endsWith("*"))) {
return match;
}
return `_${content}_`;
});

// Strikethrough: Convert ~~ to ~
slackText = slackText.replace(/~~(.*?)~~/g, "~$1~");

// Convert blockquotes
slackText = slackText.replace(/^>\s*(.*)$/gm, ">$1");

// Convert links
// Standard markdown links [text](url)
slackText = slackText.replace(/\[(.*?)\]\((.*?)\)/g, "<$2|$1>");

// Bare URLs - make sure they're properly formatted
slackText = slackText.replace(/(?<!<)(https?:\/\/\S+)(?!>)/g, "<$1>");

// Convert lists
// Unordered lists: Convert *, +, or - to •
slackText = slackText.replace(/^[\*\+\-]\s+(.*)$/gm, "• $1");

// Ordered lists: Convert any number followed by . or ) to •
slackText = slackText.replace(/^\d+[\.\)]\s+(.*)$/gm, "• $1");

// Handle line breaks - Slack uses \n
slackText = slackText.replace(/\r\n/g, "\n");

return slackText;
}

export const slackFormatInstructions = `Format output as Slack mrkdwn messages using the following style: *bold*, <link|text>, _italics_, > quote, \`code\`, \`\`\`code block\`\`\`. It's important to use the correct syntax for the output to be rendered correctly. E.g. Important Link: <https://link.com|*Important Link*>.`;
export const slackFormatInstructions = `Format all output in Slack mrkdwn format. Generate Slack messages using the following style: *bold*, <link|text>, _italics_, > quote, \`code\`, \`\`\`code block\`\`\`. It's important to use the correct syntax for the output to be rendered correctly. E.g. Important Link: <https://link.com|*Important Link*>.`;

export async function generateSlackBlockKitMessage(message: string) {
const { object: blocks } = await generateObject({
Expand Down

0 comments on commit fabc8a7

Please sign in to comment.