Skip to content

Commit 4a6f062

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 4a6f062

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

fix-markdown-linked-images.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 allPosts = await api.posts.browse({fields: 'id,slug,mobiledoc,updated_at', limit: 'all'});
41+
const allPages = await api.pages.browse({fields: 'id,slug,mobiledoc,updated_at', limit: 'all'});
42+
43+
console.log(`${allPosts.length} Posts and ${allPages.length} Pages will be checked for mangled markdown and edited if needed`);
44+
45+
const postsResult = await Promise.mapSeries(allPosts, async (post) => {
46+
let edited = false;
47+
48+
let mobiledoc = post.mobiledoc;
49+
mobiledoc.replace(/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/gm, (match, p1, p2, p3) => {
50+
if (!p1.match(/\/content\/images\//) && !p2.match(/\/content\/images\//) && !p3.match(/\/content\/images\//)) {
51+
return match;
52+
}
53+
54+
edited = true;
55+
console.log(`Found post with potential problem ${post.slug} (${post.id})`);
56+
console.log({match, p1, p2, p3});
57+
return match;
58+
});
59+
60+
if (doEdit && edited) {
61+
// missing data attributes won't be changed
62+
// updated_at is required to pass collision detection
63+
const postData = {id: post.id, updated_at: post.updated_at, mobiledoc};
64+
await api.posts.edit(postData);
65+
}
66+
67+
return Promise.delay(0).return(edited);
68+
});
69+
70+
console.log(`\nChecked ${postsResult.length} posts and fixed ${postsResult.filter(edited => edited).length}\n`);
71+
// await Promise.delay(1000);
72+
73+
// const pagesResult = await Promise.mapSeries(allPages, async (page) => {
74+
// console.log(`Re-rendering page ${page.slug} (${page.id})`);
75+
76+
// const pageData = {id: page.id, updated_at: page.updated_at};
77+
// await api.pages.edit(pageData, {force_rerender: true});
78+
79+
// return Promise.delay(50).return(true);
80+
// });
81+
82+
// console.log(`\nRe-rendered ${pagesResult.length} pages`);
83+
} catch (err) {
84+
console.error('There was an error', require('util').inspect(err, false, null));
85+
process.exit(1);
86+
}
87+
})();

0 commit comments

Comments
 (0)