// MarkdownV2 special characters that must be escaped
const ESCAPE_CHARS = /[_*[\]()~`>#+=|{}.!\\-]/g;

export function escapeMarkdown(text: string): string {
  return text.replace(ESCAPE_CHARS, '\\$&');
}

export function bold(text: string): string {
  return `*${escapeMarkdown(text)}*`;
}

export function italic(text: string): string {
  return `_${escapeMarkdown(text)}_`;
}

export function code(text: string): string {
  return `\`${text.replace(/`/g, '\\`')}\``;
}

export function pre(text: string, lang = ''): string {
  const escaped = text.replace(/`/g, '\\`');
  return `\`\`\`${lang}\n${escaped}\n\`\`\``;
}

export function link(label: string, url: string): string {
  return `[${escapeMarkdown(label)}](${url})`;
}

export function cardLine(name: string, url: string): string {
  return `• ${link(name, url)}`;
}
