Advertisement
disk6969

CustomMarkdown quote

Nov 1st, 2023 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. class CustomMarkdown:
  2.     @staticmethod
  3.     def parse(text):
  4.         text, entities = markdown.parse(text)
  5.         for i, e in enumerate(entities):
  6.             if isinstance(e, types.MessageEntityTextUrl):
  7.                 if e.url == 'spoiler':
  8.                     entities[i] = types.MessageEntitySpoiler(e.offset, e.length)
  9.                 elif e.url == 'quote':
  10.                     entities[i] = types.MessageEntityBlockquote(e.offset, e.length)
  11.                 elif e.url.startswith('emoji/'):
  12.                     entities[i] = types.MessageEntityCustomEmoji(e.offset, e.length, int(e.url.split('/')[1]))
  13.         return text, entities
  14.     @staticmethod
  15.     def unparse(text, entities):
  16.         for i, e in enumerate(entities or []):
  17.             if isinstance(e, types.MessageEntityCustomEmoji):
  18.                 entities[i] = types.MessageEntityTextUrl(e.offset, e.length, f'emoji/{e.document_id}')
  19.             if isinstance(e, types.MessageEntitySpoiler):
  20.                 entities[i] = types.MessageEntityTextUrl(e.offset, e.length, 'spoiler')
  21.             if isinstance(e, types.MessageEntityBlockquote):
  22.                 entities[i] = types.MessageEntityTextUrl(e.offset, e.length, 'quote')
  23.         return markdown.unparse(text, entities)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement