Skip to content
Merged
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
52 changes: 33 additions & 19 deletions src/lib/server/metaTagsInjector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ import fs from "fs";
import matter from "gray-matter";
import path from "path";

/**
* Verifies if an image exists and has appropriate dimensions for social media
* @param {string} imagePath - Path to the image relative to static directory
* @returns {object} - Object containing verified image path and dimensions
*/
function verifyImage(imagePath) {
const defaultImage = "/assets/media/website_screenshot.png";
const localImagePath = path.join(process.cwd(), "static", imagePath);

if (!fs.existsSync(localImagePath)) {
console.warn(`Warning: Image not found at ${imagePath}. Using default.`);
return {
path: defaultImage,
width: 1200,
height: 630
};
}

return {
path: imagePath,
width: 1200, // Default OG image width
height: 630 // Default OG image height
};
}

/**
* Extracts metadata from a markdown file
* @param {string} slug - The slug/identifier for the blog post
Expand Down Expand Up @@ -167,27 +192,15 @@ export function injectMetaTags(html, url) {
? `/assets/og/${slug}.png`
: "/assets/media/website_screenshot.png";

// Verify image exists, fall back to default if not
let finalImagePath = customOgImagePath;
const localImagePath = path.join(
process.cwd(),
"static",
customOgImagePath
);

if (!fs.existsSync(localImagePath)) {
console.warn(`Warning: OG image not found for ${slug}. Using default.`);
finalImagePath = "/assets/media/website_screenshot.png";
}
// Verify image exists, get dimensions, fall back to default if needed
const verifiedImage = verifyImage(customOgImagePath);
const finalImagePath = verifiedImage.path;

// Make sure the image URL is absolute and doesn't have any special characters
const absoluteImageUrl = `${siteUrl}${finalImagePath.replace(
/\s/g,
"%20"
)}`;
const absoluteImageUrl = new URL(finalImagePath, siteUrl).toString();

// Generate absolute URL for the post - ensure no trailing slash
const absoluteUrl = `${siteUrl}/blog/${slug}`;
const absoluteUrl = new URL(`/blog/${slug}`, siteUrl).toString();

// Create the meta tag string - with escaping for special characters
// Enforce Twitter character limits
Expand Down Expand Up @@ -226,6 +239,7 @@ ${
<meta name="twitter:title" content="${safeTitle}" />
<meta name="twitter:description" content="${safeDescription}" />
<meta name="twitter:image" content="${absoluteImageUrl}" />
<meta property="twitter:image" content="${absoluteImageUrl}" />
<meta name="twitter:image:alt" content="${safeTitle}" />
<meta name="twitter:domain" content="${siteUrl.replace(/^https?:\/\//, "")}" />

Expand All @@ -238,8 +252,8 @@ ${
<meta property="og:image" content="${absoluteImageUrl}" />
<meta property="og:image:secure_url" content="${absoluteImageUrl}" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta property="og:image:width" content="${verifiedImage.width}" />
<meta property="og:image:height" content="${verifiedImage.height}" />
<meta property="og:locale" content="en_US" />
${
formattedDate
Expand Down
44 changes: 22 additions & 22 deletions static/data/github-contributors.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,28 @@
"site_admin": false,
"contributions": 98
},
{
"login": "jsbautista",
"id": 42411448,
"node_id": "MDQ6VXNlcjQyNDExNDQ4",
"avatar_url": "https://avatars.githubusercontent.com/u/42411448?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/jsbautista",
"html_url": "https://github.com/jsbautista",
"followers_url": "https://api.github.com/users/jsbautista/followers",
"following_url": "https://api.github.com/users/jsbautista/following{/other_user}",
"gists_url": "https://api.github.com/users/jsbautista/gists{/gist_id}",
"starred_url": "https://api.github.com/users/jsbautista/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/jsbautista/subscriptions",
"organizations_url": "https://api.github.com/users/jsbautista/orgs",
"repos_url": "https://api.github.com/users/jsbautista/repos",
"events_url": "https://api.github.com/users/jsbautista/events{/privacy}",
"received_events_url": "https://api.github.com/users/jsbautista/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false,
"contributions": 94
},
{
"login": "dhoegh",
"id": 7973946,
Expand All @@ -505,28 +527,6 @@
"site_admin": false,
"contributions": 93
},
{
"login": "jsbautista",
"id": 42411448,
"node_id": "MDQ6VXNlcjQyNDExNDQ4",
"avatar_url": "https://avatars.githubusercontent.com/u/42411448?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/jsbautista",
"html_url": "https://github.com/jsbautista",
"followers_url": "https://api.github.com/users/jsbautista/followers",
"following_url": "https://api.github.com/users/jsbautista/following{/other_user}",
"gists_url": "https://api.github.com/users/jsbautista/gists{/gist_id}",
"starred_url": "https://api.github.com/users/jsbautista/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/jsbautista/subscriptions",
"organizations_url": "https://api.github.com/users/jsbautista/orgs",
"repos_url": "https://api.github.com/users/jsbautista/repos",
"events_url": "https://api.github.com/users/jsbautista/events{/privacy}",
"received_events_url": "https://api.github.com/users/jsbautista/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false,
"contributions": 92
},
{
"login": "isabela-pf",
"id": 50221806,
Expand Down
2 changes: 1 addition & 1 deletion static/data/hubspot.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,5 @@
"totalDonations": 150,
"totalMonthlyDonations": 0,
"totalOneTimeDonations": 150,
"lastUpdated": "2025-04-14T19:26:36.933Z"
"lastUpdated": "2025-04-15T18:26:07.181Z"
}