fix(discord): wrap markdown tables in code fences for readable rendering - #35707
fix(discord): wrap markdown tables in code fences for readable rendering#35707liuhao1024 wants to merge 1 commit into
Conversation
Discord does not render GFM pipe tables natively — raw pipe characters display as garbage text. Detect markdown table patterns (header row + separator row + data rows) and wrap them in triple-backtick code fences so they render as readable monospaced text. Tables already inside fenced code blocks are left untouched. Fixes NousResearch#21168
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Approved
Overview
Well-crafted fix wrapping GFM markdown tables in code fences for Discord rendering. Discord lacks native table support, so pipe tables render as garbage — wrapping them in triple backticks gives readable monospaced output.
Looks Good
- Correct detection: Header row + separator row pattern matching, avoids false positives on pipes-in-text
- Nested fences: Tables already inside ``` blocks are left untouched (tracked via
in_fencestate) - Edge cases: No pipes, no separator, pipes without separator, empty input, multiple tables, alignment rows, tables without leading/trailing pipes — all handled
- Comprehensive tests: 11 test cases covering all edge cases
- Clean integration:
format_messagecalled on every Discord send, so all messages benefit - No debug artifacts found: Clean diff
Reviewed by Hermes Agent
|
@alt-glitch Acknowledged. Both competing PRs (#4401, #16903) have CI failures (no CI run or 2 test failures), while this PR passes all checks. If maintainers prefer the duplicate flag to take precedence, feel free to close this. Otherwise, I believe this PR is ready for merge. |
|
We experimented with this PR this morning and got a table like so: That is marginally more readable than without this PR, but it doesn't actually solve the problem of readability. It seems like it should render an ascii box table (converting pipes to aligned +---+) -- unless the goal here is just to treat the table as text to shield from Discord's parser versus trying to render it 'correctly' |
|
Closing as the issue #21168 has been resolved via #53284, which takes the same bullet-group conversion approach Telegram already uses (matching your code-fence wrapping would have been an alternative approach, but the bullet-group pattern was chosen for consistency with the existing Telegram implementation and better mobile readability). Thanks for the contribution! |
What does this PR do?
Wraps GFM-style markdown tables in triple-backtick code fences when sending messages to Discord, so pipe characters render as readable monospaced text instead of garbage.
Related Issue
Fixes #21168
Type of Change
Changes Made
plugins/platforms/discord/adapter.py: Added_wrap_tables_in_code_fence()helper that detects markdown table patterns (header row + separator row + data rows) and wraps them in ``` fences. Tables already inside code blocks are left untouched. Updatedformat_message()to call this helper instead of being a no-op passthrough.tests/gateway/test_discord_table_wrap.py: Added 11 tests covering basic wrapping, surrounding text preservation, tables inside existing code fences (untouched), no-table passthrough, pipe-without-separator, empty input, tables without leading pipes, multiple tables, alignment rows, single-column separators, andformat_messageintegration.How to Test
python3 -m pytest tests/gateway/test_discord_table_wrap.py -v— all 11 tests should passpython3 -m pytest tests/gateway/test_discord_system_messages.py tests/gateway/test_discord_bot_filter.py tests/gateway/test_discord_connect.py -v— all 32 existing Discord tests should still pass (no regression)Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/gateway/test_discord_table_wrap.py tests/gateway/test_discord_system_messages.py tests/gateway/test_discord_bot_filter.py tests/gateway/test_discord_connect.py -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
plugins/platforms/discord/adapter.py—format_message()(callers: 3 in send/send_multiple_images methods),_wrap_tables_in_code_fence()(new helper)_wrap_markdown_tables()ingateway/platforms/telegram.py(same table detection logic, different output format — Telegram converts to bullet groups, Discord wraps in code fences)