Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(partners): keep empty cells when converting Slack markdown tables (#679)

Santh68de93f66ef6
diff --git a/deeptutor/partners/channels/slack.py b/deeptutor/partners/channels/slack.py
index 24ef4b20c..4d438bd64 100644
--- a/deeptutor/partners/channels/slack.py
+++ b/deeptutor/partners/channels/slack.py
@@ -15,6 +15,7 @@ from slackify_markdown import slackify_markdown
from deeptutor.partners.bus.events import OutboundMessage
from deeptutor.partners.bus.queue import MessageBus
from deeptutor.partners.channels.base import BaseChannel
+from deeptutor.partners.helpers import convert_markdown_table_to_labeled_rows
from deeptutor.partners.config.schema import Base, DeliveryOverrides
# Slack's WSS endpoint can be blocked while HTTPS still works (auth_test
@@ -317,16 +318,4 @@ class SlackChannel(BaseChannel):
@staticmethod
def _convert_table(match: re.Match) -> str:
"""Convert a Markdown table to a Slack-readable list."""
- lines = [ln.strip() for ln in match.group(0).strip().splitlines() if ln.strip()]
- if len(lines) < 2:
- return match.group(0)
- headers = [h.strip() for h in lines[0].strip("|").split("|")]
- start = 2 if re.fullmatch(r"[|\s:\-]+", lines[1]) else 1
- rows: list[str] = []
- for line in lines[start:]:
- cells = [c.strip() for c in line.strip("|").split("|")]
- cells = (cells + [""] * len(headers))[: len(headers)]
- parts = [f"**{headers[i]}**: {cells[i]}" for i in range(len(headers)) if cells[i]]
- if parts:
- rows.append(" · ".join(parts))
- return "\n".join(rows)
+ return convert_markdown_table_to_labeled_rows(match.group(0))
diff --git a/deeptutor/partners/helpers.py b/deeptutor/partners/helpers.py
index c0c5e8032..6d5631a84 100644
--- a/deeptutor/partners/helpers.py
+++ b/deeptutor/partners/helpers.py
@@ -74,11 +74,29 @@ def split_message(content: str, max_len: int = 2000) -> list[str]:
return chunks
+def convert_markdown_table_to_labeled_rows(table_text: str) -> str:
+ """Convert a Markdown pipe table to labeled rows (for Slack-style text).
+
+ Empty cells are kept so blank columns are not dropped.
+ """
+ lines = [ln.strip() for ln in table_text.strip().splitlines() if ln.strip()]
+ if len(lines) < 2:
+ return table_text
+ headers = [h.strip() for h in lines[0].strip("|").split("|")]
+ start = 2 if re.fullmatch(r"[|\s:\-]+", lines[1]) else 1
+ rows: list[str] = []
+ for line in lines[start:]:
+ cells = [c.strip() for c in line.strip("|").split("|")]
+ cells = (cells + [""] * len(headers))[: len(headers)]
+ parts = [f"**{headers[i]}**: {cells[i]}" for i in range(len(headers))]
+ if parts:
+ rows.append(" · ".join(parts))
+ return "\n".join(rows)
+
+
def is_markdown_table_separator_row(cells: list[str]) -> bool:
"""True if *cells* look like a markdown table separator row.
An all-empty row is not a separator (`all([])` would otherwise be True).
"""
- return bool(any(c for c in cells)) and all(
- re.match(r"^:?-+:?$", c) for c in cells if c
- )
+ return bool(any(c for c in cells)) and all(re.match(r"^:?-+:?$", c) for c in cells if c)
diff --git a/tests/partners/test_markdown_table_empty_cells.py b/tests/partners/test_markdown_table_empty_cells.py
new file mode 100644
index 000000000..994eebd5c
--- /dev/null
+++ b/tests/partners/test_markdown_table_empty_cells.py
@@ -0,0 +1,20 @@
+"""Empty markdown table cells must still appear in labeled-row conversion."""
+
+from __future__ import annotations
+
+from deeptutor.partners.helpers import convert_markdown_table_to_labeled_rows
+
+
+def test_empty_cells_kept_in_converted_table() -> None:
+ table = (
+ "| Name | Score | Note |\n"
+ "| --- | --- | --- |\n"
+ "| Alice | | ok |\n"
+ "| Bob | 10 | |\n"
+ )
+ out = convert_markdown_table_to_labeled_rows(table)
+ assert out.count("**Name**:") == 2
+ assert out.count("**Score**:") == 2
+ assert out.count("**Note**:") == 2
+ assert "**Score**: " in out # Alice empty score kept
+ assert out.endswith("**Note**: ") or "**Note**: " in out.split("\n")[1]