kopia lustrzana https://github.com/mkdryden/telegram-stats-bot
utils.py: escape_markdown handles urls and all reserved characters
rodzic
b8cfa98638
commit
70411cb4e0
|
@ -9,6 +9,10 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
|
|||
-------------
|
||||
`Unreleased`_
|
||||
-------------
|
||||
Fixed
|
||||
-----
|
||||
- Correctly escape all reserved markdown characters and markdown links
|
||||
|
||||
---------------------
|
||||
`0.2.0`_ - 2020-06-16
|
||||
---------------------
|
||||
|
|
|
@ -21,6 +21,13 @@
|
|||
|
||||
import re
|
||||
|
||||
md_match = re.compile(r"(\[[^][]*]\(http[^()]*\))|([_*[\]()~>#+-=|{}.!\\])")
|
||||
|
||||
|
||||
def escape_markdown(string: str) -> str:
|
||||
return re.sub(r'([\\_*\[\]()`])', r'\\\g<1>', string)
|
||||
def url_match(match: re.Match):
|
||||
if match.group(1):
|
||||
return f'{match.group(1)}'
|
||||
return f'\\{match.group(2)}'
|
||||
|
||||
return re.sub(md_match, url_match, string)
|
||||
|
|
Ładowanie…
Reference in New Issue