From 9428b29db7d265631419698746134240e7c7c10b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Peixoto?= Date: Wed, 16 Jun 2021 15:50:12 +0100 Subject: [PATCH] feat: add functions to dynamically generate news and videos json files --- package.json | 2 +- scripts/data/index.js | 11 +++++++ scripts/data/latest-news.js | 48 ++++++++++++++++++++++++++++++ scripts/{ => data}/latest-posts.js | 42 +++++++++----------------- scripts/data/latest-videos.js | 48 ++++++++++++++++++++++++++++++ scripts/data/news.json | 19 ------------ scripts/data/videos.json | 16 ---------- 7 files changed, 122 insertions(+), 64 deletions(-) create mode 100755 scripts/data/index.js create mode 100755 scripts/data/latest-news.js rename scripts/{ => data}/latest-posts.js (60%) create mode 100755 scripts/data/latest-videos.js delete mode 100644 scripts/data/news.json delete mode 100644 scripts/data/videos.json diff --git a/package.json b/package.json index b8e69320..8cdac267 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "npm run dev", "dev": "vuepress dev src", "build": "vuepress build src && npm run postbuild", - "postbuild": "./scripts/latest-posts.js", + "postbuild": "./scripts/data/index.js", "lint": "prettier --write \"**/*.{json,yaml,md}\" && run-p lint:**", "lint:eslint": "eslint --cache \"**/*.{js,vue}\" --fix", "lint:styles": "stylelint \"**/*.css\" \"src/.vuepress/**/*.css\" --fix" diff --git a/scripts/data/index.js b/scripts/data/index.js new file mode 100755 index 00000000..a7861ce4 --- /dev/null +++ b/scripts/data/index.js @@ -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() diff --git a/scripts/data/latest-news.js b/scripts/data/latest-news.js new file mode 100755 index 00000000..0d45754c --- /dev/null +++ b/scripts/data/latest-news.js @@ -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) + } + } + ) + } + ) +} diff --git a/scripts/latest-posts.js b/scripts/data/latest-posts.js similarity index 60% rename from scripts/latest-posts.js rename to scripts/data/latest-posts.js index 789add76..c5a5f3bf 100755 --- a/scripts/latest-posts.js +++ b/scripts/data/latest-posts.js @@ -1,5 +1,3 @@ -#!/usr/bin/env node - 'use strict' /** @@ -17,8 +15,6 @@ const dayjs = require('dayjs') const xmlFilePath = 'dist/index.xml' const jsonFilePath = 'dist/index.json' -const newsJsonPath = 'scripts/data/news.json' -const videosJsonPath = 'scripts/data/videos.json' function generateJsonFile(xml) { xml2js.parseString(xml, (error, dataObj) => { @@ -43,30 +39,20 @@ function generateJsonFile(xml) { }) } -fs.readFile(xmlFilePath, { encoding: 'utf-8' }, (error, data) => { - if (error) { - console.error(error) +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." - ) + if (error.code === 'ENOENT') { + console.error( + "rss xml file not found – couldn't generate the index.json file." + ) + } + + return process.exit(1) } - return process.exit(1) - } - - generateJsonFile(data) -}) - -fs.copyFile(newsJsonPath, 'dist/news.json', (err) => { - if (err) { - console.log('Error: ', err) - } -}) - -fs.copyFile(videosJsonPath, 'dist/videos.json', (err) => { - if (err) { - console.log('Error: ', err) - } -}) + generateJsonFile(data) + }) +} diff --git a/scripts/data/latest-videos.js b/scripts/data/latest-videos.js new file mode 100755 index 00000000..54808ea1 --- /dev/null +++ b/scripts/data/latest-videos.js @@ -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) + } + } + ) + }) +} diff --git a/scripts/data/news.json b/scripts/data/news.json deleted file mode 100644 index e8dfe629..00000000 --- a/scripts/data/news.json +++ /dev/null @@ -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/" - } - ] -} diff --git a/scripts/data/videos.json b/scripts/data/videos.json deleted file mode 100644 index 954d8a41..00000000 --- a/scripts/data/videos.json +++ /dev/null @@ -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" - } - ] -}