feat(prefs): imgur & reddit link replacers

This commit is contained in:
taskylizard 2024-05-20 13:01:40 +00:00
parent a451755d2b
commit c53b8d4d8a
No known key found for this signature in database
GPG Key ID: 1820131ED1A24120
2 changed files with 17 additions and 0 deletions

View File

@ -21,6 +21,9 @@ let
# Images aren't supported due to errors from Teddit when the image # Images aren't supported due to errors from Teddit when the image
# wasn't first displayed via a post on the Teddit instance. # wasn't first displayed via a post on the Teddit instance.
imgurRegex = re"((i|i.stack)\.)?imgur\.(com|io)"
mediumRegex = re"([a-zA-Z0-9_.-]+\.)?medium\.com"
wwwRegex = re"https?://(www[0-9]?\.)?" wwwRegex = re"https?://(www[0-9]?\.)?"
m3u8Regex = re"""url="(.+.m3u8)"""" m3u8Regex = re"""url="(.+.m3u8)""""
userPicRegex = re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$" userPicRegex = re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$"
@ -69,6 +72,12 @@ proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string =
if prefs.replaceReddit in result and "/gallery/" in result: if prefs.replaceReddit in result and "/gallery/" in result:
result = result.replace("/gallery/", "/comments/") result = result.replace("/gallery/", "/comments/")
if prefs.replaceImgur.len > 0 and "imgur" in result:
result = result.replace(imgurRegex, prefs.replaceImgur)
if prefs.replaceMedium.len > 0 and "medium.com" in result:
result = result.replace(mediumRegex, prefs.replaceMedium)
if absolute.len > 0 and "href" in result: if absolute.len > 0 and "href" in result:
result = result.replace("href=\"/", &"href=\"{absolute}/") result = result.replace("href=\"/", &"href=\"{absolute}/")

View File

@ -107,6 +107,14 @@ genPrefs:
"Reddit -> Teddit/Libreddit" "Reddit -> Teddit/Libreddit"
placeholder: "Teddit hostname" placeholder: "Teddit hostname"
replaceImgur(input, ""):
"Imgur -> Rimgo"
placeholder: "Rimgo hostname"
replaceMedium(input, ""):
"Medium -> Scribe"
placeholder: "Scribe hostname"
iterator allPrefs*(): Pref = iterator allPrefs*(): Pref =
for k, v in prefList: for k, v in prefList:
for pref in v: for pref in v: