🔍 Code Extractor

function convert_markdown_to_html_v1

Maturity: 47

Converts basic Markdown syntax to HTML markup compatible with ReportLab PDF generation, including support for clickable links, bold, italic, and inline code formatting.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
4160 - 4172
Complexity:
simple

Purpose

This function transforms common Markdown formatting patterns into HTML tags that can be rendered by ReportLab's Paragraph component when generating PDF documents. It specifically handles markdown links [text](url), bold text (**text**), italic text (*text*), and inline code (`code`), converting them to their HTML equivalents with appropriate styling for PDF output.

Source Code

def convert_markdown_to_html(text):
    """Convert basic markdown to HTML for reportlab, including clickable links"""
    import re
    
    # Convert markdown links [text](url) to HTML links
    text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'<a href="\2">\1</a>', text)
    
    # Convert markdown formatting to HTML
    text = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', text)  # Bold
    text = re.sub(r'\*(.*?)\*', r'<i>\1</i>', text)      # Italic
    text = re.sub(r'`(.*?)`', r'<font name="Courier">\1</font>', text)  # Code
    
    return text

Parameters

Name Type Default Kind
text - - positional_or_keyword

Parameter Details

text: A string containing Markdown-formatted text. Expected to contain basic Markdown syntax including links [text](url), bold **text**, italic *text*, and inline code `text`. Can be plain text without Markdown, which will be returned unchanged. No length constraints, but should be a valid string type.

Return Value

Returns a string containing HTML-formatted text. Markdown links are converted to <a href="url">text</a>, bold text to <b>text</b>, italic text to <i>text</i>, and inline code to <font name="Courier">text</font>. The returned HTML is specifically formatted for ReportLab's Paragraph rendering engine. If input contains no Markdown, returns the original text unchanged.

Dependencies

  • re

Required Imports

import re

Usage Example

import re

def convert_markdown_to_html(text):
    import re
    text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'<a href="\2">\1</a>', text)
    text = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', text)
    text = re.sub(r'\*(.*?)\*', r'<i>\1</i>', text)
    text = re.sub(r'`(.*?)`', r'<font name="Courier">\1</font>', text)
    return text

# Example usage
markdown_text = "Visit [Google](https://google.com) for **bold** and *italic* text with `code`."
html_output = convert_markdown_to_html(markdown_text)
print(html_output)
# Output: Visit <a href="https://google.com">Google</a> for <b>bold</b> and <i>italic</i> text with <font name="Courier">code</font>.

# Use with ReportLab
from reportlab.platypus import Paragraph
from reportlab.lib.styles import getSampleStyleSheet
styles = getSampleStyleSheet()
para = Paragraph(html_output, styles['Normal'])

Best Practices

  • This function uses non-greedy regex matching (.*?) to handle nested or multiple instances of formatting on the same line
  • The order of regex substitutions matters: links are processed first to avoid interfering with bold/italic patterns that might appear in URLs
  • This is a basic converter and does not handle all Markdown syntax (e.g., headers, lists, blockquotes, images)
  • The function does not escape HTML entities in the input text, so pre-existing HTML may interfere with Markdown conversion
  • For complex Markdown documents, consider using a full Markdown parser library like markdown2 or mistune
  • The Courier font reference assumes ReportLab has access to this font; ensure font availability in your ReportLab environment
  • Nested formatting (e.g., ***bold and italic***) may not render as expected due to the order of regex operations
  • URLs in markdown links are not validated or sanitized; ensure input is from trusted sources or add validation

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function convert_markdown_to_html 93.0% similar

    Converts basic markdown formatting (bold, italic, code) to HTML markup suitable for PDF generation using ReportLab.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function simple_markdown_to_html 78.3% similar

    Converts a subset of Markdown syntax to clean HTML, supporting headers, bold text, unordered lists, and paragraphs.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function html_to_markdown_v1 78.2% similar

    Converts HTML markup to Markdown syntax, handling headers, code blocks, text formatting, links, lists, and paragraphs with proper spacing.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function format_inline_markdown 76.9% similar

    Converts inline Markdown syntax (bold, italic, code, links) to HTML tags while escaping HTML entities for safe rendering.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function html_to_markdown 76.5% similar

    Converts HTML text back to Markdown format using regex-based pattern matching and replacement, handling headers, code blocks, formatting, links, lists, and HTML entities.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
← Back to Browse