mv url localization away from pug templates

This commit is contained in:
teddit 2020-12-10 19:16:44 +01:00
parent 63c5435e20
commit 5c3ff13585
4 changed files with 26 additions and 34 deletions

View File

@ -35,12 +35,24 @@ module.exports = function(request, fs) {
return url.replace(/&/g, '&') return url.replace(/&/g, '&')
} }
this.localizeUrl = (url, dir) => { this.teddifyUrl = (url) => {
url = cleanUrl(url) try {
let temp_url = new URL(url) let u = new URL(url)
if(!dir) if(u.host === 'www.reddit.com' || u.host === 'reddit.com') {
dir = '' url = url.replace(u.host, 'teddit.net')
return url.replace(temp_url.origin, `${protocol}${domain}${dir}`) }
if(u.host === 'i.redd.it' || u.host === 'v.redd.it') {
let image_exts = ['png', 'jpg', 'jpeg']
let video_exts = ['mp4', 'gif', 'gifv']
let file_ext = getFileExtension(url)
if(image_exts.includes(file_ext))
url = url.replace(`${u.host}/`, 'teddit.net/pics/w:null_')
if(video_exts.includes(file_ext) || !image_exts.includes(file_ext))
url = url.replace(u.host, 'teddit.net/vids') + '.mp4'
}
} catch(e) { }
return url
} }
this.kFormatter = (num) => { this.kFormatter = (num) => {

View File

@ -20,9 +20,9 @@ module.exports = function(fetch) {
locked: post.locked, locked: post.locked,
name: post_id, name: post_id,
num_comments: post.num_comments, num_comments: post.num_comments,
permalink: post.permalink, permalink: teddifyUrl(post.permalink),
title: post.title, title: post.title,
url: post.url, url: teddifyUrl(post.url),
ups: post.ups, ups: post.ups,
id: post.id, id: post.id,
domain: post.domain, domain: post.domain,
@ -79,7 +79,7 @@ module.exports = function(fetch) {
name: post.crosspost.name, name: post.crosspost.name,
num_comments: post.crosspost.num_comments, num_comments: post.crosspost.num_comments,
id: post.crosspost.id, id: post.crosspost.id,
permalink: post.crosspost.permalink, permalink: teddifyUrl(post.crosspost.permalink),
ups: post.crosspost.ups, ups: post.crosspost.ups,
selftext: unescape(post.selftext_html), selftext: unescape(post.selftext_html),
selftext_crosspost: unescape(post.crosspost.selftext_html), selftext_crosspost: unescape(post.crosspost.selftext_html),
@ -136,7 +136,7 @@ module.exports = function(fetch) {
score: comment.score, score: comment.score,
ups: comment.ups, ups: comment.ups,
id: comment.id, id: comment.id,
permalink: comment.permalink, permalink: teddifyUrl(comment.permalink),
stickied: comment.stickied, stickied: comment.stickied,
distinguished: comment.distinguished, distinguished: comment.distinguished,
score_hidden: comment.score_hidden, score_hidden: comment.score_hidden,
@ -212,7 +212,7 @@ module.exports = function(fetch) {
score: reply.score, score: reply.score,
ups: reply.ups, ups: reply.ups,
id: reply.id, id: reply.id,
permalink: reply.permalink, permalink: teddifyUrl(reply.permalink),
stickied: reply.stickied, stickied: reply.stickied,
distinguished: reply.distinguished, distinguished: reply.distinguished,
score_hidden: reply.score_hidden, score_hidden: reply.score_hidden,
@ -247,7 +247,7 @@ module.exports = function(fetch) {
score: comment.score, score: comment.score,
ups: comment.ups, ups: comment.ups,
id: comment.id, id: comment.id,
permalink: comment.permalink, permalink: teddifyUrl(comment.permalink),
score_hidden: comment.score_hidden, score_hidden: comment.score_hidden,
distinguished: comment.distinguished, distinguished: comment.distinguished,
distinguished: comment.edited, distinguished: comment.edited,

View File

@ -69,26 +69,6 @@ meta(name="viewport", content="width=device-width, initial-scale=1.0")
} }
} }
function localize(url) {
try {
let u = new URL(url)
if(u.host === 'www.reddit.com' || u.host === 'reddit.com') {
url = url.replace(u.host, 'teddit.net')
}
if(u.host === 'i.redd.it' || u.host === 'v.redd.it') {
let image_exts = ['png', 'jpg', 'jpeg']
let video_exts = ['mp4', 'gif', 'gifv']
let file_ext = getFileExtension(url)
if(image_exts.includes(file_ext))
url = url.replace(`${u.host}/`, 'teddit.net/pics/w:null_')
if(video_exts.includes(file_ext) || !image_exts.includes(file_ext))
url = url.replace(u.host, 'teddit.net/vids') + '.mp4'
}
} catch(e) { }
return url
}
function toDateString(time) { function toDateString(time) {
let d = new Date(); let d = new Date();
d.setTime(time*1000); d.setTime(time*1000);

View File

@ -21,7 +21,7 @@ html
span #{kFormatter(post.ups)} span #{kFormatter(post.ups)}
div.arrow.down div.arrow.down
.title .title
a(href="" + localize(post.url) + "") a(href="" + post.url + "")
h2 #{cleanTitle(post.title)} h2 #{cleanTitle(post.title)}
span(class="domain") (#{post.domain}) span(class="domain") (#{post.domain})
p.submitted p.submitted
@ -31,7 +31,7 @@ html
if post.crosspost.is_crosspost === true if post.crosspost.is_crosspost === true
.crosspost .crosspost
.title .title
a(href="" + localize(post.crosspost.permalink) + "") a(href="" + post.crosspost.permalink + "")
h2 #{cleanTitle(post.crosspost.title)} h2 #{cleanTitle(post.crosspost.title)}
span(class="domain") (#{post.domain}) span(class="domain") (#{post.domain})
.num_comments .num_comments