Fix incorrect link replacements (#1016)

Fix link/result description getting replaced when alternative is disabled
(set to empty string)

Replace medium.com links with value from constant
main
Andiru 2023-06-26 23:47:43 +02:00 committed by GitHub
parent 179fc5e020
commit 29992985bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 10 deletions

View File

@ -557,17 +557,18 @@ class Filter:
is enabled is enabled
""" """
for site, alt in SITE_ALTS.items(): for site, alt in SITE_ALTS.items():
if site != "medium.com" and alt != "":
# Ignore medium.com replacements since these are handled
# specifically in the link description replacement, and medium
# results are never given their own "card" result where this
# replacement would make sense.
# Also ignore if the alt is empty, since this is used to indicate
# that the alt is not enabled.
for div in self.soup.find_all('div', text=re.compile(site)): for div in self.soup.find_all('div', text=re.compile(site)):
# Use the number of words in the div string to determine if the # Use the number of words in the div string to determine if the
# string is a result description (shouldn't replace domains used # string is a result description (shouldn't replace domains used
# in desc text). # in desc text).
# Also ignore medium.com replacements since these are handled if len(div.string.split(' ')) == 1:
# specifically in the link description replacement, and medium
# results are never given their own "card" result where this
# replacement would make sense.
if site == 'medium.com' or len(div.string.split(' ')) > 1:
continue
div.string = div.string.replace(site, alt) div.string = div.string.replace(site, alt)
for link in self.soup.find_all('a', href=True): for link in self.soup.find_all('a', href=True):
@ -592,7 +593,7 @@ class Filter:
# replaced (i.e. 'philomedium.com' should stay as it is). # replaced (i.e. 'philomedium.com' should stay as it is).
if 'medium.com' in link_str: if 'medium.com' in link_str:
if link_str.startswith('medium.com') or '.medium.com' in link_str: if link_str.startswith('medium.com') or '.medium.com' in link_str:
link_str = 'farside.link/scribe' + link_str[ link_str = SITE_ALTS['medium.com'] + link_str[
link_str.find('medium.com') + len('medium.com'):] link_str.find('medium.com') + len('medium.com'):]
new_desc.string = link_str new_desc.string = link_str
else: else: