From 53ccc255865453d294e2fc2cb06e1aee1ba76a4f Mon Sep 17 00:00:00 2001 From: Deimos Date: Sat, 12 Jan 2019 18:46:31 -0700 Subject: [PATCH] Style user and group links in text differently Now that all links in text have underlines by default, I think this looks pretty strange for ~group and @user links, which are quite common and unnecessary to have underlined all the time. This modifies the markdown parser to add link-user and link-group classes to these links, which allows them to be styled differently. In addition, some of the markdown tests needed to be changed to use BeautifulSoup instead of simple string-matching, since it's not as simple to recognize links any more (and the order of attrs might change). --- tildes/scss/modules/_link.scss | 5 +++++ tildes/tests/test_markdown.py | 28 +++++++++++++++++++--------- tildes/tildes/lib/markdown.py | 10 ++++++++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/tildes/scss/modules/_link.scss b/tildes/scss/modules/_link.scss index e589d51..f41e5b4 100644 --- a/tildes/scss/modules/_link.scss +++ b/tildes/scss/modules/_link.scss @@ -3,4 +3,9 @@ a.link-user, a.link-group { white-space: nowrap; + text-decoration: none; + + &:hover { + text-decoration: underline; + } } diff --git a/tildes/tests/test_markdown.py b/tildes/tests/test_markdown.py index 1ed212e..758f3c9 100644 --- a/tildes/tests/test_markdown.py +++ b/tildes/tests/test_markdown.py @@ -1,6 +1,8 @@ # Copyright (c) 2018 Tildes contributors # SPDX-License-Identifier: AGPL-3.0-or-later +from bs4 import BeautifulSoup + from tildes.lib.markdown import convert_markdown_to_safe_html @@ -201,7 +203,8 @@ def test_group_reference_linkified(): markdown = "Yeah, I saw that in ~books.fantasy yesterday." processed = convert_markdown_to_safe_html(markdown) - assert '' in processed + soup = BeautifulSoup(processed, features="html5lib") + assert soup.find("a", href="/~books.fantasy") def test_multiple_group_references_linkified(): @@ -214,7 +217,8 @@ def test_multiple_group_references_linkified(): ) processed = convert_markdown_to_safe_html(markdown) - assert processed.count("' in processed + soup = BeautifulSoup(processed, features="html5lib") + assert soup.find("a", href="/~group.reference") def test_username_reference_linkified(): @@ -288,7 +294,8 @@ def test_username_reference_linkified(): markdown = "Hey @SomeUser, what do you think of this?" processed = convert_markdown_to_safe_html(markdown) - assert '@SomeUser' in processed + soup = BeautifulSoup(processed, features="html5lib") + assert soup.find("a", href="/user/SomeUser") def test_u_style_username_ref_linked(): @@ -296,7 +303,8 @@ def test_u_style_username_ref_linked(): markdown = "Hey /u/SomeUser, what do you think of this?" processed = convert_markdown_to_safe_html(markdown) - assert '/u/SomeUser' in processed + soup = BeautifulSoup(processed, features="html5lib") + assert soup.find("a", href="/user/SomeUser") def test_u_alt_style_username_ref_linked(): @@ -304,7 +312,8 @@ def test_u_alt_style_username_ref_linked(): markdown = "Hey u/SomeUser, what do you think of this?" processed = convert_markdown_to_safe_html(markdown) - assert 'u/SomeUser' in processed + soup = BeautifulSoup(processed, features="html5lib") + assert soup.find("a", href="/user/SomeUser") def test_accidental_u_alt_style_not_linked(): @@ -320,8 +329,9 @@ def test_username_and_group_refs_linked(): markdown = "@SomeUser makes the best posts in ~some.group for sure" processed = convert_markdown_to_safe_html(markdown) - assert '@SomeUser' in processed - assert '~some.group' in processed + soup = BeautifulSoup(processed, features="html5lib") + assert soup.find("a", href="/user/SomeUser") + assert soup.find("a", href="/~some.group") def test_invalid_username_not_linkified(): diff --git a/tildes/tildes/lib/markdown.py b/tildes/tildes/lib/markdown.py index 4d4d249..6d130f8 100644 --- a/tildes/tildes/lib/markdown.py +++ b/tildes/tildes/lib/markdown.py @@ -376,7 +376,10 @@ class LinkifyFilter(Filter): { "type": "StartTag", "name": "a", - "data": {(None, "href"): f"/~{group_path}"}, + "data": { + (None, "class"): "link-group", + (None, "href"): f"/~{group_path}", + }, }, {"type": "Characters", "data": match[0]}, {"type": "EndTag", "name": "a"}, @@ -394,7 +397,10 @@ class LinkifyFilter(Filter): { "type": "StartTag", "name": "a", - "data": {(None, "href"): f"/user/{match[1]}"}, + "data": { + (None, "class"): "link-user", + (None, "href"): f"/user/{match[1]}", + }, }, {"type": "Characters", "data": match[0]}, {"type": "EndTag", "name": "a"},