/**
 * Micron markup → HTML renderer using the micron-parser library.
 * Reference: https://github.com/RFnexus/micron-parser-js
 *
 * Uses convertMicronToFragment (DOM-based) instead of convertMicronToHtml
 * to avoid DOMPurify stripping nomadnetwork:// hrefs from link tags.
 */

import MicronParser from "micron-parser";

let darkParser: MicronParser | null = null;
let lightParser: MicronParser | null = null;

export function renderMicron(source: string, darkTheme: boolean = true): string {
  const parser = darkTheme
    ? (darkParser ??= new MicronParser(true, true))
    : (lightParser ??= new MicronParser(false, true));

  const fragment = parser.convertMicronToFragment(source);

  // Serialize the fragment to HTML string
  const div = document.createElement("div");
  div.appendChild(fragment);
  return div.innerHTML;
}
