Skip to content

fix: preserve markdown tags in multi_cell dry_run output#1843

Open
zekiyemeral wants to merge 2 commits intopy-pdf:masterfrom
zekiyemeral:fix-markdown-dry-run-issue-1840
Open

fix: preserve markdown tags in multi_cell dry_run output#1843
zekiyemeral wants to merge 2 commits intopy-pdf:masterfrom
zekiyemeral:fix-markdown-dry-run-issue-1840

Conversation

@zekiyemeral
Copy link
Copy Markdown

Hi, this PR addresses issue #1840.
The problem was that multi_cell() with dry_run=True and markdown=True was returning plain text without the markdown markers when output="LINES" was used.
I modified the fragment processing loop in multi_cell() to check the font style of each fragment. If a fragment is Bold or Italic, I add the ** or _ symbols back to the text. This way, the dry-run output is consistent with the input markdown.
I verified the fix with a test case:
Input: "Kalin ve Italik metin."
Result: ["Kalin ve Italik metin."]
(Before it was returning: ["Kalin ve Italik metin."])
Fixes #1840

Copilot AI review requested due to automatic review settings May 5, 2026 13:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make FPDF.multi_cell(..., markdown=True, dry_run=True, output=LINES) return wrapped lines that preserve the original markdown markers, addressing issue #1840 where markers were lost in dry-run output.

Changes:

  • Updates multi_cell() to rebuild returned LINES by re-inserting markdown markers based on fragment styling.
  • Adds a standalone deneme.py script to manually demonstrate/verify the behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
fpdf/fpdf.py Reconstructs output=LINES from TextLine.fragments, attempting to re-add markdown markers based on fragment emphasis.
deneme.py Adds a manual test script intended to demonstrate that bold/italic markers are preserved in dry_run line output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread fpdf/fpdf.py
Comment on lines +5096 to +5101
if "B" in frag.font_style and "I" in frag.font_style:
frag_text = f"***{frag_text}***"
elif "B" in frag.font_style:
frag_text = f"**{frag_text}**"
elif "I" in frag.font_style:
frag_text = f"_{frag_text}_"
Comment thread fpdf/fpdf.py Outdated
Comment on lines +5093 to +5103
for frag in text_line.fragments:
characters.extend(frag.characters)
output_lines.append("".join(characters))
frag_text = "".join(frag.characters)
if markdown:
if "B" in frag.font_style and "I" in frag.font_style:
frag_text = f"***{frag_text}***"
elif "B" in frag.font_style:
frag_text = f"**{frag_text}**"
elif "I" in frag.font_style:
frag_text = f"_{frag_text}_"
line_content += frag_text
output_lines.append(line_content)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread fpdf/fpdf.py Outdated
Comment on lines +5092 to +5103
line_content = ""
for frag in text_line.fragments:
characters.extend(frag.characters)
output_lines.append("".join(characters))
frag_text = "".join(frag.characters)
if markdown:
if "B" in frag.font_style and "I" in frag.font_style:
frag_text = f"***{frag_text}***"
elif "B" in frag.font_style:
frag_text = f"**{frag_text}**"
elif "I" in frag.font_style:
frag_text = f"_{frag_text}_"
line_content += frag_text
output_lines.append(line_content)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread fpdf/fpdf.py
Comment on lines +5096 to +5101
if "B" in frag.font_style and "I" in frag.font_style:
frag_text = f"***{frag_text}***"
elif "B" in frag.font_style:
frag_text = f"**{frag_text}**"
elif "I" in frag.font_style:
frag_text = f"_{frag_text}_"
Comment thread deneme.py Outdated
Comment on lines +3 to +21
pdf = FPDF()
pdf.add_page()
pdf.set_font("helvetica", size=12)

# Test metnimiz: İçinde kalın (**) ve italik (_) kelimeler var.
test_text = "**Kalin** ve _Italik_ metin."

# Fonksiyonu dry_run=True ve markdown=True ile çağırıyoruz.
# Çıktı olarak satırları (LINES) istiyoruz.
result = pdf.multi_cell(w=0, h=10, text=test_text, markdown=True, dry_run=True, output="LINES")

print("\n--- TEST SONUCU ---")
print(f"Gelen Çıktı: {result}")

if "**Kalin**" in result[0] and "_Italik_" in result[0]:
print("\nBAŞARILI: Markdown etiketleri korundu! 🚀")
else:
print("\nHATA: Etiketler hala kayıp. ❌")
print("-------------------\n") No newline at end of file
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"I have deleted this manual test script from the PR to keep the repository clean, as suggested. I'll rely on the library's existing test suite for final validation."

@zekiyemeral
Copy link
Copy Markdown
Author

"I have addressed all the feedback from the code review.
Optimized the performance by using list joining.
Added support for underline, strikethrough, and nested markdown markers.
Aligned with the library's internal markdown syntax.
Removed the manual test script.
Ready for final review."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return markdown format in FPDF.mult_cell(..., dry_run=True)

2 participants