feat: add functions to dynamically generate news and videos json files

This commit is contained in:
João Peixoto
2021-06-16 15:50:12 +01:00
parent 98f90983dd
commit 9428b29db7
7 changed files with 122 additions and 64 deletions
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env node
'use strict'
const { generateIndexFile } = require('./latest-posts')
const { generateNewsFile } = require('./latest-news')
const { generateVideosFile } = require('./latest-videos')
generateIndexFile()
generateNewsFile()
generateVideosFile()
+48
View File
@@ -0,0 +1,48 @@
'use strict'
/**
The ipfs.io website requests for a blog.ipfs.io/news.json file
with the latest 3 news.
*/
const fs = require('fs')
const path = require('path')
const matter = require('gray-matter')
const dayjs = require('dayjs')
const jsonFilePath = 'dist/news.json'
exports.generateNewsFile = () => {
const contentDir = path.resolve('src/_blog')
fs.readFile(
path.resolve(contentDir, 'newscoverage.md'),
'utf8',
(err, data) => {
if (err) {
console.error(err)
return
}
const news = matter(data)
.data.data.sort((a, b) => new Date(b.date) - new Date(a.date))
.slice(0, 3)
const newsFormatted = news.map((entry) => ({
title: entry.title,
date: dayjs(entry.date).format('D MMM YYYY'),
url: entry.path,
}))
fs.writeFile(
jsonFilePath,
JSON.stringify({ news: newsFormatted }),
(error) => {
if (error) {
console.error(error)
return process.exit(1)
}
}
)
}
)
}
+58
View File
@@ -0,0 +1,58 @@
'use strict'
/**
The ipfs.io website requests for a blog.ipfs.io/index.json file
with the latest 4 posts.
This script parses the rss feed file index.xml and generates the
required index.json file.
@see https://github.com/ipfs/ipfs-blog/issues/104
*/
const fs = require('fs')
const xml2js = require('xml2js')
const dayjs = require('dayjs')
const xmlFilePath = 'dist/index.xml'
const jsonFilePath = 'dist/index.json'
function generateJsonFile(xml) {
xml2js.parseString(xml, (error, dataObj) => {
if (error) {
console.error(error)
return process.exit(1)
}
const posts = dataObj.rss.channel[0].item.slice(0, 4).map((item) => ({
title: item.title[0],
date: dayjs(item.pubDate[0]).format('DD MMMM YYYY'),
url: item.link[0],
author: '',
}))
fs.writeFile(jsonFilePath, JSON.stringify({ posts }), (error) => {
if (error) {
console.error(error)
return process.exit(1)
}
})
})
}
exports.generateIndexFile = () => {
fs.readFile(xmlFilePath, { encoding: 'utf-8' }, (error, data) => {
if (error) {
console.error(error)
if (error.code === 'ENOENT') {
console.error(
"rss xml file not found couldn't generate the index.json file."
)
}
return process.exit(1)
}
generateJsonFile(data)
})
}
+48
View File
@@ -0,0 +1,48 @@
'use strict'
/**
The ipfs.io website requests for a blog.ipfs.io/videos.json file
with the latest 2 videos.
*/
const fs = require('fs')
const path = require('path')
const matter = require('gray-matter')
const dayjs = require('dayjs')
const jsonFilePath = 'dist/videos.json'
exports.generateVideosFile = () => {
const contentDir = path.resolve('src/_blog')
fs.readFile(path.resolve(contentDir, 'videos.md'), 'utf8', (err, data) => {
if (err) {
console.error(err)
return
}
const videos = matter(data)
.data.data.sort((a, b) => new Date(b.date) - new Date(a.date))
.slice(0, 2)
const videosFormatted = videos.map((entry) => ({
title: entry.title,
date: dayjs(entry.date).format('D MMM YYYY'),
url: entry.path,
thumbnail: `https://img.youtube.com/vi/${
new URL(entry.path).searchParams.get('v') ||
new URL(entry.path).searchParams.get('list')
}/maxresdefault.jpg`,
}))
fs.writeFile(
jsonFilePath,
JSON.stringify({ videos: videosFormatted }),
(error) => {
if (error) {
console.error(error)
return process.exit(1)
}
}
)
})
}
-19
View File
@@ -1,19 +0,0 @@
{
"news": [
{
"title": "Top NFT Projects — Rediscovered (Indorse)",
"date": "20 May 2021",
"url": "https://blog.indorse.io/top-nft-projects-rediscovered-5ec0e117b53d/"
},
{
"title": "Meet Metadata Guardians Trying to Make Your NFT Collection Available 100 Years from Now",
"date": "17 May 2021",
"url": "https://rarible.medium.com/meet-metadata-guardians-trying-to-make-your-nft-collection-available-100-years-from-now-60a18baeed6c/"
},
{
"title": "Understanding the different types of NFTs (Indorse)",
"date": "14 May 2021",
"url": "https://blog.indorse.io/what-is-a-non-fungible-token-understanding-the-different-types-of-nfts-3ef29a2b2876/"
}
]
}
-16
View File
@@ -1,16 +0,0 @@
{
"videos": [
{
"title": "Beeple Explains: What is an NFT?",
"date": "21 March 2021",
"url": "https://www.youtube.com/watch?v=13U573keZ3A/",
"thumbnail": "https://img.youtube.com/vi/13U573keZ3A/maxresdefault.jpg"
},
{
"title": "Minty Fresh NFTs with IPFS",
"date": "18 March 2021",
"url": "https://www.youtube.com/watch?v=WNukgBtlWeU/",
"thumbnail": "https://img.youtube.com/vi/WNukgBtlWeU/maxresdefault.jpg"
}
]
}