Merge pull request #594 from pipecat-ai/mb/more-text-filter-massaging

More edge case handling for text filtering
This commit is contained in:
Mark Backman
2024-10-15 14:51:43 -04:00
committed by GitHub

View File

@@ -40,8 +40,8 @@ class MarkdownTextFilter(BaseTextFilter):
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)
@@ -58,6 +58,10 @@ class MarkdownTextFilter(BaseTextFilter):
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)