""" Markdown utilities for converting markdown to HTML. """ import re import html def convert_markdown_to_html(markdown_text: str) -> str: """ Convert markdown text to HTML with basic formatting. Args: markdown_text: The markdown text to convert Returns: HTML formatted string """ if not markdown_text or not isinstance(markdown_text, str): return "" # Escape HTML special characters first text = html.escape(markdown_text.strip()) # Convert markdown to HTML # Headers text = re.sub(r'^### (.*?)$', r'

\1

', text, flags=re.MULTILINE) text = re.sub(r'^## (.*?)$', r'

\1

', text, flags=re.MULTILINE) text = re.sub(r'^# (.*?)$', r'

\1

', text, flags=re.MULTILINE) # Bold text text = re.sub(r'\*\*(.*?)\*\*', r'\1', text) text = re.sub(r'__(.*?)__', r'\1', text) # Special handling for "Confidence Notes:" to ensure proper styling text = re.sub(r'Confidence Notes:', r'Confidence Notes:', text) text = re.sub(r'\*\*Confidence Notes:\*\*', r'Confidence Notes:', text) # Italic text text = re.sub(r'\*(.*?)\*', r'\1', text) text = re.sub(r'_(.*?)_', r'\1', text) # Code blocks (triple backticks) text = re.sub(r'```(.*?)```', r'
\1
', text, flags=re.DOTALL) # Inline code text = re.sub(r'`(.*?)`', r'\1', text) # Convert line breaks to paragraphs paragraphs = text.split('\n\n') html_paragraphs = [] for paragraph in paragraphs: paragraph = paragraph.strip() if paragraph: # Check if it's already wrapped in HTML tags if not (paragraph.startswith('<') and paragraph.endswith('>')): # Replace single line breaks with
paragraph = paragraph.replace('\n', '
') paragraph = f'

{paragraph}

' html_paragraphs.append(paragraph) return '\n'.join(html_paragraphs)