Repository: phidatahq/phidata
File: libs/agno/agno/knowledge/chunking/markdown.py (Python)

Code before:
                        else "\n\n".join(current_chunk_content)
                    )
                    chunks.append(chunk_text.strip())
                    current_chunk_content = []

                # Split the large paragraph by sentences or fixed size
                available_size = self.chunk_size - heading_size
                words = para.split()
                current_words = []
                current_word_len = 0

                for word in words:
                    if current_word_len + len(word) + 1 > available_size and current_words:
                        chunk_text = heading + "\n\n" + " ".join(current_words) if heading else " ".join(current_words)
                        chunks.append(chunk_text.strip())
                        current_words = []
                        current_word_len = 0
                    current_words.append(word)
                    current_word_len += len(word) + 1

                if current_words:
                    current_chunk_content.append(" ".join(current_words))
            else:
                current_chunk_content.append(para)

        # Don't forget remaining content
        if current_chunk_content:
            chunk_text = (
                heading + "\n\n" + "\n\n".join(current_chunk_content) if heading else "\n\n".join(current_chunk_content)
            )
            chunks.append(chunk_text.strip())

        return chunks if chunks else [section]

    def _split_by_headings(self, content: str) -> List[str]:
        """
        Split markdown content by headings, keeping each heading with its content.
        Returns a list of sections where each section starts with a heading.

        When split_on_headings is an int, only splits on headings at or above that level.
        For example, split_on_headings=2 splits on H1 and H2, keeping H3-H6 content together.
        """
        # Determine which heading levels to split on
        if isinstance(self.split_on_headings, int) and not isinstance(self.split_on_headings, bool):
            # Split on headings at or above this level (1 to split_on_headings)
            max_heading_level = self.split_on_headings
            heading_pattern = rf"^#{{{1},{max_heading_level}}}\s+.+$"
        else:
            # split_on_headings is True: split on all headings (# to ######)
            heading_pattern = r"^#{1,6}\s+.+$"


Review comment (suggestion):
```suggestion
        # add remaining content
```

Code after:
                    chunks.append(chunk_text.strip())
                    current_chunk_content = []

                # Split the large paragraph by sentences or fixed size
                available_size = self.chunk_size - heading_size
                words = para.split()
                current_words: List[str] = []
                current_word_len = 0

                for word in words:
                    if current_word_len + len(word) + 1 > available_size and current_words:
                        chunk_text = heading + "\n\n" + " ".join(current_words) if heading else " ".join(current_words)
                        chunks.append(chunk_text.strip())
                        current_words = []
                        current_word_len = 0
                    current_words.append(word)
                    current_word_len += len(word) + 1

                if current_words:
                    current_chunk_content.append(" ".join(current_words))
            else:
                current_chunk_content.append(para)

        # add the remaining content
        if current_chunk_content:
            chunk_text = (
                heading + "\n\n" + "\n\n".join(current_chunk_content) if heading else "\n\n".join(current_chunk_content)
            )
            chunks.append(chunk_text.strip())

        return chunks if chunks else [section]

    def _split_by_headings(self, content: str) -> List[str]:
        """
        Split markdown content by headings, keeping each heading with its content.
        Returns a list of sections where each section starts with a heading.

        When split_on_headings is an int, only splits on headings at or above that level.
        For example, split_on_headings=2 splits on H1 and H2, keeping H3-H6 content together.
        """
        # Determine which heading levels to split on
        if isinstance(self.split_on_headings, int) and not isinstance(self.split_on_headings, bool):
            # Split on headings at or above this level (1 to split_on_headings)
            max_heading_level = self.split_on_headings
            heading_pattern = rf"^#{{{1},{max_heading_level}}}\s+.+$"
        else:
            # split_on_headings is True: split on all headings (# to ######)
            heading_pattern = r"^#{1,6}\s+.+$"

        # Split content while keeping the delimiter (heading)
        # Use non-capturing group for the pattern to avoid extra capture groups