mirror of
https://github.com/maubot/maubot.git
synced 2024-10-01 01:06:10 -04:00
Make parse_formatted output links in a nicer way than markdown
This commit is contained in:
parent
98eeb6a808
commit
341b3db99e
@ -18,12 +18,24 @@ from html import escape
|
||||
import attr
|
||||
|
||||
from mautrix.client import Client as MatrixClient, SyncStream
|
||||
from mautrix.util.formatter import parse_html
|
||||
from mautrix.util.formatter import MatrixParser, MarkdownString, EntityType
|
||||
from mautrix.util import markdown
|
||||
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
||||
MessageType, TextMessageEventContent, Format, RelatesTo)
|
||||
|
||||
|
||||
class HumanReadableString(MarkdownString):
|
||||
def format(self, entity_type: EntityType, **kwargs) -> 'MarkdownString':
|
||||
if entity_type == EntityType.URL and kwargs['url'] != self.text:
|
||||
self.text = f"{self.text} ({kwargs['url']})"
|
||||
return self
|
||||
return super(HumanReadableString, self).format(entity_type, **kwargs)
|
||||
|
||||
|
||||
class MaubotHTMLParser(MatrixParser[HumanReadableString]):
|
||||
e = HumanReadableString
|
||||
|
||||
|
||||
def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True
|
||||
) -> Tuple[str, str]:
|
||||
if render_markdown:
|
||||
@ -32,7 +44,7 @@ def parse_formatted(message: str, allow_html: bool = False, render_markdown: boo
|
||||
html = message
|
||||
else:
|
||||
return message, escape(message)
|
||||
return parse_html(html), html
|
||||
return MaubotHTMLParser.parse(html).text, html
|
||||
|
||||
|
||||
class MaubotMessageEvent(MessageEvent):
|
||||
|
Loading…
Reference in New Issue
Block a user