2021-12-12 12:04:38 +01:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import axios from 'axios'
|
|
|
|
import { languages, strapiHost } from '../config/freesewing.mjs'
|
2021-12-12 12:25:44 +01:00
|
|
|
import { unified } from 'unified'
|
|
|
|
import remarkParser from 'remark-parse'
|
|
|
|
import remarkCompiler from 'remark-stringify'
|
|
|
|
import remarkFrontmatter from 'remark-frontmatter'
|
|
|
|
import remarkFrontmatterExtractor from 'remark-extract-frontmatter'
|
|
|
|
import yaml from 'yaml'
|
|
|
|
import { remarkIntroPlugin } from './remark-intro-plugin.mjs'
|
2021-12-12 12:04:38 +01:00
|
|
|
|
|
|
|
|
2021-12-12 12:25:44 +01:00
|
|
|
/*
|
|
|
|
* Helper method to extract the intro from a Strapi post
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* - body: the post's body (markdown content)
|
|
|
|
*/
|
|
|
|
const postIntro = async markdown => await unified()
|
|
|
|
.use(remarkIntroPlugin)
|
|
|
|
.use(remarkParser)
|
|
|
|
.use(remarkCompiler)
|
|
|
|
.use(remarkFrontmatter)
|
|
|
|
.use(remarkFrontmatterExtractor, { yaml: yaml.parse })
|
|
|
|
.process(markdown)
|
|
|
|
|
2021-12-12 12:04:38 +01:00
|
|
|
/*
|
|
|
|
* Helper method to create the API url for retrieval of Strapi posts
|
|
|
|
*/
|
|
|
|
const buildUrl = (type, site, lang) => (type === 'blog')
|
|
|
|
? `${strapiHost}/blogposts?_locale=${lang}&_sort=date:ASC&dev_${site === 'dev' ? 'eq' : 'ne'}=true`
|
|
|
|
: `${strapiHost}/showcaseposts?_locale=${lang}&_sort=date:ASC`
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper method to load posts from Strapi
|
|
|
|
*/
|
|
|
|
const getPosts = async (type, site, lang) => {
|
|
|
|
let res
|
|
|
|
try {
|
|
|
|
res = await axios.get(buildUrl(type, site, lang))
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
const posts = {}
|
2021-12-12 12:25:44 +01:00
|
|
|
for (const post of res.data) {
|
2021-12-17 17:51:20 +01:00
|
|
|
const intro = await postIntro(`---\n---\n\n${post.body}`)
|
|
|
|
posts[post.slug] = { ...post, intro: intro.data.intro }
|
2021-12-12 12:25:44 +01:00
|
|
|
}
|
2021-12-12 12:04:38 +01:00
|
|
|
|
|
|
|
return posts
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 12:25:44 +01:00
|
|
|
|
2021-12-12 12:04:38 +01:00
|
|
|
/*
|
|
|
|
* Main method that does what needs doing
|
|
|
|
*/
|
2021-12-12 18:00:08 +01:00
|
|
|
export const prebuildStrapi = async(site) => {
|
2021-12-12 12:04:38 +01:00
|
|
|
|
|
|
|
// Say hi
|
|
|
|
console.log()
|
2021-12-12 18:00:08 +01:00
|
|
|
console.log(`Prebuilding Strapi content for freesewing.${site}`)
|
2021-12-12 12:04:38 +01:00
|
|
|
|
|
|
|
// What types of content to load
|
|
|
|
const types = ['blog']
|
|
|
|
if (site === 'org') types.push('showcase')
|
|
|
|
|
2021-12-12 12:25:44 +01:00
|
|
|
const posts = {}
|
|
|
|
const authors = {}
|
2021-12-12 12:04:38 +01:00
|
|
|
for (const type of types) {
|
2021-12-12 12:25:44 +01:00
|
|
|
authors[type] = {}
|
2021-12-12 12:04:38 +01:00
|
|
|
// Loop over languages
|
|
|
|
for (const lang of (site === 'dev' ? ['en'] : languages)) {
|
2021-12-12 18:00:08 +01:00
|
|
|
posts[lang] = {}
|
|
|
|
console.log(` - Language: ${lang}`)
|
|
|
|
posts[lang][type] = await getPosts(type, site, lang)
|
2021-12-12 12:25:44 +01:00
|
|
|
// Extract list of authors
|
2021-12-12 18:00:08 +01:00
|
|
|
for (const [slug, post] of Object.entries(posts[lang][type])) {
|
2021-12-12 12:25:44 +01:00
|
|
|
authors[type][post.author.slug] = post.author
|
2021-12-12 18:00:08 +01:00
|
|
|
posts[lang][type][slug].author = post.author.slug
|
2021-12-12 12:25:44 +01:00
|
|
|
}
|
2021-12-12 12:04:38 +01:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.resolve('..', `freesewing.${site}`, 'prebuild', `strapi.${type}.${lang}.js`),
|
2021-12-12 18:00:08 +01:00
|
|
|
`export const posts = ${JSON.stringify(posts[lang][type], null, 2)}`
|
2021-12-12 12:25:44 +01:00
|
|
|
)
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.resolve('..', `freesewing.${site}`, 'prebuild', `strapi.${type}.authors.js`),
|
|
|
|
`export const authors = ${JSON.stringify(authors[type], null, 2)}`
|
2021-12-12 12:04:38 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-12-12 18:00:08 +01:00
|
|
|
|
|
|
|
return [posts, authors]
|
2021-12-12 12:04:38 +01:00
|
|
|
}
|
|
|
|
|