🔍 Code Extractor

function add_inline_formatting_to_paragraph_v1

Maturity: 45

Parses markdown-formatted text and adds it to a Word document paragraph, converting markdown links [text](url) into clickable hyperlinks while delegating other markdown formatting to a helper function.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
3953 - 3972
Complexity:
moderate

Purpose

This function serves as a markdown-to-Word converter specifically for inline formatting within paragraphs. It handles the conversion of markdown link syntax into Word hyperlinks and delegates other markdown formatting (bold, italic, etc.) to add_markdown_formatting_to_paragraph. This is useful when generating Word documents from markdown content, particularly in document generation workflows where user input or stored content contains markdown formatting.

Source Code

def add_inline_formatting_to_paragraph(paragraph, text):
    """Add text with inline markdown formatting to a Word paragraph, including clickable links"""
    import re
    from docx.oxml.shared import qn
    from docx.oxml import parse_xml
    
    # First handle links in markdown format [text](url)
    link_parts = re.split(r'\[([^\]]+)\]\(([^)]+)\)', text)
    
    for i, part in enumerate(link_parts):
        if i % 3 == 0:  # Regular text (not a link)
            if part:
                # Process other markdown formatting in regular text
                add_markdown_formatting_to_paragraph(paragraph, part)
        elif i % 3 == 1:  # Link text
            link_text = part
            # The URL will be in the next part (i+1)
            if i + 1 < len(link_parts):
                link_url = link_parts[i + 1]
                add_hyperlink_to_paragraph(paragraph, link_text, link_url)

Parameters

Name Type Default Kind
paragraph - - positional_or_keyword
text - - positional_or_keyword

Parameter Details

paragraph: A python-docx Paragraph object representing the Word document paragraph to which formatted text will be added. This should be an existing paragraph object obtained from a Document instance (e.g., doc.add_paragraph() or doc.paragraphs[0]).

text: A string containing markdown-formatted text to be parsed and added to the paragraph. Expected to contain markdown link syntax [link_text](url) and potentially other markdown formatting like **bold**, *italic*, etc. The text is split and processed to extract links while preserving other content.

Return Value

This function returns None (implicitly). It modifies the provided paragraph object in-place by adding formatted runs and hyperlinks to it. The side effect is that the paragraph object will contain the parsed and formatted content after execution.

Dependencies

  • python-docx
  • re

Required Imports

import re
from docx.oxml.shared import qn
from docx.oxml import parse_xml

Conditional/Optional Imports

These imports are only needed under specific conditions:

import re

Condition: always required for regex pattern matching of markdown links

Required (conditional)
from docx.oxml.shared import qn

Condition: required for XML namespace handling in Word documents, imported inside function

Required (conditional)
from docx.oxml import parse_xml

Condition: required for parsing XML elements when creating hyperlinks, imported inside function

Required (conditional)

Usage Example

from docx import Document
from docx.oxml.shared import qn
from docx.oxml import parse_xml
import re

# Assume helper functions are defined:
# def add_markdown_formatting_to_paragraph(paragraph, text): ...
# def add_hyperlink_to_paragraph(paragraph, link_text, link_url): ...

# Create a Word document
doc = Document()
paragraph = doc.add_paragraph()

# Add markdown-formatted text with links
text = 'Check out **this link** [Google](https://www.google.com) and *another* [GitHub](https://github.com)'
add_inline_formatting_to_paragraph(paragraph, text)

# Save the document
doc.save('output.docx')

Best Practices

  • Ensure that add_markdown_formatting_to_paragraph and add_hyperlink_to_paragraph helper functions are defined before calling this function
  • The function uses modulo arithmetic (i % 3) to parse regex split results, which assumes the pattern captures exactly 2 groups per match
  • Input text should use proper markdown link syntax [text](url) with no spaces between brackets and parentheses
  • The function modifies the paragraph object in-place, so no return value is needed
  • Consider validating URLs before passing them to add_hyperlink_to_paragraph to ensure they are well-formed
  • Be aware that nested markdown formatting within link text may not be handled correctly
  • The regex pattern does not handle escaped brackets or parentheses, so avoid using those in link text or URLs

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function add_inline_formatting_to_paragraph 87.0% similar

    Parses markdown-formatted text and applies inline formatting (bold, italic, code) to a Microsoft Word paragraph object using the python-docx library.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function add_formatted_content_to_word 76.3% similar

    Converts processed markdown elements into formatted content within a Word document, handling headers, paragraphs, lists, tables, and code blocks with appropriate styling.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function add_markdown_formatting_to_paragraph 76.3% similar

    Parses markdown-formatted text and applies corresponding formatting (bold, italic, code) to runs within a python-docx paragraph object.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function add_formatted_content_to_word_v1 74.7% similar

    Converts processed markdown elements into formatted content within a Microsoft Word document, handling headers, paragraphs, lists, tables, and code blocks with appropriate styling.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function add_hyperlink_to_paragraph 74.0% similar

    Adds a clickable hyperlink to a Microsoft Word paragraph using python-docx, with XML-based hyperlink creation and styled fallback options.

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