Skip to content

Improve Matrix message body parsing with smart format detection - #13

Merged
fmartingr merged 4 commits into
masterfrom
fix/body-markdown
Jul 16, 2025
Merged

Improve Matrix message body parsing with smart format detection#13
fmartingr merged 4 commits into
masterfrom
fix/body-markdown

Conversation

@fmartingr

@fmartingr fmartingr commented Jul 15, 2025

Copy link
Copy Markdown
Contributor

Summary

  • Add smart format detection with proper HTML to markdown conversion
  • Include development Matrix Synapse server setup for testing

Changes Made

Message Parsing Improvements

  • Enhanced extractMatrixMessageContent: Automatically converts HTML content to markdown using existing conversion functions
Matrix Mattermost
Screenshot 2025-07-16 at 13 22 55 Screenshot 2025-07-16 at 13 23 01

Development Setup

  • Matrix Synapse server: Added Docker Compose setup for local development and testing
  • Configuration files: Included homeserver.yaml and docker-compose.yml for easy setup

🤖 Generated with Claude Code and simplified manually.

- Replace complex 6-path logic with simplified single-path approach
- Add smart format detection: prefer plain text body, convert HTML to markdown
- Use Matrix format field for reliable HTML detection, fall back to content analysis
- Remove unused functions (isMarkdown, detectContentFormat) identified by linter
- Fix linting issues (formatting, static check optimizations)
- Maintain Matrix spec compliance: body=plain text, formatted_body=HTML
- Preserve message formatting through HTML-to-markdown conversion

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@wiggin77

Copy link
Copy Markdown
Member

Use body field over formatted_body when available (Matrix spec compliance)

This seems the opposite of what we would want. If formatted text is available, that's what we should use. It is supposed to convert that code to markdown.

@fmartingr

fmartingr commented Jul 16, 2025

Copy link
Copy Markdown
Contributor Author

Use body field over formatted_body when available (Matrix spec compliance)

This seems the opposite of what we would want. If formatted text is available, that's what we should use. It is supposed to convert that code to markdown.

We are receiving HTML for that, and checking their spec the only format for formatted_body is org.matrix.custom.html.

https://spec.matrix.org/latest/client-server-api/#mroommessage-msgtypes

In the official python SDK, they provide the body field if set, either they remove the HTML from the formatted_body and return plain text: https://github.com/matrix-org/matrix-python-sdk/blob/master/matrix_client/room.py#L110-L116

I haven't set up a test environment yet, I need a local Matrix server, will try that later today.

fmartingr and others added 3 commits July 16, 2025 12:38
- Add docker-compose.yml with Synapse and PostgreSQL services
- Configure Synapse with proper permissions and database collation
- Add app service configuration for bridge registration
- Set room list publication rules for bridge user
- Update README with comprehensive setup instructions
- Add bridge registration file to gitignore

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Only use formatted_body when it differs from body to avoid unnecessary HTML conversion.
This improves handling of Matrix messages with rich formatting while maintaining
backward compatibility with plain text messages.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fix goimports formatting issue by replacing spaces with tabs in variable declaration.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@fmartingr fmartingr self-assigned this Jul 16, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves Matrix message body parsing by implementing smart format detection that automatically converts HTML content to markdown format. The changes enhance the bridge's ability to properly handle formatted messages from Matrix clients while maintaining compatibility with Mattermost's markdown format.

  • Enhanced message content extraction with HTML-to-markdown conversion
  • Added development infrastructure with Matrix Synapse server setup
  • Improved format detection logic using Matrix format fields and content analysis

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
server/sync_to_mattermost.go Updated comment to reflect new smart format detection behavior
server/bridge_utils.go Added HTML detection functions and enhanced message content extraction with format conversion
docker/synapse_config.yaml Added Matrix Synapse server configuration for development testing
docker-compose.yml Added Docker Compose setup for local Matrix server with PostgreSQL
README.md Added comprehensive documentation for local development setup with Matrix Synapse

Comment thread server/bridge_utils.go
}

return ""
return content

Copilot AI Jul 16, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function can return an empty string when event.Content["body"] doesn't exist and no formatted_body is processed. The fallback to plain body extraction was removed but not properly replaced.

Copilot uses AI. Check for mistakes.
Comment thread server/bridge_utils.go
Comment thread docker/synapse_config.yaml

@davidkrauser davidkrauser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@fmartingr
fmartingr merged commit db88ee7 into master Jul 16, 2025
4 checks passed
@wiggin77
wiggin77 deleted the fix/body-markdown branch August 2, 2025 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants