Instantly share code, notes, and snippets.

View 1st-Readme.md

Mongo using Docker

This gist detailes scripts you can use to run a Mongo instance for your development using docker.

The provided scripts help you to create, run, stop and interact with a local mongo Database. You should keep in mind that all data is stored inside the Docker container and will be lost when you destroy it.

View Outline.md

outline for "The rise of headless CMS"

Motivation

Should contain what CMS are and motivate that they are great.

Old days

Story telling time of what used to be a pain for me when working with CMS to motivate the move from traditional CMS to headless.

View categoriesToContentful.js
const createAndPublishCategories = async (categories, spaceId, managementToken, simpleLog = console.log) => {
const client = contentful.createClient({
accessToken: managementToken,
logHandler: (level, data) => simpleLog(`${level} | ${data}`)
})
const space = await client.getSpace(spaceId)
const createdCategories = await Promise.all(categories.map(category => new Promise(async resolve => {
let cmsCategory
try {
cmsCategory = await space.createEntry('blogCategory', {
View generateAssetsList.js
const generateAssetsList = (posts, baseUrl, simpleLog = console.log) => new Promise(async resolve =>{
const apiURL = `${baseUrl.replace(/\/$/, '')}/wp-json/wp/v2/media`
simpleLog('Reducing posts to asset numbers')
let infosFetched = 0
// First add the featured_media images and get ther URLs.
const featuredAssets = await Promise.all(posts.reduce((all, post) => {
if (!post.featured_media) return all
return all.concat([{
mediaNumber: post.featured_media,
View getCategories.js
const getCategories = (posts, baseUrl, simpleLog = console.log) => new Promise(async resolve => {
const apiURL = `${baseUrl.replace(/\/$/, '')}/wp-json/wp/v2/categories`
// First reduce posts to an array of category numbers.
simpleLog('Reducing posts to category numbers')
const categories = await Promise.all(posts.reduce((all, post) => {
if(!post.category) return all
if(all.indexOf(post.category) > -1) return all
return all.concat([post.category])
}, [])
.map(async categoryNumber => {
View perparePosts.js
const transformPosts = posts => posts.map(post => {
delete post._links
delete post.guid
delete post.excerpt
delete post.author
delete post.comment_status
delete post.ping_status
delete post.template
delete post.format
delete post.meta
View getPosts.js
const exportBlogposts = (apiUrl, log) => new Promise(resolve => {
const exportPageOfPosts = (apiUrl, page = 1, allPosts = []) => {
log(`Getting posts for page ${page}`)
const url = `${apiUrl}?page=${page}`
https.get(url, (res) => {
// When we get a 404 back we went one page over those with posts.
// So we are done now.
if(res.statusCode === 400) {
return resolve(allPosts)
}
View assetObject.js
[{
link: 'link to wordpress iage.jpg',
description: 'describe the image',
title: 'and title it',
postId: 'because linking back is nice'
}, ...]
View convertLocale.js
#!/usr/bin/env node
/* eslint-disable */
/**
* Replace the locale in an exported space.
*
* Usage:
* node changeSpaceLocale.js -l es-ES < space.json > translated.json
*
* The above will translate the space to Spanish (currently the only supported
* language to translate to).
View create-blogposts.js
const createBlogPosts = async (posts, assets, categories, managementToken, spaceId, simpleLog = console.log) => {
const client = contentful.createClient({
accessToken: managementToken,
logHandler: (level, data) => simpleLog(`${level} | ${data}`)
})
const space = await client.getSpace(spaceId)
const linkMap = new Map()
assets.forEach(asset => linkMap.set(asset.wpAsset.link, asset.fields.file['en-US'].url))