diff --git a/examples/site-to-markdown/main.py b/examples/site-to-markdown/main.py index 1ff7ade5..65f89d37 100644 --- a/examples/site-to-markdown/main.py +++ b/examples/site-to-markdown/main.py @@ -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}})"