1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-08-29 12:10:22 +02:00

Add sponsors

This commit is contained in:
Kamran Ahmed
2023-04-03 19:15:10 +01:00
parent 9d38cf7650
commit d21c1f6d0d
16 changed files with 156 additions and 38 deletions

View File

@@ -13,7 +13,42 @@ if (!apiKey || !sheetId) {
const sheetRange = 'A3:I1001';
const sheetUrl = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${sheetRange}?key=${apiKey}`;
function populatePageAds({
function removeAllSponsors(baseContentDir) {
console.log('------------------------');
console.log('Removing sponsors from: ', baseContentDir);
console.log('------------------------');
const dataDirPath = path.join(__dirname, '../src/data');
const contentDirPath = path.join(dataDirPath, baseContentDir);
const contentDir = fs.readdirSync(contentDirPath);
contentDir.forEach((content) => {
console.log('Removing sponsor from: ', content);
const pageFilePath = path.join(contentDirPath, content, `${content}.md`);
const pageFileContent = fs.readFileSync(pageFilePath, 'utf8');
const frontMatterRegex = /---\n([\s\S]*?)\n---/;
const existingFrontmatter = pageFileContent.match(frontMatterRegex)[1];
const contentWithoutFrontmatter = pageFileContent
.replace(frontMatterRegex, ``)
.trim();
let frontmatterObj = yaml.load(existingFrontmatter);
delete frontmatterObj.sponsor;
const newFrontmatter = yaml.dump(frontmatterObj, {
lineWidth: 10000,
forceQuotes: true,
quotingType: "'",
});
const newContent = `---\n${newFrontmatter}---\n${contentWithoutFrontmatter}`;
fs.writeFileSync(pageFilePath, newContent, 'utf8');
});
}
function addPageSponsor({
pageUrl,
company,
redirectUrl,
@@ -24,13 +59,6 @@ function populatePageAds({
endDate,
isActive,
}) {
const isConfiguredActive = isActive.toLowerCase() === 'yes';
const currentDate = new Date();
const isDateInRange =
currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
const shouldShowAd = isConfiguredActive && isDateInRange;
const urlPart = pageUrl
.replace('https://roadmap.sh/', '')
.replace(/\?.+?$/, '');
@@ -64,29 +92,27 @@ function populatePageAds({
let frontmatterObj = yaml.load(existingFrontmatter);
delete frontmatterObj.sponsor;
if (shouldShowAd) {
const frontmatterValues = Object.entries(frontmatterObj);
const roadmapLabel = frontmatterObj.briefTitle;
const frontmatterValues = Object.entries(frontmatterObj);
const roadmapLabel = frontmatterObj.briefTitle;
// Insert sponsor data at 10 index i.e. after
// roadmap dimensions in the frontmatter
frontmatterValues.splice(10, 0, [
'sponsor',
{
url: redirectUrl,
title: adTitle,
imageUrl,
description: adDescription,
event: {
category: 'SponsorClick',
action: `${company} Redirect`,
label: `${roadmapLabel} / ${company} Link`,
},
// Insert sponsor data at 10 index i.e. after
// roadmap dimensions in the frontmatter
frontmatterValues.splice(10, 0, [
'sponsor',
{
url: redirectUrl,
title: adTitle,
imageUrl,
description: adDescription,
event: {
category: 'SponsorClick',
action: `${company} Redirect`,
label: `${roadmapLabel} / ${company} Link`,
},
]);
},
]);
frontmatterObj = Object.fromEntries(frontmatterValues);
}
frontmatterObj = Object.fromEntries(frontmatterValues);
const newFrontmatter = yaml.dump(frontmatterObj, {
lineWidth: 10000,
@@ -98,6 +124,10 @@ function populatePageAds({
fs.writeFileSync(pageFilePath, newContent, 'utf8');
}
// Remove sponsors from all roadmaps
removeAllSponsors('roadmaps');
removeAllSponsors('best-practices');
fetch(sheetUrl)
.then((res) => res.json())
.then((rawData) => {
@@ -117,7 +147,15 @@ fetch(sheetUrl)
isActive,
] = row;
populatePageAds({
const isConfiguredActive = isActive?.toLowerCase() === 'yes';
const currentDate = new Date();
const isDateInRange = currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
if (!isConfiguredActive || !isDateInRange) {
return;
}
addPageSponsor({
pageUrl,
company,
redirectUrl,