Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions client/netlify/edge-functions/og-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,59 +166,59 @@ export default async (request, context) => {

// Replace page title
modifiedHtml = modifiedHtml.replace(
/<title>[^<]*<\/title>/,
/<title>[^<]*<\/title>/i,
`<title>${escapeHtml(ogTitle)}</title>`
);

// Replace meta description
modifiedHtml = modifiedHtml.replace(
/<meta name="description" content="[^"]*" \/>/,
/<meta[^>]*name=["']?description["']?[^>]*>/i,
`<meta name="description" content="${escapeHtml(ogDescription)}" />`
);

// Replace og:title
modifiedHtml = modifiedHtml.replace(
/<meta property="og:title" content="[^"]*" \/>/,
/<meta[^>]*property=["']?og:title["']?[^>]*>/i,
`<meta property="og:title" content="${escapeHtml(ogTitle)}" />`
);

// Replace og:description
modifiedHtml = modifiedHtml.replace(
/<meta property="og:description" content="[^"]*" \/>/,
/<meta[^>]*property=["']?og:description["']?[^>]*>/i,
`<meta property="og:description" content="${escapeHtml(ogDescription)}" />`
);

// Replace og:image
modifiedHtml = modifiedHtml.replace(
/<meta property="og:image" content="[^"]*" \/>/,
/<meta[^>]*property=["']?og:image["']?[^>]*>/i,
`<meta property="og:image" content="${escapeHtml(ogImage)}" />`
);

// Replace og:url
modifiedHtml = modifiedHtml.replace(
/<meta property="og:url" content="[^"]*" \/>/,
/<meta[^>]*property=["']?og:url["']?[^>]*>/i,
`<meta property="og:url" content="${escapeHtml(ogUrl)}" />`
);

// Replace og:type to "article" for quote pages
modifiedHtml = modifiedHtml.replace(
/<meta property="og:type" content="[^"]*" \/>/,
/<meta[^>]*property=["']?og:type["']?[^>]*>/i,
`<meta property="og:type" content="article" />`
);

// Replace Twitter card metadata
modifiedHtml = modifiedHtml.replace(
/<meta name="twitter:title" content="[^"]*" \/>/,
/<meta[^>]*name=["']?twitter:title["']?[^>]*>/i,
`<meta name="twitter:title" content="${escapeHtml(ogTitle)}" />`
);

modifiedHtml = modifiedHtml.replace(
/<meta name="twitter:description" content="[^"]*" \/>/,
/<meta[^>]*name=["']?twitter:description["']?[^>]*>/i,
`<meta name="twitter:description" content="${escapeHtml(ogDescription)}" />`
);

modifiedHtml = modifiedHtml.replace(
/<meta name="twitter:image" content="[^"]*" \/>/,
/<meta[^>]*name=["']?twitter:image["']?[^>]*>/i,
`<meta name="twitter:image" content="${escapeHtml(ogImage)}" />`
);

Expand Down
12 changes: 11 additions & 1 deletion server/app/data/resolvers/queries/post/getPost.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import mongoose from 'mongoose';
import PostModel from '../../models/PostModel';

export const getPost = (pubsub) => {
return async (_, args, context) => {
const post = await PostModel.findById(args.postId);
let post;

// Check if the provided postId is a valid MongoDB ObjectId
if (mongoose.Types.ObjectId.isValid(args.postId)) {
post = await PostModel.findById(args.postId);
} else {
// If it's not a valid ObjectId, assume it's a short URL ID (urlId)
post = await PostModel.findOne({ urlId: args.postId });
}

if (!post || post.deleted) {
if (context && context.res) {
context.res.status(404);
Expand Down
26 changes: 26 additions & 0 deletions test_fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const postId = "e_6A4M";
const graphqlUrl = 'https://api.quote.vote/graphql';
const graphqlQuery = {
query: `
query post($postId: String!) {
post(postId: $postId) {
_id
title
text
url
creator {
name
avatar
}
}
}
`,
variables: { postId },
};
fetch(graphqlUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(graphqlQuery),
}).then(res => res.json()).then(data => console.log(JSON.stringify(data, null, 2))).catch(err => console.error(err));
9 changes: 9 additions & 0 deletions test_get_posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const graphqlUrl = 'https://api.quote.vote/graphql';
fetch(graphqlUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: '{ posts(limit: 5, offset: 0, searchKey: "", sortOrder: "newest") { entities { _id title url } } }' })
})
.then(r => r.json())
.then(d => console.log(JSON.stringify(d, null, 2)))
.catch(console.error);
5 changes: 5 additions & 0 deletions test_regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('fs');
let html = fs.readFileSync('client/index.html', 'utf8');
const ogTitle = 'Test Title';
html = html.replace(/<meta property="og:title" content="[^"]*" \/>/, `<meta property="og:title" content="${ogTitle}" />`);
console.log("Matched:", html.includes(ogTitle));
16 changes: 16 additions & 0 deletions test_regex2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const ogTitle = 'Replaced Title';
const testCases = [
'<meta property="og:title" content="Quote.Vote – The Internet\'s Quote Board" />',
'<meta property="og:title" content="Quote.Vote – The Internet\'s Quote Board">',
'<meta property=og:title content="Quote.Vote – The Internet\'s Quote Board">',
'<meta content="Quote.Vote – The Internet\'s Quote Board" property="og:title" />',
];

const regex = /<meta[^>]*property=["']?og:title["']?[^>]*>/i;

testCases.forEach(html => {
const result = html.replace(regex, `<meta property="og:title" content="${ogTitle}" />`);
console.log("Original:", html);
console.log("Replaced:", result);
console.log("---");
});