function format_inline_markdown
Converts inline Markdown syntax (bold, italic, code, links) to HTML tags while escaping HTML entities for safe rendering.
/tf/active/vicechatdev/vice_ai/complex_app.py
1916 - 1931
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 <). Returns an empty string if input is empty.
Dependencies
htmlre
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function process_inline_markdown 82.7% similar
-
function html_to_markdown 79.1% similar
-
function html_to_markdown_v1 78.8% similar
-
function simple_markdown_to_html 77.6% similar
-
function convert_markdown_to_html_v1 76.9% similar