mirror of
https://github.com/zyachel/quetre.git
synced 2025-04-01 20:17:36 +03:00
fix: fix outgoing url on error page
also refactor some related code
This commit is contained in:
parent
44229f8702
commit
595b720ee1
9 changed files with 32 additions and 35 deletions
4
app.js
4
app.js
|
@ -50,9 +50,7 @@ app.use(
|
|||
if (process.env.NODE_ENV === 'development') app.use(morgan('dev')); // for logging during development
|
||||
// middleware to add baseUrl to req object
|
||||
app.use((req, res, next) => {
|
||||
req.urlObj = new URL(
|
||||
`${req.protocol}://${req.get('host')}${req.originalUrl}`
|
||||
);
|
||||
req.urlObj = new URL(req.originalUrl, `${req.protocol}://${req.get('host')}`);
|
||||
next();
|
||||
});
|
||||
|
||||
|
|
|
@ -35,9 +35,8 @@ const sendErrorResponse = (err, req, res, devMode = false) => {
|
|||
},
|
||||
meta: {
|
||||
title: 'Error',
|
||||
url: `${req.urlObj.origin}${req.urlObj.pathname}`,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
urlObj: req.urlObj,
|
||||
description: `ERROR: ${err.message}. Please try again later.`,
|
||||
},
|
||||
});
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import catchAsyncErrors from '../utils/catchAsyncErrors.js';
|
||||
import getAnswers from '../fetchers/getAnswers.js';
|
||||
import getTopic from '../fetchers/getTopic.js';
|
||||
import { nonSlugRoutes } from '../utils/constants.js';
|
||||
import { acceptedLanguages, nonSlugRoutes } from '../utils/constants.js';
|
||||
import getProfile from '../fetchers/getProfile.js';
|
||||
import getSearch from '../fetchers/getSearch.js';
|
||||
|
||||
|
@ -16,7 +16,7 @@ export const about = (req, res, next) => {
|
|||
res.render('about', {
|
||||
meta: {
|
||||
title: 'About',
|
||||
url: `${req.urlObj.origin}${req.urlObj.pathname}`,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
description:
|
||||
'Quetre is a libre front-end for Quora. See any answer without being tracked, without being required to log in, and without being bombarded by pesky ads.',
|
||||
|
@ -28,7 +28,7 @@ export const privacy = (req, res, next) => {
|
|||
res.render('privacy', {
|
||||
meta: {
|
||||
title: 'Privacy',
|
||||
url: `${req.urlObj.origin}${req.urlObj.pathname}`,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
description: 'Privacy Policy of Quetre, a libre front-end for Quora.',
|
||||
},
|
||||
|
@ -44,14 +44,14 @@ export const answers = catchAsyncErrors(async (req, res, next) => {
|
|||
|
||||
const answersData = await getAnswers(slug, lang);
|
||||
const title = answersData.question.text[0].spans
|
||||
.map((span) => span.text)
|
||||
.map(span => span.text)
|
||||
.join('');
|
||||
|
||||
return res.status(200).render('answers', {
|
||||
data: answersData,
|
||||
meta: {
|
||||
title,
|
||||
url: `${req.urlObj.origin}${req.urlObj.pathname}`,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
description: `Answers to ${title}`,
|
||||
},
|
||||
|
@ -68,7 +68,7 @@ export const topic = catchAsyncErrors(async (req, res, next) => {
|
|||
data: topicData,
|
||||
meta: {
|
||||
title: topicData.name,
|
||||
url: `${req.urlObj.origin}${req.urlObj.pathname}`,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
description: `Information about ${topicData.name} topic.`,
|
||||
},
|
||||
|
@ -84,7 +84,7 @@ export const profile = catchAsyncErrors(async (req, res, next) => {
|
|||
data: profileData,
|
||||
meta: {
|
||||
title: profileData.basic.name,
|
||||
url: `${req.urlObj.origin}${req.urlObj.pathname}`,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
description: `${profileData.basic.name}'s profile.`,
|
||||
},
|
||||
|
@ -101,7 +101,7 @@ export const search = catchAsyncErrors(async (req, res, next) => {
|
|||
data: searchData,
|
||||
meta: {
|
||||
title: searchText || 'Search',
|
||||
url: req.urlObj.href,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
description: searchText ? `results for '${searchText}'` : 'search page',
|
||||
},
|
||||
|
@ -118,9 +118,8 @@ export const unimplemented = (req, res, next) => {
|
|||
data,
|
||||
meta: {
|
||||
title: 'Not yet implemented',
|
||||
url: `${req.urlObj.origin}${req.urlObj.pathname}`,
|
||||
url: req.urlObj,
|
||||
imageUrl: `${req.urlObj.origin}/icon.svg`,
|
||||
urlObj: req.urlObj,
|
||||
description: data.message,
|
||||
},
|
||||
});
|
||||
|
|
|
@ -43,8 +43,7 @@ const fetcher = async (
|
|||
return true;
|
||||
});
|
||||
|
||||
if (!rawData || !Object.entries(rawData).length)
|
||||
throw new AppError("couldn't retrieve data", 500);
|
||||
if (!rawData) throw new AppError("couldn't retrieve data", 500);
|
||||
|
||||
return JSON.parse(rawData);
|
||||
} catch (err) {
|
||||
|
|
|
@ -13,25 +13,25 @@ mixin proxifyImg(imgUrl)
|
|||
img(src=imgUrl.replace('https://', '/api/v1/image/'), loading='lazy')&attributes(attributes)
|
||||
|
||||
mixin quetrefyUrl(url, text)
|
||||
-
|
||||
-
|
||||
const acceptedLanguages=['en','es','fr','de','it','ja','id','pt','hi','nl','da','fi','nb','sv','mr','bn','ta','ar','he','gu','kn','ml','te','po'];
|
||||
let link;
|
||||
const acceptedLanguages=['en','es','fr','de','it','ja','id','pt','hi','nl','da','fi','nb','sv','mr','bn','ta','ar','he','gu','kn','ml','te','po',];
|
||||
try {
|
||||
link = new URL(url);
|
||||
const subdomain = link.hostname.split('.')[0];
|
||||
if (!link.hostname.includes('quora.com')) link = url;
|
||||
else if (subdomain === 'www') return link.pathname;
|
||||
else if (acceptedLanguages.includes(subdomain)) link = `${link.pathname}?lang=${subdomain}`;
|
||||
else link = `/space/${subdomain}${link.pathname}`;
|
||||
} catch {
|
||||
const match = /^https:\/\/(.{2,})\.quora\.com(\/.*)$/.exec(url);
|
||||
|
||||
if (match) {
|
||||
const [_, subdomain, rest] = match;
|
||||
if (acceptedLanguages.includes(subdomain)) link = link = `${rest}${rest.includes('?') ? '&' : '?'}lang=${subdomain}`;
|
||||
else if (subdomain === 'www') link = rest;
|
||||
else link = `/space/${subdomain}${rest}`;
|
||||
} else {
|
||||
link = url;
|
||||
}
|
||||
|
||||
|
||||
a(href=link)&attributes(attributes)= text
|
||||
|
||||
mixin quorafyUrl(url)
|
||||
mixin quorafyUrl(url, text='View on Quora')
|
||||
-
|
||||
const link = new URL(url, 'https://www.quora.com');
|
||||
const link = new URL(`${url.pathname}${url.search}`, 'https://www.quora.com');
|
||||
const lang = link.searchParams.get('lang');
|
||||
const match = /(?<=^\/space\/)([^\/]+)\/(.*)$/.exec(link.pathname); // taking space name and rest of pathname out.
|
||||
if(lang) {
|
||||
|
@ -42,4 +42,4 @@ mixin quorafyUrl(url)
|
|||
link.pathname = match[2];
|
||||
}
|
||||
|
||||
a(href=link.href, rel='noreferrer', target='_blank')&attributes(attributes) View on Quora
|
||||
a(href=link.href, rel='noreferrer', target='_blank')&attributes(attributes)= text
|
|
@ -4,6 +4,7 @@
|
|||
extends ../base
|
||||
include ../mixins/_formatText
|
||||
include ../mixins/_answer
|
||||
include ../mixins/_utils
|
||||
|
||||
//-//////////////////////////////////////////////////////
|
||||
//- MAIN CONTENT
|
||||
|
@ -18,7 +19,7 @@ block content
|
|||
.answers__metadata
|
||||
p.answers__answers-total= `${ data.numAnswers ? 'Total answers: ' + data.numAnswers : 'Unanswered'}`
|
||||
p.answers__answers-shown Viewable answers: #{data.answers.length}
|
||||
+quorafyUrl(data.question.url)(class='answers__question-link answers__link')
|
||||
+quorafyUrl(meta.url)(class='answers__question-link answers__link')
|
||||
|
||||
//- ANSWERS TO THIS QUESTION
|
||||
.answers-box.answers__answers-box
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
//- INCLUDES/EXTENDS
|
||||
//-//////////////////////////////////////////////////////
|
||||
extends ../base
|
||||
include ../mixins/_utils
|
||||
|
||||
//-//////////////////////////////////////////////////////
|
||||
//- MAIN CONTENT
|
||||
|
@ -22,5 +23,5 @@ block content
|
|||
a.error__link(href="/") Home Page
|
||||
|.
|
||||
p.error__return Or view this route
|
||||
a.error__link(href="https://www.quora.com" + meta.urlObj.pathname) on Quora
|
||||
+quorafyUrl(meta.url, 'on Quora')(class='error__link')
|
||||
|.
|
|
@ -60,7 +60,7 @@ block content
|
|||
+addMetadataSecondary('flower', 'Deceased', '', 'empty')
|
||||
if data.basic.isBusiness
|
||||
+addMetadataSecondary('briefcase', 'Business', '', 'empty')
|
||||
+quorafyUrl(data.basic.profile)(class='link')
|
||||
+quorafyUrl(meta.url)(class='link')
|
||||
|
||||
if data.profileFeed.description[0].spans[0].text
|
||||
.profile-meta__description
|
||||
|
|
|
@ -24,7 +24,7 @@ block content
|
|||
+addMetadataSecondary('question','Questions', data.numQuestions)
|
||||
if data.isAdult
|
||||
+addMetadataSecondary('danger', 'Adult Topic', '18+', true)
|
||||
+quorafyUrl(data.url)(class='link')
|
||||
+quorafyUrl(meta.url)(class='link')
|
||||
|
||||
//- AUTHORS RELATED TO THE TOPIC AND METADATA
|
||||
.topic__famous-authors.famous-authors
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue