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

More edge case handling for text filtering #594

Merged
merged 1 commit into from
Oct 15, 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
8 changes: 6 additions & 2 deletions src/pipecat/utils/text/markdown_text_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def update_settings(self, settings: Mapping[str, Any]):

def filter(self, text: str) -> str:
if self._settings.enable_text_filter:
# Remove newlines only when there's no text before or after
filtered_text = re.sub(r"^\s*\n", "", text, flags=re.MULTILINE)
# Remove newlines and replace with a space only when there's no text before or after
filtered_text = re.sub(r"^\s*\n", " ", text, flags=re.MULTILINE)

# Remove backticks from inline code, but not from code blocks
filtered_text = re.sub(r"(?<!`)`([^`\n]+)`(?!`)", r"\1", filtered_text)
Expand All @@ -58,6 +58,10 @@ def filter(self, text: str) -> str:
r"^( +)|\s+$", lambda m: "§" * len(m.group(0)), filtered_text, flags=re.MULTILINE
)

# Remove space placeholders before tables, so that tables are converted to HTML
# correctly
filtered_text = re.sub(r"§\| ", "| ", filtered_text)

# Convert markdown to HTML
extension = ["tables"] if self._settings.filter_tables else []
md = Markdown(extensions=extension)
Expand Down
Loading