146 lines
4.0 KiB
JavaScript
146 lines
4.0 KiB
JavaScript
const fs = require('fs')
|
|
const { readdir } = require('fs').promises
|
|
const path = require('path')
|
|
const { resolve } = require('path')
|
|
const _ = require('lodash')
|
|
const md = require('markdown-it')({ typographer: true })
|
|
const yamlFront = require('yaml-front-matter')
|
|
const { DateTime } = require('luxon')
|
|
const Feed = require('feed').Feed
|
|
|
|
async function* getFiles(dir) {
|
|
const dirents = await readdir(dir, { withFileTypes: true })
|
|
for (const dirent of dirents) {
|
|
const res = resolve(dir, dirent.name)
|
|
if (dirent.isDirectory()) {
|
|
yield* getFiles(res)
|
|
} else {
|
|
yield res
|
|
}
|
|
}
|
|
}
|
|
|
|
const getBlogPosts = async (opts) => {
|
|
if (!opts) { opts = {} }
|
|
opts.publishedOnly = 'publishedOnly' in opts ? opts.publishedOnly : true
|
|
|
|
let data = []
|
|
for await (const f of getFiles(path.join(__dirname, '../', 'content', 'blog'))) {
|
|
const rawContent = fs.readFileSync(f, 'utf8')
|
|
const fm = yamlFront.loadFront(rawContent)
|
|
|
|
if (opts.publishedOnly && !fm.published) { continue }
|
|
|
|
data.push({
|
|
type: 'blog',
|
|
title: fm.title,
|
|
author: fm.author,
|
|
urlrel: `/post/${fm.slug}`,
|
|
url: `https://yarmo.eu/post/${fm.slug}`,
|
|
slug: fm.slug,
|
|
date: fm.date,
|
|
date_formatted: DateTime.fromFormat(fm.date, 'yyyy-LL-dd hh:mm:ss').setLocale("en").toLocaleString(DateTime.DATE_MED),
|
|
published: fm.published,
|
|
discussion: fm.discussion,
|
|
content: md.render(fm.__content)
|
|
})
|
|
}
|
|
return data.reverse()
|
|
}
|
|
|
|
const getNotes = async (opts) => {
|
|
if (!opts) { opts = {} }
|
|
opts.publishedOnly = 'publishedOnly' in opts ? opts.publishedOnly : true
|
|
|
|
let data = []
|
|
for await (const f of getFiles(path.join(__dirname, '../', 'content', 'notes'))) {
|
|
const rawContent = fs.readFileSync(f, 'utf8')
|
|
const fm = yamlFront.loadFront(rawContent)
|
|
|
|
if (opts.publishedOnly && !fm.published) { continue }
|
|
|
|
data.push({
|
|
type: 'note',
|
|
title: fm.title,
|
|
author: fm.author,
|
|
urlrel: `/post/${fm.slug}`,
|
|
url: `https://yarmo.eu/post/${fm.slug}`,
|
|
slug: fm.slug,
|
|
date: fm.date,
|
|
date_formatted: DateTime.fromFormat(fm.date, 'yyyy-LL-dd hh:mm:ss').setLocale("en").toLocaleString(DateTime.DATE_MED),
|
|
published: fm.published,
|
|
discussion: fm.discussion,
|
|
content: md.render(fm.__content)
|
|
})
|
|
}
|
|
return data.reverse()
|
|
}
|
|
|
|
const getRSS = async (opts) => {
|
|
if (!opts) { opts = {} }
|
|
opts.include = 'include' in opts ? opts.include : ['blog', 'notes']
|
|
// opts.excludeBlogPosts = 'excludeBlogPosts' in opts ? opts.excludeBlogPosts : false
|
|
// opts.excludeNotes = 'excludeNotes' in opts ? opts.excludeNotes : false
|
|
|
|
const feed = new Feed({
|
|
title: "Yarmo's blog and notes",
|
|
description: "Blog posts and notes feed for yarmo.eu discussing open source, privacy and selfhosted stuff",
|
|
id: "https://yarmo.eu",
|
|
link: "https://yarmo.eu",
|
|
language: "en",
|
|
favicon: "https://yarmo.eu/favicon.png",
|
|
copyright: "All rights reserved 2020 Yarmo Mackenbach",
|
|
updated: new Date(Date.now()),
|
|
author: {
|
|
name: "Yarmo Mackenbach",
|
|
email: "yarmo@yarmo.eu",
|
|
link: "https://yarmo.eu"
|
|
}
|
|
})
|
|
|
|
feed.addCategory("Technology")
|
|
feed.addCategory("FOSS")
|
|
feed.addCategory("Decentralization")
|
|
feed.addCategory("Privacy")
|
|
feed.addCategory("Identity")
|
|
feed.addCategory("Cryptography")
|
|
feed.addCategory("Selfhosted")
|
|
|
|
let posts = []
|
|
if (opts.include.includes('blog')) {
|
|
posts = posts.concat(await getBlogPosts())
|
|
}
|
|
if (opts.include.includes('notes')) {
|
|
posts = posts.concat(await getNotes())
|
|
}
|
|
|
|
posts = _.sortBy(posts, ['date']).reverse()
|
|
|
|
posts.forEach((item, i) => {
|
|
feed.addItem({
|
|
title: item.title,
|
|
id: item.url,
|
|
link: item.url,
|
|
description: item.content,
|
|
content: item.content,
|
|
author: [
|
|
{
|
|
name: "Yarmo Mackenbach",
|
|
email: "yarmo@yarmo.eu",
|
|
link: "https://yarmo.eu"
|
|
}
|
|
],
|
|
date: new Date(item.date)
|
|
})
|
|
})
|
|
|
|
return feed.rss2()
|
|
}
|
|
|
|
module.exports = {
|
|
getFiles: getFiles,
|
|
getBlogPosts: getBlogPosts,
|
|
getNotes: getNotes,
|
|
getRSS: getRSS
|
|
}
|