Last active
December 28, 2016 15:34
-
-
Save yossale/4c9b380ddd37dd976e65 to your computer and use it in GitHub Desktop.
Fix "error: Value in [posts.slug] exceeds maximum length of 150 characters." error when importing from Wordpress to Ghost
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
var fs = require('fs'); | |
var filePath = process.argv[2]; | |
var content = null; | |
fs.readFile(filePath, 'utf8', function (err, data) { | |
if (err) { | |
console.log('Error: ' + err); | |
return; | |
} | |
//Sometimes the shortning causes duplicte sluglines, which are forbidden. So the map is here to handle it. | |
var slugsMap = {}; | |
content = JSON.parse(data); | |
var posts = content.data.posts; | |
posts.forEach(function (post) { | |
if (post.slug.length > 150) { | |
post.slug = post.slug.substring(0, 149); | |
if (slugsMap[post.slug]) { | |
console.log("Found duplicate: " + post.slug); | |
slugsMap[post.slug]++ | |
post.slug = (slugsMap[post.slug] + post.slug).substring(0, 149); | |
console.log("Changed to: " + post.slug); | |
} else { | |
slugsMap[post.slug] = 1; | |
} | |
} | |
}); | |
var outputFile = "./parsed_slugline.json"; | |
fs.writeFile(outputFile, JSON.stringify(content), function (err) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log("File was saved to " + __dirname + "/" + outputFile); | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run the script on the json file you exported from wordpress, and use the new output when importing to Ghost.