Skip to content

Python tool for converting files and office documents to Markdown.

fix(ipynb): preserve leading # in notebook heading titles (#2371)

αIa85dff7fc1a4
diff --git a/packages/markitdown/src/markitdown/converters/_ipynb_converter.py b/packages/markitdown/src/markitdown/converters/_ipynb_converter.py
index 80914c2d..9152218e 100644
--- a/packages/markitdown/src/markitdown/converters/_ipynb_converter.py
+++ b/packages/markitdown/src/markitdown/converters/_ipynb_converter.py
@@ -73,7 +73,7 @@ class IpynbConverter(DocumentConverter):
if title is None:
for line in source_lines:
if line.startswith("# "):
- title = line.lstrip("# ").strip()
+ title = line.removeprefix("# ").strip()
break
elif cell_type == "code":
diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py
index 89a03bd2..57fd6523 100644
--- a/packages/markitdown/tests/test_module_misc.py
+++ b/packages/markitdown/tests/test_module_misc.py
@@ -956,6 +956,29 @@ def test_zip_stream_no_filename_header() -> None:
assert "Hello world" in result.markdown
+def test_ipynb_heading_title_preserves_leading_hash() -> None:
+ """Heading marker removal must not eat a leading '#' from the title text.
+
+ Regression for https://github.com/microsoft/markitdown/issues/2367
+ """
+ from markitdown.converters._ipynb_converter import IpynbConverter
+
+ notebook = {
+ "nbformat": 4,
+ "nbformat_minor": 5,
+ "metadata": {},
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "source": ["# #hashtag campaign results\n"],
+ "metadata": {},
+ }
+ ],
+ }
+ result = IpynbConverter()._convert(notebook)
+ assert result.title == "#hashtag campaign results"
+
+
def test_ipynb_accepts_non_ascii() -> None:
"""IpynbConverter.accepts() must not raise on non-ASCII binary content."""
from markitdown.converters._ipynb_converter import IpynbConverter