import { marked } from 'marked';
import dompurify from 'dompurify';

/**
 * Converts the input text, which is assumed to be in markdown format, into HTML and returns it.
 * The returned HTML is sanitized to prevent XSS.
 *
 * @param {string} text - The input text in markdown format to convert.
 * @returns {string} The HTML representation of the input text.
 */
export function markdownToHtml(text: string): string {
  // Remove zerowidth chars from beginning of input string
  // This is probably not necessary...
  text = text.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, "");

  // Return markdown parsed as HTML and subsequently sanitized
  return dompurify.sanitize(marked.parse(text));
}
