Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use this php script to convert a Ghost export json file to Hugo content file. Files will be created in a "output" folder, to reuse images place the ghost content folder into the hugo's static folder
<?php
$ghost_data = json_decode(file_get_contents("./GhostData.json"),true) ;
foreach ($ghost_data['data']['posts'] as $key => $value) {
$created_at = date("c",$value["created_at"]/1000);
$title = str_replace('\\','\\\\',$value["title"]) ;
$title = str_replace('"','\"',$title) ;
$slug = $value["slug"] ;
$markdown = $value["markdown"] ;
$draft = ($value["published_at"] == null) ? 'true' : 'false' ;
$published_at = ($value["published_at"] == null) ? date("c",$value["created_at"]/1000) : date("c",$value["published_at"]/1000); ;
$output_dir = "./output" ;
if (!is_dir($output_dir)) {
mkdir($output_dir);
}
$file = $output_dir."/".$slug.".md" ;
file_put_contents($file, '+++');
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'date = "' .$published_at.'"', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'draft = ' .$draft, FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'title = "' .$title.'"', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'slug = "' .$slug.'"', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, '+++', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, $markdown, FILE_APPEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment