🔍 Code Extractor

function format_inline_markdown

Maturity: 45

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

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
1916 - 1931
Complexity:
simple

Purpose

This function processes text containing Markdown inline formatting and converts it to equivalent HTML markup. It handles bold (**text**), italic (*text*), inline code (`code`), and links ([text](url)). The function first escapes any existing HTML entities to prevent XSS attacks, then applies regex-based transformations to convert Markdown syntax to HTML tags. This is useful for rendering user-generated Markdown content safely in web applications.

Source Code

def format_inline_markdown(text):
    """Format inline markdown elements like bold, italic, code, links"""
    # Escape HTML entities first
    text = html.escape(text)
    
    # Bold and italic (be careful with order)
    text = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', text)
    text = re.sub(r'\*(.*?)\*', r'<em>\1</em>', text)
    
    # Inline code
    text = re.sub(r'`(.*?)`', r'<code>\1</code>', text)
    
    # Links [text](url)
    text = re.sub(r'\[([^\]]*)\]\(([^)]*)\)', r'<a href="\2">\1</a>', text)
    
    return text

Parameters

Name Type Default Kind
text - - positional_or_keyword

Parameter Details

text: A string containing Markdown-formatted text with inline elements. Expected to contain Markdown syntax like **bold**, *italic*, `code`, or [link](url). Can be plain text without Markdown syntax, which will only be HTML-escaped. Should be a string type; no explicit validation for None or non-string types.

Return Value

Returns a string containing HTML-formatted text. Markdown inline elements are converted to their HTML equivalents: **text** becomes <strong>text</strong>, *text* becomes <em>text</em>, `code` becomes <code>code</code>, and [text](url) becomes <a href="url">text</a>. All HTML special characters in the original text are escaped (e.g., < becomes &lt;). Returns an empty string if input is empty.

Dependencies

  • html
  • re

Required Imports

import html
import re

Usage Example

import html
import re

def format_inline_markdown(text):
    text = html.escape(text)
    text = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', text)
    text = re.sub(r'\*(.*?)\*', r'<em>\1</em>', text)
    text = re.sub(r'`(.*?)`', r'<code>\1</code>', text)
    text = re.sub(r'\[([^\]]*)\]\(([^)]*)\)', r'<a href="\2">\1</a>', text)
    return text

# Example usage
markdown_text = "This is **bold** and *italic* text with `code` and a [link](https://example.com)"
html_output = format_inline_markdown(markdown_text)
print(html_output)
# Output: This is <strong>bold</strong> and <em>italic</em> text with <code>code</code> and a <a href="https://example.com">link</a>

Best Practices

  • The function processes bold before italic to avoid conflicts with nested asterisks (e.g., ***text*** should become <strong><em>text</em></strong>)
  • HTML entities are escaped first to prevent XSS attacks when rendering user-generated content
  • The regex patterns use non-greedy matching (.*?) to handle multiple inline elements on the same line correctly
  • This function only handles inline Markdown elements, not block-level elements like headers, lists, or code blocks
  • For nested formatting (e.g., bold within italic), the order of regex substitutions matters and may not handle all edge cases
  • Link URLs are not validated or sanitized beyond HTML escaping; consider additional validation for javascript: or data: URLs in production
  • Input should be a string; passing None or non-string types will raise an AttributeError

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function process_inline_markdown 82.7% similar

    Processes inline markdown formatting by unescaping HTML entities in text. Currently performs basic cleanup while preserving markdown syntax for downstream processing.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function html_to_markdown 79.1% 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
  • function html_to_markdown_v1 78.8% 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 simple_markdown_to_html 77.6% 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 convert_markdown_to_html_v1 76.9% similar

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

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