Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(export): fix excessive newline characters in Markdown content #13

Merged
merged 1 commit into from
Jun 14, 2024
Merged
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
32 changes: 28 additions & 4 deletions export_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from database_manager import DatabaseManager
import logging # Add log messages
import re


class ExportManager:
Expand Down Expand Up @@ -32,10 +33,29 @@ def _adjust_headers(self, content, level_increment=1):
if line.startswith("#"):
hashes = len(line.split(" ")[0])
new_hashes = min(hashes + level_increment, 6) # Limit to ######
line = "#" * new_hashes + line[hashes:]
line = "\n" + "#" * new_hashes + line[hashes:] + "\n"
new_content += line + "\n"
return new_content

def _cleanup_markdown(self, content):
"""
Remove excessive newline characters from Markdown content.

This method replaces sequences of three or more consecutive newline characters
with exactly two newline characters, ensuring that there are no unnecessary
blank lines in the output.

Args:
content (str): The Markdown content to be cleaned up.

Returns:
str: The cleaned-up Markdown content with reduced newline characters.
"""
while "\n\n\n" in content:
content = content.replace("\n\n\n", "\n\n")
return content


def _concatenate_markdown(self, pages):
"""
Concatenate a list of Markdown files into one, with header adjustments.
Expand All @@ -46,7 +66,7 @@ def _concatenate_markdown(self, pages):
Returns:
str: The concatenated Markdown content.
"""
final_content = f"# {self.title}\n\n"
final_content = f"# {self.title}\n"
for url, content, metadata in pages:
if content is None:
continue # Skip empty pages
Expand All @@ -56,7 +76,7 @@ def _concatenate_markdown(self, pages):
}

# Prepare metadata as an HTML comment
metadata_content = f"<!--\n"
metadata_content = "<!--\n"
metadata_content += f"URL: {url}\n"
for key, value in filtered_metadata.items():
metadata_content += f"{key}: {value}\n"
Expand All @@ -66,8 +86,10 @@ def _concatenate_markdown(self, pages):
adjusted_content = self._adjust_headers(content)

final_content += (
"\n\n" + metadata_content + "\n\n" + adjusted_content + "\n\n---"
"\n" + metadata_content + "\n\n" + adjusted_content + "\n---"
) # Add a separator and metadata

final_content = self._cleanup_markdown(final_content)

return final_content

Expand Down Expand Up @@ -100,6 +122,8 @@ def export_to_json(self, output_path):
if content is None:
continue # Skip empty pages

content = self._cleanup_markdown(content)

filtered_metadata = {
k: v for k, v in json.loads(metadata).items() if v is not None
}
Expand Down