Skip to content
This repository was archived by the owner on Feb 21, 2026. It is now read-only.

Commit 346c5bb

Browse files
committed
Add method to md parser to extract text surrounded by entities
1 parent e5deaf5 commit 346c5bb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

telethon/extensions/markdown.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
since they seem to count as two characters and it's a bit strange.
55
"""
66
import re
7+
8+
from telethon.tl import TLObject
9+
710
from ..tl.types import (
811
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
912
MessageEntityPre, MessageEntityTextUrl
@@ -124,3 +127,23 @@ def parse(message, delimiters=None, url_re=None):
124127
)
125128

126129
return message.decode('utf-16le'), result
130+
131+
132+
def get_inner_text(text, entity):
133+
"""Gets the inner text that's surrounded by the given entity or entities.
134+
For instance: text = 'hey!', entity = MessageEntityBold(2, 2) -> 'y!'.
135+
"""
136+
if not isinstance(entity, TLObject) and hasattr(entity, '__iter__'):
137+
multiple = True
138+
else:
139+
entity = [entity]
140+
multiple = False
141+
142+
text = text.encode('utf-16le')
143+
result = []
144+
for e in entity:
145+
start = e.offset * 2
146+
end = (e.offset + e.length) * 2
147+
result.append(text[start:end].decode('utf-16le'))
148+
149+
return result if multiple else result[0]

0 commit comments

Comments
 (0)