Created
June 1, 2017 08:08
-
-
Save vlgutv22/e4d310bc630b17aed1e0635964a54481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const htmlTag = require('html-tag'); | |
const stringifyObject = require('stringify-object'); | |
const toHtml = (tags) => (tags.map(({tagName, attributes, content}) => (htmlTag(tagName, attributes, content))).join("")); | |
module.exports = (dato, root, i18n) => { | |
i18n.availableLocales.forEach((locale) => { | |
i18n.withLocale(locale, () => { | |
root.createPost(`content/_index.${locale}.md`, 'yaml', { | |
frontmatter: { | |
title: dato.index.title, | |
type: "dato", | |
slug: "/", | |
first_section: convertToObject(dato.indexfirstsection), | |
home_blog: convertToObject(dato.homeblog), | |
portfolio: convertToObject(dato.portfolioblock), | |
portfolio_items: eachList(dato.portfoliolists), | |
what_we_do_services: eachList(dato.whatwedos), | |
what_we_do: convertToObject(dato.whatdodescription) | |
} | |
}); | |
dato.blogposts.forEach((blogpost) => { | |
root.createPost(`content/blog/${blogpost.slug}.${locale}.md`, 'yaml', { | |
frontmatter: { | |
title: blogpost.title, | |
type: "page", | |
slug: blogpost.slug, | |
date: blogpost.date, | |
categories: blogpost.categories.map(cat => cat.title), | |
image: blogpost.image.value | |
} | |
}); | |
}); | |
}); | |
}); | |
} | |
function eachList(obj) { | |
var keys = []; | |
obj.forEach((item, index) => { | |
keys.push(convertToObject(item)) | |
}); | |
return keys; | |
} | |
function convertToObject(obj) { | |
var keys = [], | |
item, | |
key, | |
values = []; | |
for (var k in obj) { | |
if (!obj.hasOwnProperty(k)) | |
continue | |
item = k; | |
if (item !== 'entity' && item !== 'itemsRepo') { | |
keys.push(item) | |
} | |
} | |
for (var i = 0; i < keys.length; i++) { | |
key = keys[i]; | |
if (typeof obj[key] === 'object') { | |
item = key + ':' + stringifyObject(obj[key], { | |
indent: ' ', | |
singleQuotes: false | |
}); | |
} else { | |
item = key + ': "' + obj[key] + '"'; | |
} | |
values.push(item) | |
} | |
return eval('({' + values + '})'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment