mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-08-26 10:34:40 +02:00
Add support for ads on best-practices
This commit is contained in:
@@ -13,8 +13,8 @@ if (!apiKey || !sheetId) {
|
||||
const sheetRange = 'A3:I1001';
|
||||
const sheetUrl = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${sheetRange}?key=${apiKey}`;
|
||||
|
||||
function populateRoadmapAds({
|
||||
roadmapUrl,
|
||||
function populatePageAds({
|
||||
pageUrl,
|
||||
company,
|
||||
redirectUrl,
|
||||
imageUrl,
|
||||
@@ -30,26 +30,25 @@ function populateRoadmapAds({
|
||||
const isDateInRange = currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
|
||||
const shouldShowAd = isConfiguredActive && isDateInRange;
|
||||
|
||||
// get id from the roadmap URL
|
||||
const roadmapId = roadmapUrl
|
||||
.split('/')
|
||||
.pop()
|
||||
.replace(/\?.+?$/, '');
|
||||
const urlPart = pageUrl.replace('https://roadmap.sh/', '').replace(/\?.+?$/, '');
|
||||
|
||||
const roadmapFilePath = path.join(__dirname, '../src/data/roadmaps', `${roadmapId}/${roadmapId}.md`);
|
||||
const parentDir = urlPart.startsWith('best-practices/') ? 'best-practices' : 'roadmaps';
|
||||
const pageId = urlPart.replace(`${parentDir}/`, '');
|
||||
|
||||
if (!fs.existsSync(roadmapFilePath)) {
|
||||
console.error(`Roadmap file not found: ${roadmapFilePath}`);
|
||||
const pageFilePath = path.join(__dirname, `../src/data/${parentDir}`, `${pageId}/${pageId}.md`);
|
||||
|
||||
if (!fs.existsSync(pageFilePath)) {
|
||||
console.error(`Page file not found: ${pageFilePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Updating roadmap: ${roadmapId}`);
|
||||
const roadmapFileContent = fs.readFileSync(roadmapFilePath, 'utf8');
|
||||
console.log(`Updating page: ${urlPart}`);
|
||||
const pageFileContent = fs.readFileSync(pageFilePath, 'utf8');
|
||||
|
||||
const frontMatterRegex = /---\n([\s\S]*?)\n---/;
|
||||
|
||||
const existingFrontmatter = roadmapFileContent.match(frontMatterRegex)[1];
|
||||
const contentWithoutFrontmatter = roadmapFileContent.replace(frontMatterRegex, ``).trim();
|
||||
const existingFrontmatter = pageFileContent.match(frontMatterRegex)[1];
|
||||
const contentWithoutFrontmatter = pageFileContent.replace(frontMatterRegex, ``).trim();
|
||||
|
||||
let frontmatterObj = yaml.load(existingFrontmatter);
|
||||
delete frontmatterObj.sponsor;
|
||||
@@ -59,7 +58,7 @@ function populateRoadmapAds({
|
||||
const roadmapLabel = frontmatterObj.briefTitle;
|
||||
|
||||
// Insert sponsor data at 10 index i.e. after
|
||||
// roadmap dimensions in the fronmatter
|
||||
// roadmap dimensions in the frontmatter
|
||||
frontmatterValues.splice(10, 0, [
|
||||
'sponsor',
|
||||
{
|
||||
@@ -81,7 +80,7 @@ function populateRoadmapAds({
|
||||
const newFrontmatter = yaml.dump(frontmatterObj, { lineWidth: 10000, forceQuotes: true, quotingType: '"' });
|
||||
const newContent = `---\n${newFrontmatter}---\n\n${contentWithoutFrontmatter}`;
|
||||
|
||||
fs.writeFileSync(roadmapFilePath, newContent, 'utf8');
|
||||
fs.writeFileSync(pageFilePath, newContent, 'utf8');
|
||||
}
|
||||
|
||||
fetch(sheetUrl)
|
||||
@@ -92,7 +91,7 @@ fetch(sheetUrl)
|
||||
rows.map((row) => {
|
||||
// prettier-ignore
|
||||
const [
|
||||
roadmapUrl,
|
||||
pageUrl,
|
||||
company,
|
||||
redirectUrl,
|
||||
imageUrl,
|
||||
@@ -103,8 +102,8 @@ fetch(sheetUrl)
|
||||
isActive,
|
||||
] = row;
|
||||
|
||||
populateRoadmapAds({
|
||||
roadmapUrl,
|
||||
populatePageAds({
|
||||
pageUrl,
|
||||
company,
|
||||
redirectUrl,
|
||||
imageUrl,
|
||||
|
Reference in New Issue
Block a user