Spaces:
Sleeping
Sleeping
| """ | |
| 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'<h3>\1</h3>', text, flags=re.MULTILINE) | |
| text = re.sub(r'^## (.*?)$', r'<h2>\1</h2>', text, flags=re.MULTILINE) | |
| text = re.sub(r'^# (.*?)$', r'<h1>\1</h1>', text, flags=re.MULTILINE) | |
| # Bold text | |
| text = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', text) | |
| text = re.sub(r'__(.*?)__', r'<strong>\1</strong>', text) | |
| # Special handling for "Confidence Notes:" to ensure proper styling | |
| text = re.sub(r'<strong>Confidence Notes:</strong>', r'<strong style="color: #212529 !important;">Confidence Notes:</strong>', text) | |
| text = re.sub(r'\*\*Confidence Notes:\*\*', r'<strong style="color: #212529 !important;">Confidence Notes:</strong>', text) | |
| # Italic text | |
| text = re.sub(r'\*(.*?)\*', r'<em>\1</em>', text) | |
| text = re.sub(r'_(.*?)_', r'<em>\1</em>', text) | |
| # Code blocks (triple backticks) | |
| text = re.sub(r'```(.*?)```', r'<pre><code>\1</code></pre>', text, flags=re.DOTALL) | |
| # Inline code | |
| text = re.sub(r'`(.*?)`', r'<code>\1</code>', 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 <br> | |
| paragraph = paragraph.replace('\n', '<br>') | |
| paragraph = f'<p>{paragraph}</p>' | |
| html_paragraphs.append(paragraph) | |
| return '\n'.join(html_paragraphs) |