Spaces:
Sleeping
Sleeping
File size: 2,205 Bytes
dd8c8ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
"""
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) |