Skip to content

Commit

Permalink
Switch to commonmark-formatting for markdown-messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alturiak committed Apr 15, 2023
1 parent 2ba7540 commit 74433d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
24 changes: 8 additions & 16 deletions core/chat_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import blurhash

from nio import SendRetryError, RoomSendResponse, Event, RoomGetEventResponse, RoomGetEventError, UploadResponse, AsyncClient, RoomSendError
from markdown import markdown
import commonmark

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -96,23 +96,19 @@ async def send_text_to_room(client: AsyncClient, room_id: str, message, notice=T
msgtype = "m.notice" if notice else "m.text"

if markdown_convert:
formatted_message: str = markdown(message)
formatted_message: str = commonmark.commonmark(message)
else:
formatted_message: str = message

content = {
"msgtype": msgtype,

# legacy format
"body": strip_tags(message),
"format": "org.matrix.custom.html",
"formatted_body": formatted_message,

# MSC1767
"m.message": [
{"mimetype": "text/plain", "body": strip_tags(message)},
{"mimetype": "text/html", "body": formatted_message}
]
"m.message": [{"mimetype": "text/plain", "body": strip_tags(message)},
{"mimetype": "text/html", "body": formatted_message}],
}

response: RoomSendResponse
Expand Down Expand Up @@ -174,20 +170,16 @@ async def send_replace(client, room_id: str, event_id: str, message: str, messag
"format": "org.matrix.custom.html",
"body": strip_tags(message),
"formatted_body": markdown(message),
"m.message": [
{"mimetype": "text/plain", "body": strip_tags(message)},
{"mimetype": "text/html", "body": markdown(message)}
]
"m.message": [{"mimetype": "text/plain", "body": strip_tags(message)},
{"mimetype": "text/html", "body": markdown(message)}],
},
"m.relates_to": {"rel_type": "m.replace", "event_id": event_id},
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"body": strip_tags(message),
"formatted_body": markdown(message),
"m.message": [
{"mimetype": "text/plain", "body": strip_tags(message)},
{"mimetype": "text/html", "body": markdown(message)}
]
"m.message": [{"mimetype": "text/plain", "body": strip_tags(message)},
{"mimetype": "text/html", "body": markdown(message)}],
}

# check if there are any differences in body or formatted_body before actually sending the m.replace-event
Expand Down
5 changes: 5 additions & 0 deletions plugins/sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ List all users in the room by their homeserver
Usage: `sample_get_client`
Sends a message using plugin.get_client() instead of the command's client

### sample_markdown

Usage: `sample_markdown`
Post a message containing a markdown code block.

## Configuration

Configuration options in `sample.yaml`
Expand Down
20 changes: 19 additions & 1 deletion plugins/sample/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ def setup():
)

"""Get the bot's client instance from plugin.get_client instead of the command"""
plugin.add_command("sample_get_client", sample_get_client, "Post a message using the client instance of `plugin.get_client()`")
plugin.add_command("sample_get_client", sample_get_client,
"Post a message using the client instance of `plugin.get_client()`")

"""Markdown sample"""
plugin.add_command("sample_markdown", sample_markdown, "Post a message containing a markdown code block.")

"""The following part demonstrates registering timers by fixed interval and timedelta"""
if timers_enabled:
Expand Down Expand Up @@ -524,4 +528,18 @@ async def sample_get_client(command: Command):
await plugin.send_notice(await plugin.get_client(), command.room.room_id, "This message uses plugin.get_client()")


async def sample_markdown(command: Command):
"""
Post a message containing a markdown code block.
:param command:
:return:
"""

await plugin.send_notice(
await plugin.get_client(),
command.room.room_id,
"This is a test message header\n\n" "```python\n" "import markdown\n" "```\n\n" "This is a test message footer",
)


setup()

0 comments on commit 74433d1

Please sign in to comment.