Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/site-to-markdown/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ async def site_to_markdown():
await page.screenshot(full_page=False, type='png')
).decode('utf-8')

# Base64-encode the HTML before injecting into the f-string so that
# triple-quote sequences, backslashes, or closing-string markers in
# the page content cannot break out of the generated Python source.
html_b64 = base64.b64encode(html.encode("utf-8")).decode("utf-8")

# Jupyter: Run code in sandbox to convert html to markdown
c.jupyter.execute_code(
code=f"""
import base64
from markdownify import markdownify
html = '''{html}'''

html = base64.b64decode("{html_b64}").decode("utf-8")
screenshot_b64 = "{screenshot_b64}"

md = f"{{markdownify(html)}}\\n\\n![Screenshot](data:image/png;base64,{{screenshot_b64}})"
Expand Down