Skip to content

Update README with latest blog posts #177

Update README with latest blog posts

Update README with latest blog posts #177

Workflow file for this run

name: Update README with latest blog posts
on:
push:
paths:
- 'src/content/blog/**'
schedule:
- cron: '0 0 * * *' # daily at midnight UTC
workflow_dispatch:
permissions:
contents: write
jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Fetch RSS and update README
uses: actions/github-script@v7
with:
script: |
const https = require('https');
const fs = require('fs');
function fetchRSS(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? https : require('http');
client.get(url, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return fetchRSS(res.headers.location).then(resolve).catch(reject);
}
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve(data));
}).on('error', reject);
});
}
try {
const rss = await fetchRSS('https://codestz.dev/feed');
const items = [...rss.matchAll(/<item>([\s\S]*?)<\/item>/g)];
if (items.length === 0) {
console.log('No items found in RSS feed, skipping update');
return;
}
const decode = (s) => s.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&apos;/g, "'");
const MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
const posts = items.slice(0, 7).map(([, block]) => {
const grab = (tag) => (block.match(new RegExp(`<${tag}>([\\s\\S]*?)<\\/${tag}>`)) || [])[1] || '';
const title = decode(grab('title').trim());
const link = grab('link').trim();
const pubDate = grab('pubDate').trim();
const readingTime = decode(grab('blog:readingTime').trim());
const meta = [];
if (pubDate) {
const d = new Date(pubDate);
if (!isNaN(d)) meta.push(`${MONTHS[d.getUTCMonth()]} ${d.getUTCFullYear()}`);
}
if (readingTime) meta.push(readingTime.replace(/ read$/, ''));
const suffix = meta.length ? ` — ${meta.join(' · ')}` : '';
return `- [${title}](${link})${suffix}`;
}).join('\n');
const readme = fs.readFileSync('README.md', 'utf8');
const updated = readme.replace(
/<!-- BLOG-POST-LIST:START -->[\s\S]*?<!-- BLOG-POST-LIST:END -->/,
`<!-- BLOG-POST-LIST:START -->\n${posts}\n<!-- BLOG-POST-LIST:END -->`
);
if (updated !== readme) {
fs.writeFileSync('README.md', updated);
console.log(`Updated README with ${items.slice(0, 7).length} posts`);
} else {
console.log('README already up to date');
}
} catch (error) {
console.log(`Failed to fetch RSS: ${error.message}`);
console.log('Skipping README update');
}
- name: Commit changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git diff --quiet README.md || (git add README.md && git commit -m "chore: update README blog posts from RSS feed" && git push)