Skip to content

Commit

Permalink
Merge pull request #98 from fulldecent/features/github-pages-template…
Browse files Browse the repository at this point in the history
…-add-sitemap-js#7236

github-pages-template added sitemap.js with updated actions.
  • Loading branch information
Raza403 authored Aug 5, 2024
2 parents d30c01a + d7e124f commit 64bf9a5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build-test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ jobs:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Build site
run: bundle exec jekyll build
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '16' # Use the LTS version or the version you need
- name: Install dependencies
run: npm install xml2js front-matter
- name: Generate sitemap
run: node generate-sitemap.js
- name: Upload build artifact, ready for GitHub Pages deployment
uses: actions/upload-pages-artifact@v3
with:
Expand Down
101 changes: 101 additions & 0 deletions generate-sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const fs = require('fs');
const path = require('path');
const https = require('https');
const { parseString } = require('xml2js'); // Using xml2js for XML parsing

const site = 'https://fulldecent.github.io/github-pages-template';
const buildFolderPath = path.join(__dirname, 'build');
const sourceFolderPath = path.join(__dirname, 'build');
const sitemapPath = path.join(sourceFolderPath, 'sitemap.xml');
const daysThreshold = 30; // Number of days to compare for updating lastmod

// Function to generate sitemap XML content
function generateSitemap(files, lastmodDate) {
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';

files.forEach((file) => {
// Convert file path to URL and remove file extensions
const url = file
.replace(buildFolderPath, '')
.replace(/\\/g, '/')
.replace(/index\.html?$/, '')
.replace(/\.html?$/, '');
xml += `<url><loc>${site}${url}</loc>`;
xml += `<lastmod>${lastmodDate.toISOString()}</lastmod>`; // Use lastmodDate if it exists
xml += url === '/' ? '<priority>1.00</priority>' : '<priority>0.80</priority>';
xml += '</url>\n';
});

xml += '</urlset>';
return xml;
}

// Recursive function to find HTML files in subdirectories
function getHTMLFiles(dir, fileList) {
const files = fs.readdirSync(dir);
fileList = fileList || [];

files.forEach((file) => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
getHTMLFiles(filePath, fileList);
} else if (path.extname(file) === '.html' || path.extname(file) === '.htm') {
const content = fs.readFileSync(filePath, 'utf8');
const experimentMetaTag = content.match(/<meta\s+name=["']experiment["']\s+content=["']true["']\s*\/?>/i);
if (!experimentMetaTag) {
fileList.push(filePath);
}
}
});

return fileList;
}

// Fetch sitemap.xml from the provided URL
https
.get(`${site}/sitemap.xml`, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
// Check if sitemap.xml file exists
parseString(data, (err, result) => {
if (err) {
generateAndWriteSitemap(new Date());
return;
}

// Get the lastmod date of the first URL
const lastmod = new Date(result.urlset.url[0].lastmod[0]);
const today = new Date();
// Calculate the difference in days
const differenceInTime = today.getTime() - lastmod.getTime();
const differenceInDays = Math.round(differenceInTime / (1000 * 3600 * 24));
// Generate the sitemap using lastmod if the difference is more than daysThreshold, else use currentDate
const sitemapDate = differenceInDays > daysThreshold ? today : lastmod;
generateAndWriteSitemap(sitemapDate);
});
});
})
.on('error', (err) => {
console.error('Error fetching sitemap:', err);
// If there's an error fetching the sitemap, generate the sitemap with the current date
generateAndWriteSitemap(new Date());
});

// Function to generate and write sitemap
function generateAndWriteSitemap(lastmodDate) {
// Find all HTML files in build folder and its subdirectories
const htmlFiles = getHTMLFiles(buildFolderPath);
// Generate sitemap XML content
const sitemapXML = generateSitemap(htmlFiles, lastmodDate);
// Write sitemap XML to file
fs.writeFile(sitemapPath, sitemapXML, (err) => {
if (err) throw err;
console.log('Sitemap.xml generated successfully!');
});
}
5 changes: 5 additions & 0 deletions source/experiment-template/original-variant.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{% if page.experiment %}
<!-- Add experiment meta tag to detect if page is an experiment page
and don't add the page in sitemap.xml https://github.com/fulldecent/mtssites/issues/7236 -->
<meta name="experiment" content="true">
{% endif %}
<!-- Canonical link should be the original experiment page. "experiment-template" in this example. -->
<link
rel="canonical"
Expand Down
5 changes: 5 additions & 0 deletions source/experiment-template/variant1.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{% if page.experiment %}
<!-- Add experiment meta tag to detect if page is an experiment page
and don't add the page in sitemap.xml https://github.com/fulldecent/mtssites/issues/7236 -->
<meta name="experiment" content="true">
{% endif %}
<!-- Canonical link should be the original experiment page. "experiment-template" in this example. -->
<link
rel="canonical"
Expand Down

0 comments on commit 64bf9a5

Please sign in to comment.