1
0
Fork 0
freesewing/sites/org/scripts/prebuild-step3.mjs
joostdecock c5b0daf390 fix: Various website and repo fixes (#243)
- Fixes things in Oath flow, yet needs testing before we close #235
- Updates security notice, closes #240
- Fixes broken links to user profiles, closes #238

Reviewed-on: https://codeberg.org/freesewing/freesewing/pulls/243
Co-authored-by: joostdecock <joost@joost.at>
Co-committed-by: joostdecock <joost@joost.at>
2025-04-12 10:42:35 +00:00

97 lines
3.3 KiB
JavaScript

import fs from 'fs'
import { glob } from 'glob'
import matter from 'gray-matter'
import { orderBy } from '../../../packages/utils/src/index.mjs'
const loadExamplesTagsAndAuthors = async () => {
const lists = {
blog: await glob('./blog/*/index.mdx'),
newsletter: await glob('./newsletter/*/index.mdx'),
showcase: await glob('./showcase/*/index.mdx'),
}
const tags = []
const authors = []
const examples = {}
const recentBlogPosts = []
for (const type of Object.keys(lists)) {
for (const file of lists[type]) {
const content = await fs.readFileSync(file, 'utf-8')
const data = matter(content)
if (data.data.tags) {
if (Array.isArray(data.data.tags)) tags.push(...data.data.tags)
else tags.push(data.data.tags)
} else if (type === 'showcase')
console.log('Missing design tag in showcase post:', { file, data: data.data })
if (data.data.authors) {
if (Array.isArray(data.data.authors)) authors.push(...data.data.authors)
else authors.push(data.data.authors)
} else if (type === 'showcase') console.warn('Missing authors in showcase post:', file)
else if (type === 'blog') console.warn('Missing authors in blog post:', file)
else if (type === 'newsletter') console.warn('Missing authors in newsletter edition:', file)
if (type === 'showcase') {
for (const tag of data.data.tags) {
if (typeof examples[tag] === 'undefined') examples[tag] = []
examples[tag].push({ title: data.data.title, id: file.split('/')[1] })
}
} else if (type === 'blog')
recentBlogPosts.push({
title: data.data.title,
slug: file.split('/')[1],
date: new Date(data.data.date).getTime(),
})
}
}
return {
authors: [...new Set(authors.sort())], // Make them unique
tags: [...new Set(tags.sort())], // Make them unique
examples,
recentBlogPosts: orderBy(recentBlogPosts, 'date', 'desc').slice(0, 2),
}
}
const titles = {
admin: 'FreeSewing Maintainer',
user: 'FreeSewing User',
}
const userAsAuthor = (user) => ({
name: user.profile.username,
title: titles[user.profile.role],
url: `https://freesewing.eu/users/?id=${user.profile.id}`,
image_url: `https://imagedelivery.net/ouSuR9yY1bHt-fuAokSA5Q/uid-${user.profile.ihash}/public`,
bio: user.profile.bio,
})
const loadUser = async (id) => {
let result
try {
result = await fetch(`https://backend.freesewing.eu/users/${id}`)
} catch (err) {
console.warn(`Failed to load user with id ${id}`, err)
}
if (result) return await result.json()
}
async function prebuild() {
const all = {}
const { authors, examples, tags, recentBlogPosts } = await loadExamplesTagsAndAuthors()
for (const author of authors) {
const user = await loadUser(author)
if (user.profile.id) all[user.profile.id] = userAsAuthor(user)
}
fs.writeFileSync(`./authors.json`, JSON.stringify(all, null, 2))
fs.writeFileSync(
`./showcase-tags.mjs`,
`export const tags = ${JSON.stringify([...new Set(tags.sort())])}`
)
fs.writeFileSync(`./design-examples.mjs`, `export const examples = ${JSON.stringify(examples)}`)
fs.writeFileSync(
`./recent-blog-posts.mjs`,
`export const recentBlogPosts = ${JSON.stringify(recentBlogPosts, 0, 2)}`
)
}
prebuild()