telegramify-markdown: bug: `strict_markdown` obsolete, format entity syntax outdated

https://github.com/sudoskys/telegramify-markdown/blob/9a8109099141d28c1d88ba57d6a6ffea3e7c16e5/src/telegramify_markdown/customize.py#L27

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||\~*\*\*_\*

Image

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||~*\*\*_\*

Image

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

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

    def render_strong(self, token: span_token.Strong) -> Iterable[Fragment]:
        if strict_markdown:
            # Telegram strong: *text*
            # Markdown strong: **text** or __text__
            return self.embed_span(Fragment('*'), token.children)
        else:
            # bold
            if token.delimiter == "*":
                return self.embed_span(Fragment(token.delimiter * 1), token.children)
            # underline
            return self.embed_span(Fragment(token.delimiter * 2), token.children)

Maybe we should just remove these little tricks?