telegramify-markdown: bug: `strict_markdown` obsolete, format entity syntax outdated
The strict_markdown value in the customize module has no effect on Markdown_V2 formatted output given the current syntax rules expected by the Telegram bot API.
Additionally, the strikethrough and underline syntax no longer behaves as expected, regardless of the strict setting.
Finally, the documented behavior of closing multiple nested entities is not being handled when parsing the syntax tree (eg, using the empty bold entity to separate the closing underline from the closing italic entities: __**_*). The nested entity parsing most likely will fix itself once the syntax grammar has been updated, I expect.
Testing the two strings below (the first based on Telegramโs API syntax spec, the second using the expected syntax documented by telegramify-markdown):
import asyncio
import telegramify_markdown as tgmd
from aiogram import Bot
SYNTAX_STR_TELEGRAM = r"""
*bold text*
_italic text_
__underline__
~strikethrough~
||spoiler||
*bold & _italic & __underline & ~strikethrough & ||spoiler||~__**_*
"""
SYNTAX_STR_TELEGRAMIFY = r"""
*bold text*
_italic text_
__underline__
~~strikethrough~~
||spoiler||
*bold & _italic & __underline & ~~strikethrough & ||spoiler||~~__**_*
"""
def build_strings(test, text):
strings = []
strings.append("\\# Raw markdown string" + text)
tgmd.customize.strict_markdown = True
strings.append(tgmd.markdownify(f"# Markdownify (strict={tgmd.customize.strict_markdown})" + text))
tgmd.customize.strict_markdown = False
strings.append(tgmd.markdownify(f"# Markdownify (strict={tgmd.customize.strict_markdown})" + text))
return test, text, "\n".join(strings)
async def main():
async with Bot(TOKEN) as bot:
test, input, output = build_strings("Telegram Markdown V2 syntax:", SYNTAX_STR_TELEGRAM)
print(f"{test} [RAW INPUT]:" + input)
print(f"{test} [PROCESSED]:\n" + output)
await bot.send_message(CHAT_ID, test + "\n\n" + output, parse_mode="MarkdownV2")
test, input, output = build_strings("Telegramify Markdown V2 syntax:", SYNTAX_STR_TELEGRAMIFY)
print(f"{test} [RAW INPUT]:" + input)
print(f"{test} [PROCESSED]:\n" + output)
await bot.send_message(CHAT_ID, test + "\n\n" + output, parse_mode="MarkdownV2")
asyncio.run(main())
Telegram syntax
Console output:
\# Raw markdown string
*bold text*
_italic text_
__underline__
~strikethrough~
||spoiler||
*bold & _italic & __underline & ~strikethrough & ||spoiler||~__**_*
*๐ Markdownify \(strict\=True\)*
*bold text*
_italic text_
*underline*
\~strikethrough\~
||spoiler||
\*bold & _italic & *underline & \~strikethrough & ||spoiler||\~*\*\*_\*
*๐ Markdownify \(strict\=False\)*
*bold text*
_italic text_
*underline*
\~strikethrough\~
||spoiler||
\*bold & _italic & *underline & \~strikethrough & ||spoiler||\~*\*\*_\*
Telegramify syntax
Console output:
\# Raw markdown string
*bold text*
_italic text_
__underline__
~~strikethrough~~
||spoiler||
*bold & _italic & __underline & ~~strikethrough & ||spoiler||~~__**_*
*๐ Markdownify \(strict\=True\)*
*bold text*
_italic text_
*underline*
~strikethrough~
||spoiler||
\*bold & _italic & *underline & ~strikethrough & ||spoiler||~*\*\*_\*
*๐ Markdownify \(strict\=False\)*
*bold text*
_italic text_
*underline*
~strikethrough~
||spoiler||
\*bold & _italic & *underline & ~strikethrough & ||spoiler||~*\*\*_\*
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (9 by maintainers)
Commits related to this issue
- โจ fix(customize): BREAKING - refactor as singleton class to ensure changes made after library initialization are reflected bug: `strict_markdown` obsolete, format entity syntax outdated #68 — committed to Ste1io/telegramify-markdown by Ste1io 2 years ago
- โจ fix(render): update emphasis and strong rendering see bug: `strict_markdown` obsolete, format entity syntax outdated #68 — committed to Ste1io/telegramify-markdown by Ste1io 2 years ago
A new version has been released to pypi to fix the issue
For subsequent maintenance considerations, I decided to create a new standardized method specifically for Markdown security with TelegramMarkdownV2 syntax. (This makes it easier for bot developers to create formatted messages) The original method is dedicated to the task of parsing normal Markdown. Thanks for your testing and problem discovery
Maybe we should just remove these little tricks?