Skip to content

Conversation

@idbrii
Copy link

@idbrii idbrii commented Oct 8, 2022

Fix #36.

If the topic contains chars that make markdown formatting difficult ([ ] `), escape them and omit fixed width styling.

Markdown parsers are often inconsistent, so hopefully simpler markdown is more likely to display properly for more users.

Never deployed, but I've tested the code separately based on some examples from #36. I don't know what actual values pop out of the db for topic -- I've guessed them based on vimhelpbot's responses.

My test code:

import re

tests = [
    {
        "topic": "`]",
        "link": "https://vimhelp.org/motion.txt.html#%60%5D",
        "doc": "motion",
    },
    {
        "topic": ":make",
        "link": "https://neovim.io/doc/user/quickfix.html#%3Amake",
        "doc": "quickfix",
    },
    {
        "topic": "c_CTRL-G",
        "link": "https://vimhelp.org/cmdline.txt.html#c_CTRL-G",
        "doc": "cmdline",
    },
    {
        "topic": "c_CTRL-L",
        "link": "https://vimhelp.org/cmdline.txt.html#c_CTRL-L",
        "doc": "cmdline",
    },
    {
        "topic": "pattern.txt",
        "link": "https://vimhelp.org/pattern.txt.html#pattern.txt",
        "doc": "pattern",
    },
    {
        "topic": "c_CTRL-F",
        "link": "https://vimhelp.org/cmdline.txt.html#c_CTRL-F",
        "doc": "cmdline",
    },
    {
        "topic": "search()",
        "link": "https://vimhelp.org/eval.txt.html#search%28%29",
        "doc": "eval",
    },
    {
        "topic": "c_CTRL-R_CTRL-W",
        "link": "https://vimhelp.org/cmdline.txt.html#c_CTRL-R_CTRL-W",
        "doc": "cmdline",
    },
    {
        "topic": "[I",
        "link": "https://vimhelp.org/tagsrch.txt.html#%5BI",
        "doc": "tagsrch",
    },
    {
        "topic": "[_CTRL-I",
        "link": "https://vimhelp.org/tagsrch.txt.html#%5B_CTRL-I",
        "doc": "tagsrch",
    },
]



for t in tests:
    topic = t["topic"]
    link = t["link"]
    doc = t["doc"]

    text = ""
    escaped, count = re.subn(r"([\[\]`])", r"\\\1", topic)
    if count > 0:
        topic = escaped
        text += "* [{}]({}) in _{}.txt_\n".format(topic, link, doc)
    else:
        text += "* [`{}`]({}) in _{}.txt_\n".format(topic, link, doc)

    print(text)

Which produces results that look okay on old reddit and new reddit:

* [\`\]](https://vimhelp.org/motion.txt.html#%60%5D) in _motion.txt_
* [`:make`](https://neovim.io/doc/user/quickfix.html#%3Amake) in _quickfix.txt_
* [`c_CTRL-G`](https://vimhelp.org/cmdline.txt.html#c_CTRL-G) in _cmdline.txt_
* [`c_CTRL-L`](https://vimhelp.org/cmdline.txt.html#c_CTRL-L) in _cmdline.txt_
* [`pattern.txt`](https://vimhelp.org/pattern.txt.html#pattern.txt) in _pattern.txt_
* [`c_CTRL-F`](https://vimhelp.org/cmdline.txt.html#c_CTRL-F) in _cmdline.txt_
* [`search()`](https://vimhelp.org/eval.txt.html#search%28%29) in _eval.txt_
* [`c_CTRL-R_CTRL-W`](https://vimhelp.org/cmdline.txt.html#c_CTRL-R_CTRL-W) in _cmdline.txt_
* [\[I](https://vimhelp.org/tagsrch.txt.html#%5BI) in _tagsrch.txt_
* [\[_CTRL-I](https://vimhelp.org/tagsrch.txt.html#%5B_CTRL-I) in _tagsrch.txt_

@heraldofsolace
Copy link
Owner

Looks good. Can you add some tests in test_bot.py?

Fix heraldofsolace#36.

If the topic contains chars that make markdown formatting difficult,
escape them and omit fixed width styling.

Markdown parsers are often inconsistent, so hopefully simpler markdown
is more likely to display properly for more users.

Never deployed or run. (I'm not setup for either.)
@idbrii
Copy link
Author

idbrii commented Nov 16, 2022

I added some tests. I don't have reddit's API auth or whatever else is necessary run it locally so I didn't try running the tests directly. They're a port of my above test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proper markdown for bracket commands

2 participants