mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Add markdown util.
This commit is contained in:
parent
69316a7c06
commit
06f46c5e6a
2 changed files with 37 additions and 0 deletions
0
aiogram/utils/__init__.py
Normal file
0
aiogram/utils/__init__.py
Normal file
37
aiogram/utils/markdown.py
Normal file
37
aiogram/utils/markdown.py
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
MD_SYMBOLS = '*_`'
|
||||||
|
|
||||||
|
|
||||||
|
def _rst(symbol, *content, sep=' '):
|
||||||
|
start, end = (symbol, symbol) if isinstance(symbol, str) else (symbol[0], symbol[1])
|
||||||
|
return start + sep.join(map(str, content)) + end
|
||||||
|
|
||||||
|
|
||||||
|
def text(*content, sep=' '):
|
||||||
|
return _rst('', *content, sep=sep)
|
||||||
|
|
||||||
|
|
||||||
|
def bold(*content, sep=' '):
|
||||||
|
return _rst(MD_SYMBOLS[0], *content, sep=sep)
|
||||||
|
|
||||||
|
|
||||||
|
def italic(*content, sep=' '):
|
||||||
|
return _rst(MD_SYMBOLS[1], *content, sep=sep)
|
||||||
|
|
||||||
|
|
||||||
|
def code(*content, sep=' '):
|
||||||
|
return _rst(MD_SYMBOLS[2], *content, sep=sep)
|
||||||
|
|
||||||
|
|
||||||
|
def pre(*content, sep='\n'):
|
||||||
|
return _rst(('```\n', '\n```'), *content, sep=sep)
|
||||||
|
|
||||||
|
|
||||||
|
def link(title, url):
|
||||||
|
return f"[{title}]({url})"
|
||||||
|
|
||||||
|
|
||||||
|
def escape_md(*content, sep=' '):
|
||||||
|
result = text(*content, sep=sep)
|
||||||
|
for symbol in MD_SYMBOLS + '[':
|
||||||
|
result = result.replace(symbol, '\\' + symbol)
|
||||||
|
return result
|
||||||
Loading…
Add table
Add a link
Reference in a new issue