-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script to fix linked markdown images mangled by URL transforms
refs TryGhost/Product#596 - fetch all posts and pages from the API - check mobiledoc for existence of linked images - fix any linked images that have mangled markup - fix any URLs that weren't correctly transformed from relative to absolute (stored with `__GHOST_URL__` internally)
- Loading branch information
1 parent
56b486e
commit e62addb
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
})(); |