@tryghost/mg-ghost-api 0.11.3 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -7
- package/build/index.d.ts +12 -0
- package/{index.js → build/index.js} +3 -5
- package/build/index.js.map +1 -0
- package/build/lib/fetch.d.ts +50 -0
- package/build/lib/fetch.js +97 -0
- package/build/lib/fetch.js.map +1 -0
- package/build/lib/mapper.d.ts +73 -0
- package/build/lib/mapper.js +107 -0
- package/build/lib/mapper.js.map +1 -0
- package/build/test/mapper.test.d.ts +1 -0
- package/build/test/mapper.test.js +60 -0
- package/build/test/mapper.test.js.map +1 -0
- package/package.json +25 -9
- package/lib/fetch.js +0 -143
- package/lib/processor.js +0 -95
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Export content from an existing Ghost installation using the [Admin API](https://docs.ghost.org/admin-api), and generate a `zip` file you can import into a Ghost installation.
|
|
4
4
|
|
|
5
|
+
Posts are fetched in their native Lexical format and stored in a [mg-context](../mg-context) SQLite database (cached in the migration's tmp dir). The exporter writes chunked `posts.json` / `posts-N.json` files via `MigrateContext.writeGhostJson` so very large sites stay within Ghost's import file size limits. Re-running the same command is idempotent — posts are deduplicated by their Ghost ID via mg-context's `lookupKey`.
|
|
6
|
+
|
|
5
7
|
|
|
6
8
|
## Install
|
|
7
9
|
|
|
@@ -42,12 +44,11 @@ It's possible to pass more options, in order to achieve a better migration file
|
|
|
42
44
|
- **`--zip`**
|
|
43
45
|
- bool - default: `true`
|
|
44
46
|
- Create a zip file
|
|
45
|
-
- **`-s` `--scrape`**
|
|
47
|
+
- **`-s` `--scrape`**
|
|
46
48
|
- Configure scraping tasks
|
|
47
|
-
- string - default: `all`
|
|
48
|
-
- Choices: `all`, `
|
|
49
|
+
- string - default: `all`
|
|
50
|
+
- Choices: `all`, `assets`, `none`
|
|
49
51
|
- `all`: Scrape web metadata and download assets
|
|
50
|
-
- `web`: Only scrape metadata from web pages
|
|
51
52
|
- `assets`: Only download assets (images, media, files)
|
|
52
53
|
- `none`: Skip all scraping tasks
|
|
53
54
|
- **`-I` `--info`**
|
|
@@ -58,7 +59,10 @@ It's possible to pass more options, in order to achieve a better migration file
|
|
|
58
59
|
- Batch number to run (defaults to running all)
|
|
59
60
|
- **`-l` `--limit`**
|
|
60
61
|
- number - default: `15`
|
|
61
|
-
- Number of items fetched
|
|
62
|
+
- Number of items fetched per Ghost Admin API request (fetch batch size)
|
|
63
|
+
- **`--postsPerFile`**
|
|
64
|
+
- number - default: `2000`
|
|
65
|
+
- Maximum posts written per output JSON file. Larger sites are split into `posts-1.json`, `posts-2.json`, etc. to keep files at manageable sizes
|
|
62
66
|
- **`--posts`**
|
|
63
67
|
- bool - default: `true`
|
|
64
68
|
- Import posts (set to false to skip)
|
|
@@ -71,9 +75,18 @@ It's possible to pass more options, in order to achieve a better migration file
|
|
|
71
75
|
- **`--pageFilter`**
|
|
72
76
|
- string - default: `null`
|
|
73
77
|
- A string of page filters, as defined in the Ghost Admin API
|
|
74
|
-
- **`--cache`**
|
|
75
|
-
- Persist local cache after migration is complete (Only if `--zip` is `true`)
|
|
78
|
+
- **`--cache`**
|
|
79
|
+
- Persist local cache (including the mg-context SQLite DB) after migration is complete (Only if `--zip` is `true`)
|
|
76
80
|
- bool - default: `true`
|
|
81
|
+
- **`--tmpPath`**
|
|
82
|
+
- string - default: `null`
|
|
83
|
+
- Full path where temporary files (cache + mg-context DB) are stored. Defaults to a hidden tmp dir
|
|
84
|
+
- **`--outputPath`**
|
|
85
|
+
- string - default: `null`
|
|
86
|
+
- Full path where the final zip file is written. Defaults to CWD
|
|
87
|
+
- **`--cacheName`**
|
|
88
|
+
- string - default: `null`
|
|
89
|
+
- Unique name for the cache directory. Defaults to a UUID derived from the URL
|
|
77
90
|
|
|
78
91
|
A more complex migration command could look like this:
|
|
79
92
|
|
|
@@ -83,6 +96,12 @@ migrate ghost --url https://example.com --apikey 1234abcd --batch 5 --limit 10 -
|
|
|
83
96
|
|
|
84
97
|
This will get the first 50 posts with the tag `news` or `press`, in 5 batches of 10 posts, exclude pages, and show all available output in the console.
|
|
85
98
|
|
|
99
|
+
For very large sites, tune `--postsPerFile` to control how the output is chunked:
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
migrate ghost --url https://example.com --apikey 1234abcd --postsPerFile 1000
|
|
103
|
+
```
|
|
104
|
+
|
|
86
105
|
See the [Filter documentation](https://docs.ghost.org/content-api/filtering) for more info.
|
|
87
106
|
|
|
88
107
|
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
contentStats: (options: import("./lib/fetch.js").BaseOptions) => Promise<import("./lib/fetch.js").ContentStatsResult>;
|
|
3
|
+
discover: (options: import("./lib/fetch.js").DiscoverOptions) => Promise<import("./lib/fetch.js").DiscoverResult>;
|
|
4
|
+
fetch: {
|
|
5
|
+
contentStats: (options: import("./lib/fetch.js").BaseOptions) => Promise<import("./lib/fetch.js").ContentStatsResult>;
|
|
6
|
+
discover: (options: import("./lib/fetch.js").DiscoverOptions) => Promise<import("./lib/fetch.js").DiscoverResult>;
|
|
7
|
+
tasks: (options: import("./lib/fetch.js").FetchOptions, ctx: import("./lib/fetch.js").TaskContext) => Promise<import("./lib/fetch.js").ListrTask[]>;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export default _default;
|
|
11
|
+
export { mapPost } from './lib/mapper.js';
|
|
12
|
+
export type { GhostApiPost } from './lib/mapper.js';
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import fetch from './lib/fetch.js';
|
|
2
|
-
import process from './lib/processor.js';
|
|
3
|
-
|
|
4
2
|
const contentStats = fetch.contentStats;
|
|
5
3
|
const discover = fetch.discover;
|
|
6
|
-
|
|
7
4
|
export default {
|
|
8
5
|
contentStats,
|
|
9
6
|
discover,
|
|
10
|
-
fetch
|
|
11
|
-
process
|
|
7
|
+
fetch
|
|
12
8
|
};
|
|
9
|
+
export { mapPost } from './lib/mapper.js';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,gBAAgB,CAAC;AAEnC,MAAM,YAAY,GAAG,KAAK,CAAC,YAAY,CAAC;AACxC,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;AAEhC,eAAe;IACX,YAAY;IACZ,QAAQ;IACR,KAAK;CACR,CAAC;AAEF,OAAO,EAAC,OAAO,EAAC,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import GhostAdminAPI from '@tryghost/admin-api';
|
|
2
|
+
import type { MigrateContext } from '@tryghost/mg-context';
|
|
3
|
+
export interface BaseOptions {
|
|
4
|
+
url: string;
|
|
5
|
+
apikey: string;
|
|
6
|
+
}
|
|
7
|
+
export interface DiscoverOptions extends BaseOptions {
|
|
8
|
+
limit?: number | string;
|
|
9
|
+
posts?: boolean;
|
|
10
|
+
pages?: boolean;
|
|
11
|
+
postFilter?: string | null;
|
|
12
|
+
pageFilter?: string | null;
|
|
13
|
+
}
|
|
14
|
+
export interface FetchOptions extends DiscoverOptions {
|
|
15
|
+
limit: number | string;
|
|
16
|
+
}
|
|
17
|
+
export interface ContentStatsResult {
|
|
18
|
+
posts: number;
|
|
19
|
+
pages: number;
|
|
20
|
+
users: number;
|
|
21
|
+
}
|
|
22
|
+
export interface DiscoverResult {
|
|
23
|
+
site: GhostAdminAPI;
|
|
24
|
+
totals: {
|
|
25
|
+
posts: number;
|
|
26
|
+
pages: number;
|
|
27
|
+
users: number;
|
|
28
|
+
};
|
|
29
|
+
batches: {
|
|
30
|
+
posts: number;
|
|
31
|
+
pages: number;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export interface TaskContext {
|
|
35
|
+
migrateContext: MigrateContext;
|
|
36
|
+
options: {
|
|
37
|
+
limit: number | string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export interface ListrTask {
|
|
41
|
+
title: string;
|
|
42
|
+
task: (ctx: TaskContext) => Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
export type FetchType = 'posts' | 'pages';
|
|
45
|
+
declare const _default: {
|
|
46
|
+
contentStats: (options: BaseOptions) => Promise<ContentStatsResult>;
|
|
47
|
+
discover: (options: DiscoverOptions) => Promise<DiscoverResult>;
|
|
48
|
+
tasks: (options: FetchOptions, ctx: TaskContext) => Promise<ListrTask[]>;
|
|
49
|
+
};
|
|
50
|
+
export default _default;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import GhostAdminAPI from '@tryghost/admin-api';
|
|
2
|
+
import errors from '@tryghost/errors';
|
|
3
|
+
import { mapPost } from './mapper.js';
|
|
4
|
+
const contentStats = async (options) => {
|
|
5
|
+
const site = new GhostAdminAPI({
|
|
6
|
+
url: options.url,
|
|
7
|
+
version: 'v6.0',
|
|
8
|
+
key: options.apikey
|
|
9
|
+
});
|
|
10
|
+
const posts = await site.posts.browse({ limit: 1, fields: 'title' });
|
|
11
|
+
const pages = await site.pages.browse({ limit: 1, fields: 'title' });
|
|
12
|
+
const users = await site.users.browse({ limit: 1, fields: 'name' });
|
|
13
|
+
return {
|
|
14
|
+
posts: posts.meta?.pagination.total ?? 0,
|
|
15
|
+
pages: pages.meta?.pagination.total ?? 0,
|
|
16
|
+
users: users.meta?.pagination.total ?? 0
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const discover = async (options) => {
|
|
20
|
+
const site = new GhostAdminAPI({
|
|
21
|
+
url: options.url,
|
|
22
|
+
version: 'v6.0',
|
|
23
|
+
key: options.apikey
|
|
24
|
+
});
|
|
25
|
+
const posts = options.posts ? await site.posts.browse({ limit: options.limit, filter: options.postFilter ?? null }) : null;
|
|
26
|
+
const pages = options.pages ? await site.pages.browse({ limit: options.limit, filter: options.postFilter ?? null }) : null;
|
|
27
|
+
const users = await site.users.browse({ limit: options.limit });
|
|
28
|
+
return {
|
|
29
|
+
site,
|
|
30
|
+
totals: {
|
|
31
|
+
posts: posts?.meta?.pagination.total ?? 0,
|
|
32
|
+
pages: pages?.meta?.pagination.total ?? 0,
|
|
33
|
+
users: users.meta?.pagination.total ?? 0
|
|
34
|
+
},
|
|
35
|
+
batches: {
|
|
36
|
+
posts: posts?.meta?.pagination.pages ?? 0,
|
|
37
|
+
pages: pages?.meta?.pagination.pages ?? 0
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const fetchPage = async (api, type, options, page) => {
|
|
42
|
+
const params = {
|
|
43
|
+
formats: 'lexical',
|
|
44
|
+
limit: options.limit,
|
|
45
|
+
page: page,
|
|
46
|
+
filter: (type === 'posts' ? options.postFilter : options.pageFilter) || null
|
|
47
|
+
};
|
|
48
|
+
const response = type === 'posts'
|
|
49
|
+
? await api.site.posts.browse(params, { source: 'html' })
|
|
50
|
+
: await api.site.pages.browse(params, { source: 'html' });
|
|
51
|
+
const ghostType = type === 'posts' ? 'post' : 'page';
|
|
52
|
+
response.forEach((item) => {
|
|
53
|
+
item.type = ghostType;
|
|
54
|
+
});
|
|
55
|
+
return response;
|
|
56
|
+
};
|
|
57
|
+
const buildTasks = (tasks, api, type, options) => {
|
|
58
|
+
for (let page = 1; page <= api.batches[type]; page++) {
|
|
59
|
+
tasks.push({
|
|
60
|
+
title: `Fetching ${type}, page ${page} of ${api.batches[type]}`,
|
|
61
|
+
task: async (ctx) => {
|
|
62
|
+
try {
|
|
63
|
+
const response = await fetchPage(api, type, options, page);
|
|
64
|
+
await ctx.migrateContext.transaction(async () => {
|
|
65
|
+
for (const ghPost of response) {
|
|
66
|
+
const post = await mapPost(ghPost, ctx.migrateContext);
|
|
67
|
+
post.save(ctx.migrateContext.db);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.error(`Failed to fetch ${type}, page ${page} of ${api.batches[type]}`, error); // eslint-disable-line no-console
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const tasks = async (options, ctx) => {
|
|
80
|
+
const { limit } = ctx.options;
|
|
81
|
+
if (!ctx.migrateContext) {
|
|
82
|
+
throw new errors.IncorrectUsageError({
|
|
83
|
+
message: 'mg-ghost-api fetch.tasks requires ctx.migrateContext to be set by the caller'
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const api = await discover({ ...options, limit });
|
|
87
|
+
const taskList = [];
|
|
88
|
+
buildTasks(taskList, api, 'posts', options);
|
|
89
|
+
buildTasks(taskList, api, 'pages', options);
|
|
90
|
+
return taskList;
|
|
91
|
+
};
|
|
92
|
+
export default {
|
|
93
|
+
contentStats,
|
|
94
|
+
discover,
|
|
95
|
+
tasks
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/lib/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAEhD,OAAO,MAAM,MAAM,kBAAkB,CAAC;AAEtC,OAAO,EAAC,OAAO,EAAoB,MAAM,aAAa,CAAC;AAkDvD,MAAM,YAAY,GAAG,KAAK,EAAE,OAAoB,EAA+B,EAAE;IAC7E,MAAM,IAAI,GAAG,IAAI,aAAa,CAAC;QAC3B,GAAG,EAAE,OAAO,CAAC,GAAG;QAChB,OAAO,EAAE,MAAM;QACf,GAAG,EAAE,OAAO,CAAC,MAAM;KACtB,CAAC,CAAC;IAEH,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAC,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,EAAC,CAAC,CAAC;IACn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B,CAAC;YACL,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,KAAK,GAAG,KAAK,EAAE,OAAqB,EAAE,GAAgB,EAAwB,EAAE;IAClF,MAAM,EAAC,KAAK,EAAC,GAAG,GAAG,CAAC,OAAO,CAAC;IAE5B,IAAI,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;QACtB,MAAM,IAAI,MAAM,CAAC,mBAAmB,CAAC;YACjC,OAAO,EAAE,8EAA8E;SAC1F,CAAC,CAAC;IACP,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,EAAC,GAAG,OAAO,EAAE,KAAK,EAAC,CAAC,CAAC;IAEhD,MAAM,QAAQ,GAAgB,EAAE,CAAC;IACjC,UAAU,CAAC,QAAQ,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAC5C,UAAU,CAAC,QAAQ,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAE5C,OAAO,QAAQ,CAAC;AACpB,CAAC,CAAC;AAEF,eAAe;IACX,YAAY;IACZ,QAAQ;IACR,KAAK;CACR,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { MigrateContext, PostContext } from '@tryghost/mg-context';
|
|
2
|
+
interface GhostApiAuthor {
|
|
3
|
+
id?: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
slug?: string;
|
|
6
|
+
email?: string;
|
|
7
|
+
profile_image?: string | null;
|
|
8
|
+
cover_image?: string | null;
|
|
9
|
+
bio?: string | null;
|
|
10
|
+
website?: string | null;
|
|
11
|
+
location?: string | null;
|
|
12
|
+
facebook?: string | null;
|
|
13
|
+
twitter?: string | null;
|
|
14
|
+
meta_title?: string | null;
|
|
15
|
+
meta_description?: string | null;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
interface GhostApiTag {
|
|
19
|
+
id?: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
slug?: string;
|
|
22
|
+
description?: string | null;
|
|
23
|
+
feature_image?: string | null;
|
|
24
|
+
visibility?: string;
|
|
25
|
+
og_image?: string | null;
|
|
26
|
+
og_title?: string | null;
|
|
27
|
+
og_description?: string | null;
|
|
28
|
+
twitter_image?: string | null;
|
|
29
|
+
twitter_title?: string | null;
|
|
30
|
+
twitter_description?: string | null;
|
|
31
|
+
meta_title?: string | null;
|
|
32
|
+
meta_description?: string | null;
|
|
33
|
+
codeinjection_head?: string | null;
|
|
34
|
+
codeinjection_foot?: string | null;
|
|
35
|
+
canonical_url?: string | null;
|
|
36
|
+
[key: string]: unknown;
|
|
37
|
+
}
|
|
38
|
+
export interface GhostApiPost {
|
|
39
|
+
id?: string;
|
|
40
|
+
url?: string;
|
|
41
|
+
slug?: string;
|
|
42
|
+
title?: string;
|
|
43
|
+
type?: 'post' | 'page';
|
|
44
|
+
status?: 'published' | 'draft' | 'scheduled' | 'sent';
|
|
45
|
+
visibility?: 'public' | 'members' | 'paid';
|
|
46
|
+
featured?: boolean;
|
|
47
|
+
lexical?: string | null;
|
|
48
|
+
feature_image?: string | null;
|
|
49
|
+
feature_image_alt?: string | null;
|
|
50
|
+
feature_image_caption?: string | null;
|
|
51
|
+
custom_excerpt?: string | null;
|
|
52
|
+
comment_id?: string | null;
|
|
53
|
+
canonical_url?: string | null;
|
|
54
|
+
custom_template?: string | null;
|
|
55
|
+
codeinjection_head?: string | null;
|
|
56
|
+
codeinjection_foot?: string | null;
|
|
57
|
+
og_image?: string | null;
|
|
58
|
+
og_title?: string | null;
|
|
59
|
+
og_description?: string | null;
|
|
60
|
+
twitter_image?: string | null;
|
|
61
|
+
twitter_title?: string | null;
|
|
62
|
+
twitter_description?: string | null;
|
|
63
|
+
meta_title?: string | null;
|
|
64
|
+
meta_description?: string | null;
|
|
65
|
+
created_at?: string | null;
|
|
66
|
+
updated_at?: string | null;
|
|
67
|
+
published_at?: string | null;
|
|
68
|
+
authors?: GhostApiAuthor[];
|
|
69
|
+
tags?: GhostApiTag[];
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
}
|
|
72
|
+
export declare function mapPost(ghPost: GhostApiPost, migrateContext: MigrateContext): Promise<PostContext>;
|
|
73
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { AuthorContext, TagContext } from '@tryghost/mg-context';
|
|
2
|
+
const POST_OBJECT_FIELDS = [
|
|
3
|
+
'title', 'slug', 'status', 'visibility', 'featured', 'type',
|
|
4
|
+
'feature_image', 'feature_image_alt', 'feature_image_caption',
|
|
5
|
+
'custom_excerpt', 'comment_id', 'canonical_url', 'custom_template',
|
|
6
|
+
'codeinjection_head', 'codeinjection_foot',
|
|
7
|
+
'og_image', 'og_title', 'og_description',
|
|
8
|
+
'twitter_image', 'twitter_title', 'twitter_description',
|
|
9
|
+
'meta_title', 'meta_description'
|
|
10
|
+
];
|
|
11
|
+
const DATE_FIELDS = ['created_at', 'updated_at', 'published_at'];
|
|
12
|
+
const MIGRATOR_TAG = {
|
|
13
|
+
name: '#ghost',
|
|
14
|
+
slug: 'hash-ghost',
|
|
15
|
+
visibility: 'internal',
|
|
16
|
+
description: 'Posts migrated from an existing Ghost installation'
|
|
17
|
+
};
|
|
18
|
+
const upgradeGravatar = (url) => {
|
|
19
|
+
let upgraded = url.replace(/s=([0-9]{1,4})/, 's=3000');
|
|
20
|
+
upgraded = upgraded.replace(/^\/\/www\.gravatar\.com/, 'https://www.gravatar.com');
|
|
21
|
+
return upgraded;
|
|
22
|
+
};
|
|
23
|
+
const buildAuthorData = (ghAuthor) => {
|
|
24
|
+
const slug = ghAuthor.slug ?? '';
|
|
25
|
+
const data = {
|
|
26
|
+
name: ghAuthor.name,
|
|
27
|
+
slug,
|
|
28
|
+
email: ghAuthor.email ?? `${slug || 'unknown'}@migrated.invalid`,
|
|
29
|
+
profile_image: ghAuthor.profile_image ?? null,
|
|
30
|
+
cover_image: ghAuthor.cover_image ?? null,
|
|
31
|
+
bio: ghAuthor.bio ?? null,
|
|
32
|
+
website: ghAuthor.website ?? null,
|
|
33
|
+
location: ghAuthor.location ?? null,
|
|
34
|
+
facebook: ghAuthor.facebook ?? null,
|
|
35
|
+
twitter: ghAuthor.twitter ?? null,
|
|
36
|
+
meta_title: ghAuthor.meta_title ?? null,
|
|
37
|
+
meta_description: ghAuthor.meta_description ?? null
|
|
38
|
+
};
|
|
39
|
+
if (typeof data.profile_image === 'string') {
|
|
40
|
+
data.profile_image = upgradeGravatar(data.profile_image);
|
|
41
|
+
}
|
|
42
|
+
return data;
|
|
43
|
+
};
|
|
44
|
+
const buildTagData = (ghTag) => {
|
|
45
|
+
return {
|
|
46
|
+
name: ghTag.name,
|
|
47
|
+
slug: ghTag.slug,
|
|
48
|
+
description: ghTag.description ?? null,
|
|
49
|
+
feature_image: ghTag.feature_image ?? null,
|
|
50
|
+
visibility: ghTag.visibility ?? 'public',
|
|
51
|
+
og_image: ghTag.og_image ?? null,
|
|
52
|
+
og_title: ghTag.og_title ?? null,
|
|
53
|
+
og_description: ghTag.og_description ?? null,
|
|
54
|
+
twitter_image: ghTag.twitter_image ?? null,
|
|
55
|
+
twitter_title: ghTag.twitter_title ?? null,
|
|
56
|
+
twitter_description: ghTag.twitter_description ?? null,
|
|
57
|
+
meta_title: ghTag.meta_title ?? null,
|
|
58
|
+
meta_description: ghTag.meta_description ?? null,
|
|
59
|
+
codeinjection_head: ghTag.codeinjection_head ?? null,
|
|
60
|
+
codeinjection_foot: ghTag.codeinjection_foot ?? null,
|
|
61
|
+
canonical_url: ghTag.canonical_url ?? null
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const toDate = (value) => {
|
|
65
|
+
if (!value) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return new Date(value);
|
|
69
|
+
};
|
|
70
|
+
export async function mapPost(ghPost, migrateContext) {
|
|
71
|
+
const post = await migrateContext.addPost({
|
|
72
|
+
source: { url: ghPost.url, id: ghPost.id },
|
|
73
|
+
lookupKey: ghPost.id
|
|
74
|
+
});
|
|
75
|
+
for (const field of POST_OBJECT_FIELDS) {
|
|
76
|
+
const value = ghPost[field];
|
|
77
|
+
if (value !== undefined && value !== null) {
|
|
78
|
+
post.set(field, value);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (const field of DATE_FIELDS) {
|
|
82
|
+
const date = toDate(ghPost[field]);
|
|
83
|
+
if (date) {
|
|
84
|
+
post.set(field, date);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (ghPost.lexical) {
|
|
88
|
+
post.set('lexical', ghPost.lexical);
|
|
89
|
+
}
|
|
90
|
+
if (ghPost.tags) {
|
|
91
|
+
for (const tag of ghPost.tags) {
|
|
92
|
+
if (tag?.slug) {
|
|
93
|
+
post.addTag(new TagContext({ initialData: buildTagData(tag) }));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
post.addTag(new TagContext({ initialData: MIGRATOR_TAG }));
|
|
98
|
+
if (ghPost.authors) {
|
|
99
|
+
for (const author of ghPost.authors) {
|
|
100
|
+
if (author?.slug) {
|
|
101
|
+
post.addAuthor(new AuthorContext({ initialData: buildAuthorData(author) }));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return post;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=mapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapper.js","sourceRoot":"","sources":["../../src/lib/mapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,UAAU,EAAC,MAAM,sBAAsB,CAAC;AA4E/D,MAAM,kBAAkB,GAAG;IACvB,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM;IAC3D,eAAe,EAAE,mBAAmB,EAAE,uBAAuB;IAC7D,gBAAgB,EAAE,YAAY,EAAE,eAAe,EAAE,iBAAiB;IAClE,oBAAoB,EAAE,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CAAC;YAClE,CAAC;QACL,CAAC;IACL,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,EAAC,WAAW,EAAE,YAAY,EAAC,CAAC,CAAC,CAAC;IAEzD,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACjB,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YAClC,IAAI,MAAM,EAAE,IAAI,EAAE,CAAC;gBACf,IAAI,CAAC,SAAS,CAAC,IAAI,aAAa,CAAC,EAAC,WAAW,EAAE,eAAe,CAAC,MAAM,CAAC,EAAC,CAAC,CAAC,CAAC;YAC9E,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { after, before, describe, it } from 'node:test';
|
|
5
|
+
import { MigrateContext } from '@tryghost/mg-context';
|
|
6
|
+
import { mapPost } from '../lib/mapper.js';
|
|
7
|
+
const __dirname = new URL('.', import.meta.url).pathname;
|
|
8
|
+
const fixturesPath = join(__dirname, '../../src/test/fixtures');
|
|
9
|
+
const fixture = JSON.parse(readFileSync(join(fixturesPath, 'posts.json'), 'utf8'));
|
|
10
|
+
describe('mapPost', function () {
|
|
11
|
+
let ctx;
|
|
12
|
+
before(async function () {
|
|
13
|
+
ctx = new MigrateContext({ contentFormat: 'lexical' });
|
|
14
|
+
await ctx.init();
|
|
15
|
+
});
|
|
16
|
+
after(async function () {
|
|
17
|
+
await ctx.close();
|
|
18
|
+
});
|
|
19
|
+
it('populates a PostContext from a Ghost API post', async function () {
|
|
20
|
+
const post = await mapPost(fixture.posts[0], ctx);
|
|
21
|
+
post.save(ctx.db);
|
|
22
|
+
assert.equal(post.get('title'), 'Welcome');
|
|
23
|
+
assert.equal(post.get('slug'), 'welcome-short');
|
|
24
|
+
assert.equal(post.get('status'), 'published');
|
|
25
|
+
assert.equal(post.get('feature_image'), 'https://casper.ghost.org/v2.0.0/images/welcome-to-ghost.jpg');
|
|
26
|
+
assert.equal(post.get('custom_excerpt'), 'Welcome, it\'s great to have you here.');
|
|
27
|
+
const lexical = post.get('lexical');
|
|
28
|
+
assert.ok(lexical, 'lexical was preserved from the Ghost API response');
|
|
29
|
+
assert.match(lexical, /Welcome/);
|
|
30
|
+
assert.ok(post.get('created_at') instanceof Date);
|
|
31
|
+
assert.ok(post.get('published_at') instanceof Date);
|
|
32
|
+
});
|
|
33
|
+
it('adds the #ghost migrator tag alongside source tags', async function () {
|
|
34
|
+
const post = await mapPost(fixture.posts[0], ctx);
|
|
35
|
+
post.save(ctx.db);
|
|
36
|
+
const tags = post.get('tags');
|
|
37
|
+
assert.equal(tags.length, 2, 'one source tag plus the #ghost migrator tag');
|
|
38
|
+
const ghostTag = tags.find((t) => t.data.slug === 'hash-ghost');
|
|
39
|
+
assert.ok(ghostTag, '#ghost tag is present');
|
|
40
|
+
assert.equal(ghostTag.data.name, '#ghost');
|
|
41
|
+
assert.equal(ghostTag.data.visibility, 'internal');
|
|
42
|
+
});
|
|
43
|
+
it('upgrades gravatar URLs on author profile_image', async function () {
|
|
44
|
+
const ghPost = {
|
|
45
|
+
...fixture.posts[0],
|
|
46
|
+
authors: [{
|
|
47
|
+
slug: 'gravatar-user',
|
|
48
|
+
name: 'Gravatar User',
|
|
49
|
+
email: 'gravatar@example.com',
|
|
50
|
+
profile_image: '//www.gravatar.com/avatar/abc123?s=250'
|
|
51
|
+
}]
|
|
52
|
+
};
|
|
53
|
+
const post = await mapPost(ghPost, ctx);
|
|
54
|
+
post.save(ctx.db);
|
|
55
|
+
const authors = post.get('authors');
|
|
56
|
+
assert.equal(authors.length, 1);
|
|
57
|
+
assert.equal(authors[0].data.profile_image, 'https://www.gravatar.com/avatar/abc123?s=3000');
|
|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/mg-ghost-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/migrate.git",
|
|
@@ -8,25 +8,41 @@
|
|
|
8
8
|
},
|
|
9
9
|
"author": "Ghost Foundation",
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"main": "index.js",
|
|
12
11
|
"type": "module",
|
|
12
|
+
"main": "build/index.js",
|
|
13
|
+
"types": "build/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"source": "./src/index.ts",
|
|
17
|
+
"default": "./build/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
13
20
|
"files": [
|
|
14
|
-
"
|
|
15
|
-
"lib"
|
|
21
|
+
"build"
|
|
16
22
|
],
|
|
17
23
|
"publishConfig": {
|
|
18
24
|
"access": "public"
|
|
19
25
|
},
|
|
20
26
|
"devDependencies": {
|
|
21
|
-
"
|
|
27
|
+
"@types/node": "24.12.4",
|
|
28
|
+
"@typescript-eslint/parser": "8.60.0",
|
|
29
|
+
"c8": "11.0.0",
|
|
30
|
+
"eslint": "8.57.1",
|
|
31
|
+
"eslint-plugin-ghost": "3.5.0",
|
|
32
|
+
"typescript": "6.0.3"
|
|
22
33
|
},
|
|
23
34
|
"dependencies": {
|
|
24
|
-
"@tryghost/admin-api": "1.14.6"
|
|
35
|
+
"@tryghost/admin-api": "1.14.6",
|
|
36
|
+
"@tryghost/errors": "3.0.3",
|
|
37
|
+
"@tryghost/mg-context": "0.10.0"
|
|
25
38
|
},
|
|
26
39
|
"scripts": {
|
|
27
40
|
"dev": "echo \"Implement me!\"",
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"lint": "eslint
|
|
41
|
+
"build:watch": "tsc --watch --preserveWatchOutput --sourceMap",
|
|
42
|
+
"build": "tsc --build --sourceMap",
|
|
43
|
+
"lint": "eslint src/ --ext .ts --cache",
|
|
44
|
+
"test": "c8 --src src --all --reporter text --reporter cobertura node --test 'build/test/**/*.test.js'",
|
|
45
|
+
"test:local": "pnpm build && pnpm test",
|
|
46
|
+
"posttest": "pnpm lint"
|
|
31
47
|
}
|
|
32
48
|
}
|
package/lib/fetch.js
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import ghostAPI from '@tryghost/admin-api';
|
|
2
|
-
|
|
3
|
-
const contentStats = async (options) => {
|
|
4
|
-
const requestOptions = {
|
|
5
|
-
url: options.url,
|
|
6
|
-
version: 'v6.0',
|
|
7
|
-
key: options.apikey
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const site = new ghostAPI(requestOptions);
|
|
11
|
-
|
|
12
|
-
// Request the smallest amount of data possible
|
|
13
|
-
const posts = await site.posts.browse({limit: 1, fields: 'title'});
|
|
14
|
-
const pages = await site.pages.browse({limit: 1, fields: 'title'});
|
|
15
|
-
const users = await site.users.browse({limit: 1, fields: 'name'});
|
|
16
|
-
|
|
17
|
-
return {
|
|
18
|
-
posts: posts.meta.pagination.total,
|
|
19
|
-
pages: pages.meta.pagination.total,
|
|
20
|
-
users: users.meta.pagination.total
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const discover = async (options) => {
|
|
25
|
-
const requestOptions = {
|
|
26
|
-
url: options.url,
|
|
27
|
-
version: 'v6.0',
|
|
28
|
-
key: options.apikey
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const site = new ghostAPI(requestOptions);
|
|
32
|
-
|
|
33
|
-
const posts = (options.posts) ? await site.posts.browse({limit: options.limit, filter: options?.postFilter ?? null}) : null;
|
|
34
|
-
const pages = (options.pages) ? await site.pages.browse({limit: options.limit, filter: options?.postFilter ?? null}) : null;
|
|
35
|
-
const users = await site.users.browse({limit: options.limit});
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
site,
|
|
39
|
-
totals: {
|
|
40
|
-
posts: (posts && posts.meta.pagination) && posts.meta.pagination.total ? posts.meta.pagination.total : 0,
|
|
41
|
-
pages: (pages && pages.meta.pagination) && pages.meta.pagination.total ? pages.meta.pagination.total : 0,
|
|
42
|
-
users: users.meta.pagination && users.meta.pagination.total ? users.meta.pagination.total : 0
|
|
43
|
-
},
|
|
44
|
-
batches: {
|
|
45
|
-
posts: (posts && posts.meta.pagination) && posts.meta.pagination.pages ? posts.meta.pagination.pages : 0,
|
|
46
|
-
pages: (pages && pages.meta.pagination) && pages.meta.pagination.pages ? pages.meta.pagination.pages : 0,
|
|
47
|
-
users: users.meta.pagination && users.meta.pagination.pages ? users.meta.pagination.pages : 0
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const cachedFetch = async (fileCache, api, type, options, page) => {
|
|
53
|
-
let filename = `gh_api_${type}_${options.limit}_${page}.json`;
|
|
54
|
-
|
|
55
|
-
if (fileCache.hasFile(filename, 'tmp')) {
|
|
56
|
-
return await fileCache.readTmpJSONFile(filename);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
let response = null;
|
|
60
|
-
|
|
61
|
-
const postParams = {
|
|
62
|
-
formats: 'html',
|
|
63
|
-
limit: options.limit,
|
|
64
|
-
page: page,
|
|
65
|
-
filter: options.postFilter || null
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const pageParams = {
|
|
69
|
-
formats: 'html',
|
|
70
|
-
limit: options.limit,
|
|
71
|
-
page: page,
|
|
72
|
-
filter: options.pageFilter || null
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const userParams = {
|
|
76
|
-
limit: options.limit,
|
|
77
|
-
page: page
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
if (type === 'posts') {
|
|
81
|
-
response = await api.site.posts.browse(postParams, {source: 'html'});
|
|
82
|
-
response.forEach((item) => {
|
|
83
|
-
item.type = 'post';
|
|
84
|
-
});
|
|
85
|
-
} else if (type === 'pages') {
|
|
86
|
-
response = await api.site.pages.browse(pageParams, {source: 'html'});
|
|
87
|
-
response.forEach((item) => {
|
|
88
|
-
item.type = 'page';
|
|
89
|
-
});
|
|
90
|
-
} else {
|
|
91
|
-
response = await api.site.users.browse(userParams);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
await fileCache.writeTmpFile(response, filename);
|
|
95
|
-
|
|
96
|
-
return response;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const buildTasks = (fileCache, tasks, api, type, options) => {
|
|
100
|
-
for (let page = 1; page <= api.batches[type]; page++) {
|
|
101
|
-
tasks.push({
|
|
102
|
-
title: `Fetching ${type}, page ${page} of ${api.batches[type]}`,
|
|
103
|
-
task: async (ctx) => {
|
|
104
|
-
try {
|
|
105
|
-
let response = await cachedFetch(fileCache, api, type, options, page);
|
|
106
|
-
|
|
107
|
-
// This is weird, but we don't yet deal with pages as a separate concept in imports
|
|
108
|
-
type = (type === 'pages') ? 'posts' : type;
|
|
109
|
-
|
|
110
|
-
ctx.result[type] = ctx.result[type].concat(response);
|
|
111
|
-
} catch (error) {
|
|
112
|
-
console.error(`Failed to fetch ${type}, page ${page} of ${api.batches[type]}`, error); // eslint-disable-line no-console
|
|
113
|
-
throw error;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const tasks = async (options, ctx) => {
|
|
121
|
-
const {limit} = ctx.options;
|
|
122
|
-
|
|
123
|
-
const api = await discover(options, {limit});
|
|
124
|
-
|
|
125
|
-
const tasks = []; // eslint-disable-line no-shadow
|
|
126
|
-
|
|
127
|
-
ctx.result = {
|
|
128
|
-
posts: [],
|
|
129
|
-
users: []
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
buildTasks(ctx.fileCache, tasks, api, 'posts', options);
|
|
133
|
-
buildTasks(ctx.fileCache, tasks, api, 'pages', options);
|
|
134
|
-
buildTasks(ctx.fileCache, tasks, api, 'users', options);
|
|
135
|
-
|
|
136
|
-
return tasks;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
export default {
|
|
140
|
-
contentStats,
|
|
141
|
-
discover,
|
|
142
|
-
tasks
|
|
143
|
-
};
|
package/lib/processor.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
const processTags = (ghTags) => {
|
|
2
|
-
let tags = [];
|
|
3
|
-
|
|
4
|
-
ghTags.forEach((tag) => {
|
|
5
|
-
// If the tag is internal (which typically comes from another Ghost site), it's URL value is a 404 page
|
|
6
|
-
// The tools merge tags with the same slug, so this means multiple tags with the same URL don't get merged
|
|
7
|
-
// TODO: Find out where tags with the same URL are being 'merged'
|
|
8
|
-
if (tag.visibility === 'internal') {
|
|
9
|
-
tag.url = `#${tag.slug}`;
|
|
10
|
-
}
|
|
11
|
-
tags.push({url: tag.url, data: tag});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
tags.push({
|
|
15
|
-
url: 'migrator-added-tag', data: {
|
|
16
|
-
name: '#ghost',
|
|
17
|
-
slug: 'hash-ghost',
|
|
18
|
-
description: 'Posts migrated from an existing Ghost installation',
|
|
19
|
-
visibility: 'internal'
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
return tags;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const processPost = (ghPost) => {
|
|
27
|
-
const post = {
|
|
28
|
-
url: ghPost.url,
|
|
29
|
-
data: ghPost
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
post.data.authors = processAuthors(ghPost.authors);
|
|
33
|
-
|
|
34
|
-
if (ghPost.tags && ghPost.tags.length > 0) {
|
|
35
|
-
post.data.tags = processTags(ghPost.tags);
|
|
36
|
-
} else {
|
|
37
|
-
delete post.data.tags;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
delete post.data.tiers;
|
|
41
|
-
delete post.data.post_revisions;
|
|
42
|
-
delete post.data.count;
|
|
43
|
-
delete post.data.primary_author;
|
|
44
|
-
delete post.data.primary_tag;
|
|
45
|
-
|
|
46
|
-
return post;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const processPosts = (posts) => {
|
|
50
|
-
const results = [];
|
|
51
|
-
for (let i = 0; i < posts.length; i++) {
|
|
52
|
-
const post = posts[i];
|
|
53
|
-
if (post) {
|
|
54
|
-
results.push(processPost(post));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return results;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const processAuthor = (ghAuthor) => {
|
|
61
|
-
let authorData = {
|
|
62
|
-
url: ghAuthor.url,
|
|
63
|
-
data: ghAuthor
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
if (ghAuthor.profile_image) {
|
|
67
|
-
let profileImage = ghAuthor.profile_image.replace(/s=([0-9]{1,4})/, 's=3000');
|
|
68
|
-
profileImage = profileImage.replace(/\/\/www.gravatar.com/, 'https://www.gravatar.com');
|
|
69
|
-
authorData.data.profile_image = profileImage;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return authorData;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const processAuthors = (authors) => {
|
|
76
|
-
return authors.map(author => processAuthor(author));
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const all = async ({result: input}) => {
|
|
80
|
-
const output = {};
|
|
81
|
-
|
|
82
|
-
output.users = processAuthors(input.users);
|
|
83
|
-
output.posts = processPosts(input.posts, output.users);
|
|
84
|
-
|
|
85
|
-
return output;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export default {
|
|
89
|
-
processTags,
|
|
90
|
-
processPost,
|
|
91
|
-
processPosts,
|
|
92
|
-
processAuthor,
|
|
93
|
-
processAuthors,
|
|
94
|
-
all
|
|
95
|
-
};
|