Browse Source

Don't linkify group refs starting with a number

This was a little more strict before and would only skip linkification
if the entire path was digits and/or periods. However, I've seen it
still hitting some people if they write things like "~100k". It's very
unlikely that we're ever going to have a top-level group with a name
starting with a number, so let's just skip linkification for all
instances where a number is the first thing.
merge-requests/64/head
Deimos 6 years ago
parent
commit
cea200852d
  1. 2
      tildes/tests/test_markdown.py
  2. 16
      tildes/tildes/lib/markdown.py

2
tildes/tests/test_markdown.py

@ -250,7 +250,7 @@ def test_invalid_group_reference_not_linkified():
def test_approximately_tilde_not_linkified():
"""Ensure a tilde in front of a number doesn't linkify."""
markdown = "Mix in ~2 cups of flour and ~1.5 tbsp of sugar."
markdown = "Mix in ~2 cups of flour and ~1.5 tbsp of sugar with ~3kg of meat."
processed = convert_markdown_to_safe_html(markdown)
assert "<a" not in processed

16
tildes/tildes/lib/markdown.py

@ -404,14 +404,14 @@ class LinkifyFilter(Filter):
# casing but still have it link properly
group_path = match[1].lower()
# Even though they're technically valid paths, we don't want to linkify things
# like "~10" or "~4.5" since that's just going to be someone using it in the
# "approximately" sense. So if the path consists of only numbers and/or periods,
# we won't linkify it
is_numeric = all(char in "0123456789." for char in group_path)
# if it's a valid group path and not totally numeric, convert to <a>
if is_valid_group_path(group_path) and not is_numeric:
# Even though they're technically valid paths, we don't want to linkify anything
# starting with a number like "~10" or "~4.5", since that's just going to be
# someone using it in the "approximately" sense. This will be a problem if a
# top-level group's name ever starts with a number, but I think that's unlikely.
is_ignored = group_path.startswith(tuple("0123456789"))
# if it's a valid group path and not ignored by the above logic, convert to <a>
if is_valid_group_path(group_path) and not is_ignored:
return [
{
"type": "StartTag",

Loading…
Cancel
Save