Created
June 20, 2022 17:52
-
-
Save SebbeJohansson/2f64e0a76e3b786e5ea3ed1cdbf8ca29 to your computer and use it in GitHub Desktop.
This file contains 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
import axios from "axios"; | |
import StoryblokClient from "storyblok-js-client"; | |
import TurndownService from "turndown"; | |
import markdownConverter from "storyblok-markdown-richtext"; | |
// rember to remove | |
const spaceId = {{INSERT STORYBLOK SPACE ID HERE}}; | |
let Storyblok = new StoryblokClient({ | |
// Remmber to remove | |
oauthToken: "{{INSERT STORYBLOK MANAGEMENT API TOKEN HERE}}", | |
}); | |
const data = ["fields id,title,slug,content,author,date,cat,status;"]; | |
let blogPosts = []; | |
await axios | |
.post("https://admin.sebbejohansson.com/api/blogs/get", data.join("")) | |
.then((response) => { | |
//console.log(response.data); | |
blogPosts = response.data; | |
}) | |
.catch((error) => { | |
console.log(error.response); | |
}); | |
blogPosts.forEach((post) => { | |
const blogPost = post; | |
const turndownService = new TurndownService(); | |
const richtextData = markdownConverter.markdownToRichtext( | |
turndownService.turndown(blogPost.content) | |
); | |
console.log(richtextData); | |
// use the universal js client to perform the request | |
Storyblok.post(`spaces/${spaceId}/stories/`, { | |
story: { | |
name: blogPost.title, | |
slug: blogPost.slug, | |
content: { | |
component: "blog-entry", | |
content: [ | |
{ | |
component: "text", | |
text: richtextData, | |
}, | |
], | |
date: blogPost.date, | |
}, | |
first_published_at: blogPost.date, | |
}, | |
publish: blogPost.status, | |
}) | |
.then((response) => { | |
// console.log(response) | |
}) | |
.catch((error) => { | |
console.log(blogPost.slug, " - ", error.response.status); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment