@tryghost/mg-medium-export 0.27.8-0 → 0.28.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 +83 -10
- package/build/index.d.ts +7 -0
- package/build/index.js +6 -0
- package/build/index.js.map +1 -0
- package/build/lib/ingest.d.ts +22 -0
- package/build/lib/ingest.js +66 -0
- package/build/lib/ingest.js.map +1 -0
- package/build/lib/map.d.ts +23 -0
- package/build/lib/map.js +245 -0
- package/build/lib/map.js.map +1 -0
- package/build/lib/process-content.d.ts +21 -0
- package/{lib → build/lib}/process-content.js +14 -47
- package/build/lib/process-content.js.map +1 -0
- package/build/lib/process-post.d.ts +17 -0
- package/build/lib/process-post.js +176 -0
- package/build/lib/process-post.js.map +1 -0
- package/build/lib/process-profile.d.ts +6 -0
- package/{lib → build/lib}/process-profile.js +10 -14
- package/build/lib/process-profile.js.map +1 -0
- package/build/lib/types.d.ts +58 -0
- package/build/lib/types.js +3 -0
- package/build/lib/types.js.map +1 -0
- package/build/test/helpers.d.ts +39 -0
- package/build/test/helpers.js +78 -0
- package/build/test/helpers.js.map +1 -0
- package/build/test/ingest.test.d.ts +1 -0
- package/build/test/ingest.test.js +115 -0
- package/build/test/ingest.test.js.map +1 -0
- package/build/test/map.test.d.ts +1 -0
- package/build/test/map.test.js +286 -0
- package/build/test/map.test.js.map +1 -0
- package/build/test/process-profile.test.d.ts +1 -0
- package/build/test/process-profile.test.js +70 -0
- package/build/test/process-profile.test.js.map +1 -0
- package/build/test/process.test.d.ts +1 -0
- package/build/test/process.test.js +505 -0
- package/build/test/process.test.js.map +1 -0
- package/package.json +22 -12
- package/index.js +0 -11
- package/lib/process-post.js +0 -182
- package/lib/process.js +0 -24
- package/lib/read-zip.js +0 -83
package/lib/process-post.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import {domUtils} from '@tryghost/mg-utils';
|
|
2
|
-
import string from '@tryghost/string';
|
|
3
|
-
import processContent from './process-content.js';
|
|
4
|
-
|
|
5
|
-
const sectionTags = ['aside', 'blockquote', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'img'];
|
|
6
|
-
|
|
7
|
-
const processMeta = ({name, parsed, options}) => {
|
|
8
|
-
const mediumAsCanonical = options?.mediumAsCanonical ?? false;
|
|
9
|
-
|
|
10
|
-
let urlInfo;
|
|
11
|
-
|
|
12
|
-
const dateNow = new Date();
|
|
13
|
-
|
|
14
|
-
const post = {
|
|
15
|
-
url: parsed.$('.p-canonical')[0]?.getAttribute('href'),
|
|
16
|
-
data: {
|
|
17
|
-
title: parsed.$('.p-name')[0]?.textContent || '',
|
|
18
|
-
custom_excerpt: (parsed.$('.p-summary')[0]?.textContent || '').trim()
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
if (/^(?:posts\/)?draft/.test(name)) {
|
|
23
|
-
urlInfo = name.match(/_(.*?)-([0-9a-f]+)\.html/);
|
|
24
|
-
const footerLink = parsed.$('footer p a')[0];
|
|
25
|
-
post.url = footerLink?.getAttribute('href');
|
|
26
|
-
post.data.status = 'draft';
|
|
27
|
-
post.data.created_at = dateNow;
|
|
28
|
-
post.data.updated_at = dateNow;
|
|
29
|
-
} else {
|
|
30
|
-
urlInfo = post.url.match(/([^/]*?)-([0-9a-f]+)$/);
|
|
31
|
-
|
|
32
|
-
// urlInfo can be null if the canonical url is a profile url followed by ID, without a post title slug
|
|
33
|
-
// i.e. https://medium.com/@username/1234567890, instead of https://medium.com/@username/post-title-1234567890
|
|
34
|
-
if (urlInfo === null) {
|
|
35
|
-
urlInfo = name.match(/_(.*?)-([0-9a-f]+)\.html/);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
post.data.status = 'published';
|
|
39
|
-
const publishedDatetime = parsed.$('.dt-published')[0]?.getAttribute('datetime');
|
|
40
|
-
post.data.created_at = publishedDatetime || dateNow;
|
|
41
|
-
post.data.published_at = publishedDatetime || dateNow;
|
|
42
|
-
post.data.updated_at = publishedDatetime || dateNow;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (mediumAsCanonical) {
|
|
46
|
-
const canonicalUrl = parsed.$('.p-canonical')[0]?.getAttribute('href');
|
|
47
|
-
post.data.canonical_url = canonicalUrl;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Ensure the slug is clean and valid according to Ghost
|
|
51
|
-
post.data.slug = string.slugify(urlInfo[1]);
|
|
52
|
-
// Store the medium ID in the comment ID legacy field - not sure if this is useful
|
|
53
|
-
post.data.comment_id = urlInfo[2];
|
|
54
|
-
|
|
55
|
-
return post;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const processAuthor = ({pAuthor}) => {
|
|
59
|
-
const href = pAuthor.getAttribute('href');
|
|
60
|
-
return {
|
|
61
|
-
url: href,
|
|
62
|
-
data: {
|
|
63
|
-
name: pAuthor.textContent,
|
|
64
|
-
slug: href.replace(/.*?@(.*?)$/, (m, p) => p.toLowerCase()),
|
|
65
|
-
roles: ['Contributor']
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const processTags = ({tagLinks}) => {
|
|
71
|
-
return tagLinks.map(tag => {
|
|
72
|
-
const href = tag.getAttribute('href');
|
|
73
|
-
return {
|
|
74
|
-
url: href,
|
|
75
|
-
data: {
|
|
76
|
-
name: tag.textContent,
|
|
77
|
-
slug: href.replace(/.*\/(.*?)$/, (m, p) => p.toLowerCase())
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const processFeatureImage = ({html, post, options}) => {
|
|
84
|
-
return domUtils.processFragment(html, parsed => {
|
|
85
|
-
// Look for data-is-featured
|
|
86
|
-
let featured = parsed.$('[data-is-featured]')[0] || null;
|
|
87
|
-
|
|
88
|
-
// Look for an image that appears before content
|
|
89
|
-
let allSections = parsed.$(sectionTags.join(','));
|
|
90
|
-
let foundImg = false;
|
|
91
|
-
let preImageTags = [];
|
|
92
|
-
|
|
93
|
-
allSections.forEach(el => {
|
|
94
|
-
if (!foundImg) {
|
|
95
|
-
preImageTags.push(el.tagName.toLowerCase());
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (!foundImg && el.tagName.toLowerCase() === 'img') {
|
|
99
|
-
foundImg = el;
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
// We don't have a designated feature image, but there's an image above the content so use that image instead
|
|
104
|
-
if (!featured && !preImageTags.includes('p')) {
|
|
105
|
-
featured = foundImg;
|
|
106
|
-
|
|
107
|
-
if (options?.addPlatformTag) {
|
|
108
|
-
// tag it with #auto-feature-image so we can tell the difference
|
|
109
|
-
post.data.tags.push({
|
|
110
|
-
data: {
|
|
111
|
-
name: '#auto-feature-image'
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (featured) {
|
|
118
|
-
post.data.feature_image = featured.getAttribute('src');
|
|
119
|
-
post.data.feature_image_alt = featured.getAttribute('alt') || null;
|
|
120
|
-
const figure = featured.closest('figure');
|
|
121
|
-
const figcaption = figure ? figure.querySelector('figcaption') : null;
|
|
122
|
-
post.data.feature_image_caption = figcaption ? domUtils.serializeChildren(figcaption).trim() : null;
|
|
123
|
-
|
|
124
|
-
if (figure) {
|
|
125
|
-
figure.remove();
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return parsed.html().trim();
|
|
130
|
-
});
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
export default ({name, html, globalUser, options}) => {
|
|
134
|
-
return domUtils.processFragment(html, parsed => {
|
|
135
|
-
let post = processMeta({name, parsed, options});
|
|
136
|
-
|
|
137
|
-
// Process author
|
|
138
|
-
const pAuthor = parsed.$('.p-author')[0];
|
|
139
|
-
if (pAuthor) {
|
|
140
|
-
post.data.author = processAuthor({pAuthor});
|
|
141
|
-
} else if (globalUser) {
|
|
142
|
-
post.data.author = globalUser;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
post.data.tags = [];
|
|
146
|
-
|
|
147
|
-
if (options?.addTag) {
|
|
148
|
-
post.data.tags.push({
|
|
149
|
-
url: 'migrator-added-tag',
|
|
150
|
-
data: {
|
|
151
|
-
name: options.addTag
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Process tags
|
|
157
|
-
const tagLinks = parsed.$('.p-tags a');
|
|
158
|
-
if (tagLinks.length) {
|
|
159
|
-
post.data.tags = [...post.data.tags, ...processTags({tagLinks})];
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (options?.addPlatformTag) {
|
|
163
|
-
post.data.tags.push({
|
|
164
|
-
url: 'migrator-added-platform-tag',
|
|
165
|
-
data: {
|
|
166
|
-
name: '#medium'
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Process content
|
|
172
|
-
const eContent = parsed.$('.e-content')[0];
|
|
173
|
-
const contentHtml = eContent ? domUtils.serializeChildren(eContent) : '';
|
|
174
|
-
post = processContent({html: contentHtml, post});
|
|
175
|
-
|
|
176
|
-
// Grab the featured image
|
|
177
|
-
// Do this last so that we can add tags to indicate feature image style
|
|
178
|
-
post.data.html = processFeatureImage({html: post.data.html, post, options});
|
|
179
|
-
|
|
180
|
-
return post;
|
|
181
|
-
});
|
|
182
|
-
};
|
package/lib/process.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import processPost from './process-post.js';
|
|
2
|
-
import processProfile from './process-profile.js';
|
|
3
|
-
|
|
4
|
-
export default (input, options) => {
|
|
5
|
-
let output = {};
|
|
6
|
-
|
|
7
|
-
if (input.profile || input.profiles) {
|
|
8
|
-
output.users = [processProfile({html: input.profile || input.profiles})];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
let globalUser = output.users && output.users.length === 1 ? output.users[0] : null;
|
|
12
|
-
|
|
13
|
-
if (input.posts && input.posts.length > 0) {
|
|
14
|
-
output.posts = [];
|
|
15
|
-
for (let i = 0; i < input.posts.length; i++) {
|
|
16
|
-
const post = input.posts[i];
|
|
17
|
-
if (post) {
|
|
18
|
-
output.posts.push(processPost({name: post.name, html: post.html, globalUser, options}));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return output;
|
|
24
|
-
};
|
package/lib/read-zip.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import {lstatSync, readdirSync} from 'node:fs';
|
|
2
|
-
import {join} from 'node:path';
|
|
3
|
-
import fsUtils from '@tryghost/mg-fs-utils';
|
|
4
|
-
import {_base as debugFactory} from '@tryghost/debug';
|
|
5
|
-
|
|
6
|
-
const debug = debugFactory('migrate:medium:lib:read-zip');
|
|
7
|
-
|
|
8
|
-
const contentStats = async zipPath => {
|
|
9
|
-
const entries = await fsUtils.readZipEntries(zipPath);
|
|
10
|
-
const posts = entries.filter(value => /^posts\/.*\.html$/.test(value)).length;
|
|
11
|
-
const users = entries.filter(value => value.startsWith('profile/profile.html')).length;
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
posts: posts,
|
|
15
|
-
users: users
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const readMediumZip = ({content, zipPath, options, skippedFileCount}) => {
|
|
20
|
-
fsUtils.zip.read(zipPath, (entryName, zipEntry) => {
|
|
21
|
-
const entryHtml = zipEntry.getData().toString('utf8');
|
|
22
|
-
|
|
23
|
-
// Catch all HTML files inside `profile/`
|
|
24
|
-
if (entryName.startsWith('profile/profile.html')) {
|
|
25
|
-
content.profiles.push({data: entryHtml});
|
|
26
|
-
|
|
27
|
-
// Catch all HTML files in `posts/`
|
|
28
|
-
} else if (/^posts\/.*\.html$/.test(entryName)) {
|
|
29
|
-
content.posts.push({
|
|
30
|
-
name: entryName,
|
|
31
|
-
html: entryHtml
|
|
32
|
-
});
|
|
33
|
-
// Skip if not matched above, and report skipped files if `--verbose`
|
|
34
|
-
} else if (/.html$/.test(entryName)) {
|
|
35
|
-
if (options.verbose) {
|
|
36
|
-
debug(`Skipped: ${entryName}`);
|
|
37
|
-
}
|
|
38
|
-
skippedFileCount += 1;
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
content,
|
|
44
|
-
skippedFileCount
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export default (zipPath, options) => {
|
|
49
|
-
let content = {
|
|
50
|
-
profiles: [],
|
|
51
|
-
posts: []
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
let skippedFileCount = 0;
|
|
55
|
-
|
|
56
|
-
// We only support the current Medium export file structure:
|
|
57
|
-
// - posts
|
|
58
|
-
// - the-post-name-b3h4k5l6b2u5.html
|
|
59
|
-
// - another-post-name-w9g8b5a4v6n8.html
|
|
60
|
-
// - profile
|
|
61
|
-
// - file.html
|
|
62
|
-
|
|
63
|
-
const ifIsDirectory = lstatSync(zipPath).isDirectory();
|
|
64
|
-
|
|
65
|
-
if (ifIsDirectory) {
|
|
66
|
-
readdirSync(zipPath).forEach(file => {
|
|
67
|
-
const filePath = join(zipPath, file);
|
|
68
|
-
const zipContent = readMediumZip({content, zipPath: filePath, options, skippedFileCount});
|
|
69
|
-
content = zipContent.content;
|
|
70
|
-
skippedFileCount = zipContent.skippedFileCount;
|
|
71
|
-
});
|
|
72
|
-
} else {
|
|
73
|
-
const zipContent = readMediumZip({content, zipPath, options, skippedFileCount});
|
|
74
|
-
content = zipContent.content;
|
|
75
|
-
skippedFileCount = zipContent.skippedFileCount;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
debug(`Skipped files: ${skippedFileCount}`);
|
|
79
|
-
|
|
80
|
-
return content;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export {contentStats};
|