-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
927abcd
commit bd23596
Showing
6 changed files
with
145 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
let fs = require('node:fs/promises'); | ||
let path = require('node:path'); | ||
let getArticles = require('./get-articles'); | ||
|
||
let siteData = { | ||
title: "Massa Labs", | ||
description: "Sean Massa's personal site where they discuss tech, organizations, ethics, and games.", | ||
url: 'https://massalabs.com' | ||
}; | ||
|
||
// Source: https://stackoverflow.com/a/27979933 | ||
function escapeXml(unsafe) { | ||
return unsafe.replace(/[<>&'"]/g, function (c) { | ||
switch (c) { | ||
case '<': return '<'; | ||
case '>': return '>'; | ||
case '&': return '&'; | ||
case '\'': return '''; | ||
case '"': return '"'; | ||
} | ||
}); | ||
} | ||
|
||
function toUtcDate(str) { | ||
return new Date(Date.parse(str)).toUTCString();; | ||
} | ||
|
||
function generateTags(article) { | ||
// TODO: implement tags | ||
// <category>{{ tag | xml_escape }}</category> | ||
return ''; | ||
} | ||
|
||
function generateRssItem(article) { | ||
let fullUrl = `https://massalabs.com/blog/${article.slug}`; | ||
let tags = generateTags(article); | ||
|
||
return ` | ||
<item> | ||
<title>${escapeXml(article.title)}</title> | ||
<description>${escapeXml(article.description)}</description> | ||
<pubDate>${toUtcDate(article.date)}</pubDate> | ||
<link>${fullUrl}</link> | ||
<guid isPermaLink="true">${fullUrl}</guid> | ||
${tags} | ||
</item> | ||
`.trim(); | ||
} | ||
|
||
function generateRss(articles) { | ||
let mostRecentPublishedArticle = articles.find((a) => a.published); | ||
let mostRecentPublishedDate = toUtcDate(mostRecentPublishedArticle.date); | ||
let generationDate = (new Date()).toUTCString(); | ||
let posts = articles.map(a => generateRssItem(a)).join('\n'); | ||
|
||
return ` | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> | ||
<channel> | ||
<title>${siteData.title}</title> | ||
<description>${siteData.description}</description> | ||
<link>${siteData.url}</link> | ||
<atom:link href="${siteData.url}/feed.xml" rel="self" type="application/rss+xml" /> | ||
<pubDate>${generationDate}</pubDate> | ||
<lastBuildDate>${mostRecentPublishedDate}</lastBuildDate> | ||
${posts} | ||
</channel> | ||
</rss> | ||
`.trim(); | ||
} | ||
|
||
async function writeRss() { | ||
let articles = getArticles(); | ||
let rssString = generateRss(articles); | ||
|
||
let pathToRss = path.join(__dirname, '../dist/feed.xml'); | ||
await fs.writeFile(pathToRss, rssString); | ||
} | ||
|
||
writeRss().then(() => { | ||
console.log('Generated rss!') | ||
}).catch((error) => { | ||
console.error('Failed to generate rss!'); | ||
console.error(error); | ||
}); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
let fs = require('fs'); | ||
let { join } = require('path'); | ||
|
||
function tryParseJSON(jsonText, originalFilePath) { | ||
try { | ||
return JSON.parse(jsonText); | ||
} catch (error) { | ||
console.log(`JSON Parse Error of "${originalFilePath}": ${error.message}\nText:\n${jsonText}`); | ||
} | ||
} | ||
|
||
module.exports = function getArticles() { | ||
/* | ||
We have to convert the module format into json because no other mechanism for | ||
share this data worked between ember build (node, commonjs) and the | ||
project code (browser, module). | ||
*/ | ||
let articlePath = join(__dirname, '/../app/article-data.js'); | ||
let articleData = fs.readFileSync(articlePath).toString(); | ||
let trimmedArticleData = articleData | ||
.replace('let articles =', '') | ||
.replace('export default articles;', ''); | ||
return tryParseJSON(trimmedArticleData, articlePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters