Skip to content

Commit

Permalink
Fix Twitter link replacements
Browse files Browse the repository at this point in the history
Fixes #492
  • Loading branch information
zedeus committed Dec 30, 2021
1 parent d96550f commit 51c6605
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/formatters.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only
import strutils, strformat, times, uri, tables, xmltree, htmlparser
import strutils, strformat, times, uri, tables, xmltree, htmlparser, htmlgen
import regex
import types, utils, query

Expand All @@ -15,6 +15,8 @@ const
# wasn't first displayed via a post on the Teddit instance.

twRegex = re"(?<=(?<!\S)https:\/\/|(?<=\s))(www\.|mobile\.)?twitter\.com"
twLinkRegex = re"""<a href="https:\/\/twitter.com([^"]+)">twitter\.com(\S+)</a>"""

cards = "cards.twitter.com/cards"
tco = "https://t.co"

Expand Down Expand Up @@ -57,10 +59,12 @@ proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string =
result = result.replace("/c/", "/")

if prefs.replaceTwitter.len > 0 and
(twRegex in result or tco in result):
(twRegex in result or twLinkRegex in result or tco in result):
result = result.replace(tco, https & prefs.replaceTwitter & "/t.co")
result = result.replace(cards, prefs.replaceTwitter & "/cards")
result = result.replace(twRegex, prefs.replaceTwitter)
result = result.replace(twLinkRegex, a(
prefs.replaceTwitter & "$2", href = https & prefs.replaceTwitter & "$1"))

if prefs.replaceReddit.len > 0 and (rdRegex in result or "redd.it" in result):
result = result.replace(rdShortRegex, prefs.replaceReddit & "/comments/")
Expand Down

0 comments on commit 51c6605

Please sign in to comment.