diff --git a/fix-markdown-linked-images.js b/fix-markdown-linked-images.js new file mode 100644 index 0000000..1d481da --- /dev/null +++ b/fix-markdown-linked-images.js @@ -0,0 +1,94 @@ +/** + * Finds and fixes any mangled + * + * Usage: + * + * node fix-markdown-linked-images.js https://blah.ghost.io ADMIN_API_KEY - dry run + * node fix-markdown-linked-images.js https://blah.ghost.io ADMIN_API_KEY true - live run + */ + +if (process.argv.length < 4) { + console.log('not enough arguments, provide an API url and admin key'); + process.exit(1); +} + +const Promise = require('bluebird'); +const GhostAdminAPI = require('@tryghost/admin-api'); + +const url = process.argv[2]; +const key = process.argv[3]; + +(async function main() { + const doEdit = process.argv[4] === 'true'; + + if (doEdit) { + console.log('REAL Run'); + } else { + console.log('Dry Run - nothing will be edited'); + } + + // Give the user time to read... + await Promise.delay(1000); + + const api = new GhostAdminAPI({ + url, + key, + version: 'canary' + }); + + try { + const site = await api.site.read(); + const allPosts = await api.posts.browse({fields: 'id,slug,mobiledoc,updated_at', limit: 'all'}); + const allPages = await api.pages.browse({fields: 'id,slug,mobiledoc,updated_at', limit: 'all'}); + + console.log(`${allPosts.length} Posts and ${allPages.length} Pages will be checked for mangled markdown and edited if needed`); + // give time to cancel if needed + await Promise.delay(2000); + + const postsResult = await Promise.mapSeries(allPosts, async (post) => { + let edited = false; + + function hasLocal(str) { + return str.indexOf(site.url) > -1 || str.match(/\/content\/images\//); + } + + let mobiledoc = post.mobiledoc; + mobiledoc = mobiledoc.replace(/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/gm, (match, p1, p2, p3) => { + if (!hasLocal(p1) && !hasLocal(p2) && !hasLocal(p3)) { + return match; + } + + edited = true; + console.log(`Found post with potential problem ${post.slug} (${post.id})`); + console.log({match, p1, p2, p3}); + return match; + }); + + if (doEdit && edited) { + // missing data attributes won't be changed + // updated_at is required to pass collision detection + const postData = {id: post.id, updated_at: post.updated_at, mobiledoc}; + await api.posts.edit(postData); + } + + return Promise.delay(0).return(edited); + }); + + console.log(`\nChecked ${postsResult.length} posts and fixed ${postsResult.filter(edited => edited).length}\n`); + // await Promise.delay(1000); + + // const pagesResult = await Promise.mapSeries(allPages, async (page) => { + // console.log(`Re-rendering page ${page.slug} (${page.id})`); + + // const pageData = {id: page.id, updated_at: page.updated_at}; + // await api.pages.edit(pageData, {force_rerender: true}); + + // return Promise.delay(50).return(true); + // }); + + // console.log(`\nRe-rendered ${pagesResult.length} pages`); + } catch (err) { + console.error('There was an error', require('util').inspect(err, false, null)); + process.exit(1); + } +})();