Skip to content

Instantly share code, notes, and snippets.

View HoverBaum's full-sized avatar
🌳
 I like 🌳🌳🌳 

Hendrik HoverBaum

🌳
 I like 🌳🌳🌳 
View GitHub Profile
@HoverBaum
HoverBaum / commands
Created October 10, 2017 09:17
See branches on the commandline and create an alias for it.
git log --all --color --graph --pretty=format:'%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
git config --global alias.lg "log --all --color --graph --pretty=format:'%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
@HoverBaum
HoverBaum / Readme.md
Last active March 4, 2020 05:34
Filter empty assets from Contentful export.

Using the Contentful Export tool I noticed that you can currently run into a situation where you export assets that have no files linked which will through an error when you try to pass the exported json into the Contentful Import tool as discussed in issue 95.

As a temporary solution I wrote myselve this script that you can pipe data through to filter out these malformed assets.

cat export.json | node filter-empty-assets.js > filtered-export.json

Now you can use the filteres-export.json for your import and it should work.

@HoverBaum
HoverBaum / translateSpace.js
Last active March 21, 2018 13:21
Translate a space to a different locale for Contentful.
#!/usr/bin/env node
/**
* 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).
*/
@HoverBaum
HoverBaum / create-and-publish-assets.js
Last active March 16, 2020 14:05
Create and publish assets in Contentful.
const createAndPublishAssets = (assets, managementToken, spaceId, locale, simpleLog = console.log) => new Promise(async resolve => {
simpleLog('Creating Contentful client')
const client = contentful.createClient({
accessToken: managementToken,
logHandler: (level, data) => simpleLog(`${level} | ${data}`)
})
const iterableAssets = makeIterator(assets)
const space = await client.getSpace(spaceId)
const cmsAssets = []
@HoverBaum
HoverBaum / create-blogposts.js
Created March 22, 2018 13:12
Creating blogposts in Contentful.
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))
@HoverBaum
HoverBaum / convertLocale.js
Created March 23, 2018 16:00
Convert a exported Spaces locale.
#!/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).
@HoverBaum
HoverBaum / assetObject.js
Created March 26, 2018 08:37
Code example for blogpost about migrating from wordpress to Contentful.
[{
link: 'link to wordpress iage.jpg',
description: 'describe the image',
title: 'and title it',
postId: 'because linking back is nice'
}, ...]
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)
}
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
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 => {