Skip to content

Commit e62addb

Browse files
committed
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)
1 parent 56b486e commit e62addb

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

fix-markdown-linked-images.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Finds and fixes any mangled
3+
*
4+
* Usage:
5+
*
6+
* node fix-markdown-linked-images.js https://blah.ghost.io ADMIN_API_KEY - dry run
7+
* node fix-markdown-linked-images.js https://blah.ghost.io ADMIN_API_KEY true - live run
8+
*/
9+
10+
if (process.argv.length < 4) {
11+
console.log('not enough arguments, provide an API url and admin key');
12+
process.exit(1);
13+
}
14+
15+
const Promise = require('bluebird');
16+
const GhostAdminAPI = require('@tryghost/admin-api');
17+
18+
const url = process.argv[2];
19+
const key = process.argv[3];
20+
21+
(async function main() {
22+
const doEdit = process.argv[4] === 'true';
23+
24+
if (doEdit) {
25+
console.log('REAL Run');
26+
} else {
27+
console.log('Dry Run - nothing will be edited');
28+
}
29+
30+
// Give the user time to read...
31+
await Promise.delay(1000);
32+
33+
const api = new GhostAdminAPI({
34+
url,
35+
key,
36+
version: 'canary'
37+
});
38+
39+
try {
40+
const site = await api.site.read();
41+
const allPosts = await api.posts.browse({fields: 'id,slug,mobiledoc,updated_at', limit: 'all'});
42+
const allPages = await api.pages.browse({fields: 'id,slug,mobiledoc,updated_at', limit: 'all'});
43+
44+
console.log(`${allPosts.length} Posts and ${allPages.length} Pages will be checked for mangled markdown and edited if needed`);
45+
// give time to cancel if needed
46+
await Promise.delay(2000);
47+
48+
const postsResult = await Promise.mapSeries(allPosts, async (post) => {
49+
let edited = false;
50+
51+
function hasLocal(str) {
52+
return str.indexOf(site.url) > -1 || str.match(/\/content\/images\//);
53+
}
54+
55+
let mobiledoc = post.mobiledoc;
56+
mobiledoc = mobiledoc.replace(/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/gm, (match, p1, p2, p3) => {
57+
if (!hasLocal(p1) && !hasLocal(p2) && !hasLocal(p3)) {
58+
return match;
59+
}
60+
61+
edited = true;
62+
console.log(`Found post with potential problem ${post.slug} (${post.id})`);
63+
console.log({match, p1, p2, p3});
64+
return match;
65+
});
66+
67+
if (doEdit && edited) {
68+
// missing data attributes won't be changed
69+
// updated_at is required to pass collision detection
70+
const postData = {id: post.id, updated_at: post.updated_at, mobiledoc};
71+
await api.posts.edit(postData);
72+
}
73+
74+
return Promise.delay(0).return(edited);
75+
});
76+
77+
console.log(`\nChecked ${postsResult.length} posts and fixed ${postsResult.filter(edited => edited).length}\n`);
78+
// await Promise.delay(1000);
79+
80+
// const pagesResult = await Promise.mapSeries(allPages, async (page) => {
81+
// console.log(`Re-rendering page ${page.slug} (${page.id})`);
82+
83+
// const pageData = {id: page.id, updated_at: page.updated_at};
84+
// await api.pages.edit(pageData, {force_rerender: true});
85+
86+
// return Promise.delay(50).return(true);
87+
// });
88+
89+
// console.log(`\nRe-rendered ${pagesResult.length} pages`);
90+
} catch (err) {
91+
console.error('There was an error', require('util').inspect(err, false, null));
92+
process.exit(1);
93+
}
94+
})();

0 commit comments

Comments
 (0)