@tryghost/mg-medium-export 0.27.2 → 0.27.4
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 +2 -2
- package/index.js +1 -3
- package/lib/process-content.js +45 -15
- package/lib/process-post.js +5 -7
- package/lib/process-profile.js +3 -5
- package/lib/read-zip.js +7 -9
- package/package.json +6 -9
package/README.md
CHANGED
|
@@ -97,8 +97,8 @@ pnpm dev medium --pathToZip /path/to/my-export.zip
|
|
|
97
97
|
|
|
98
98
|
## Test
|
|
99
99
|
|
|
100
|
-
- `pnpm
|
|
101
|
-
-
|
|
100
|
+
- `pnpm test` runs the package tests
|
|
101
|
+
- Linting and formatting are run from the repo root (`pnpm lint`, `pnpm format`)
|
|
102
102
|
|
|
103
103
|
|
|
104
104
|
# Copyright & License
|
package/index.js
CHANGED
package/lib/process-content.js
CHANGED
|
@@ -4,17 +4,17 @@ const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
|
|
|
4
4
|
const galleryCard = cardUtils.getCard('gallery');
|
|
5
5
|
const bookmarkCard = cardUtils.getCard('bookmark');
|
|
6
6
|
|
|
7
|
-
const doReplace =
|
|
7
|
+
const doReplace = str => {
|
|
8
8
|
const replaceParts = [
|
|
9
9
|
[/[\s\n]/g, ''], // Remove spaces
|
|
10
10
|
[/\.$/, ''], // Trim trailing full stop
|
|
11
|
-
[/\u2018/, '
|
|
12
|
-
[/\u2019/, '
|
|
11
|
+
[/\u2018/, "'"], // Convert single opening curly quote to straight quote
|
|
12
|
+
[/\u2019/, "'"], // Convert single closing curly quote to straight quote
|
|
13
13
|
[/\u201c/, '"'], // Convert double opening curly quote to straight quote
|
|
14
14
|
[/\u201d/, '"'] // Convert double closing curly quote to straight quote
|
|
15
15
|
];
|
|
16
16
|
|
|
17
|
-
replaceParts.forEach(
|
|
17
|
+
replaceParts.forEach(re => {
|
|
18
18
|
str = str.replace(re[0], re[1]);
|
|
19
19
|
});
|
|
20
20
|
|
|
@@ -28,6 +28,34 @@ const equivalentTitles = (title1, title2) => {
|
|
|
28
28
|
return title1 === title2;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
// Trim leading and trailing whitespace from an element's edge text nodes using
|
|
32
|
+
// DOM operations. This reproduces the output of the previous
|
|
33
|
+
// `serializeChildren(el).trim()` round-trip without reinterpreting serialized
|
|
34
|
+
// DOM text as HTML via innerHTML. U+00A0 (non-breaking space) is excluded so the
|
|
35
|
+
// result matches the old String.trim(): serializeChildren escaped a nbsp to
|
|
36
|
+
// ` `, which trim() never stripped.
|
|
37
|
+
const trimEdgeWhitespace = el => {
|
|
38
|
+
let node = el.firstChild;
|
|
39
|
+
while (node && node.nodeType === 3) {
|
|
40
|
+
const trimmed = node.nodeValue.replace(/^[^\S\u00a0]+/, '');
|
|
41
|
+
node.nodeValue = trimmed;
|
|
42
|
+
if (trimmed !== '') {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
node = node.nextSibling;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
node = el.lastChild;
|
|
49
|
+
while (node && node.nodeType === 3) {
|
|
50
|
+
const trimmed = node.nodeValue.replace(/[^\S\u00a0]+$/, '');
|
|
51
|
+
node.nodeValue = trimmed;
|
|
52
|
+
if (trimmed !== '') {
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
node = node.previousSibling;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
31
59
|
export default ({html, post}) => {
|
|
32
60
|
const parsed = domUtils.parseFragment(html);
|
|
33
61
|
|
|
@@ -66,14 +94,14 @@ export default ({html, post}) => {
|
|
|
66
94
|
}
|
|
67
95
|
|
|
68
96
|
// Remove the subtitle if it's the same as the excerpt
|
|
69
|
-
parsed.$('.graf--subtitle').forEach(
|
|
97
|
+
parsed.$('.graf--subtitle').forEach(el => {
|
|
70
98
|
if (equivalentTitles(el.textContent, post.data.custom_excerpt)) {
|
|
71
99
|
el.remove();
|
|
72
100
|
}
|
|
73
101
|
});
|
|
74
102
|
|
|
75
103
|
// Convert galleries
|
|
76
|
-
parsed.$('.section-inner.sectionLayout--outsetRow').forEach(
|
|
104
|
+
parsed.$('.section-inner.sectionLayout--outsetRow').forEach(el => {
|
|
77
105
|
// Check we're only working with a group of <figure> elements
|
|
78
106
|
const children = Array.from(el.children);
|
|
79
107
|
const allFigures = children.every(child => child.tagName.toLowerCase() === 'figure');
|
|
@@ -91,7 +119,8 @@ export default ({html, post}) => {
|
|
|
91
119
|
}
|
|
92
120
|
};
|
|
93
121
|
|
|
94
|
-
el.querySelectorAll('figure').forEach(
|
|
122
|
+
el.querySelectorAll('figure').forEach(figure => {
|
|
123
|
+
// eslint-disable-line no-shadow
|
|
95
124
|
let img = figure.querySelector('img');
|
|
96
125
|
cardOpts.payload.images.push({
|
|
97
126
|
row: 0,
|
|
@@ -109,7 +138,7 @@ export default ({html, post}) => {
|
|
|
109
138
|
});
|
|
110
139
|
|
|
111
140
|
// Convert Medium bookmarks
|
|
112
|
-
parsed.$('.graf--mixtapeEmbed').forEach(
|
|
141
|
+
parsed.$('.graf--mixtapeEmbed').forEach(el => {
|
|
113
142
|
let cardOpts = {
|
|
114
143
|
env: {dom: new SimpleDom.Document()},
|
|
115
144
|
payload: {}
|
|
@@ -137,12 +166,12 @@ export default ({html, post}) => {
|
|
|
137
166
|
});
|
|
138
167
|
|
|
139
168
|
// Handle blockquotes made of 2 elements
|
|
140
|
-
parsed.$('blockquote.graf--pullquote, blockquote.graf--blockquote').forEach(
|
|
169
|
+
parsed.$('blockquote.graf--pullquote, blockquote.graf--blockquote').forEach(bq => {
|
|
141
170
|
bq.removeAttribute('name');
|
|
142
171
|
bq.removeAttribute('id');
|
|
143
172
|
bq.removeAttribute('class');
|
|
144
173
|
|
|
145
|
-
bq.querySelectorAll('a').forEach(
|
|
174
|
+
bq.querySelectorAll('a').forEach(a => {
|
|
146
175
|
a.removeAttribute('data-href');
|
|
147
176
|
a.removeAttribute('class');
|
|
148
177
|
});
|
|
@@ -162,24 +191,24 @@ export default ({html, post}) => {
|
|
|
162
191
|
domUtils.replaceWith(bq, `<blockquote><p>${textElements.join('<br><br>')}</p></blockquote>`);
|
|
163
192
|
});
|
|
164
193
|
|
|
165
|
-
parsed.$('pre.graf--pre').forEach(
|
|
194
|
+
parsed.$('pre.graf--pre').forEach(pre => {
|
|
166
195
|
pre.removeAttribute('name');
|
|
167
196
|
pre.removeAttribute('id');
|
|
168
197
|
pre.removeAttribute('class');
|
|
169
198
|
pre.removeAttribute('data-code-block-mode');
|
|
170
199
|
pre.removeAttribute('spellcheck');
|
|
171
|
-
|
|
200
|
+
trimEdgeWhitespace(pre);
|
|
172
201
|
|
|
173
202
|
const lang = pre.getAttribute('data-code-block-lang');
|
|
174
203
|
pre.removeAttribute('data-code-block-lang');
|
|
175
204
|
|
|
176
|
-
pre.querySelectorAll('span.pre--content').forEach(
|
|
205
|
+
pre.querySelectorAll('span.pre--content').forEach(span => {
|
|
177
206
|
const code = parsed.document.createElement('code');
|
|
178
207
|
code.innerHTML = span.innerHTML;
|
|
179
208
|
span.parentNode.replaceChild(code, span);
|
|
180
209
|
});
|
|
181
210
|
|
|
182
|
-
pre.querySelectorAll('code').forEach(
|
|
211
|
+
pre.querySelectorAll('code').forEach(code => {
|
|
183
212
|
code.removeAttribute('name');
|
|
184
213
|
code.removeAttribute('id');
|
|
185
214
|
code.removeAttribute('class');
|
|
@@ -188,7 +217,8 @@ export default ({html, post}) => {
|
|
|
188
217
|
code.classList.add(`language-${lang}`);
|
|
189
218
|
}
|
|
190
219
|
|
|
191
|
-
code.
|
|
220
|
+
code.querySelectorAll('br').forEach(br => br.replaceWith(' \n'));
|
|
221
|
+
trimEdgeWhitespace(code);
|
|
192
222
|
});
|
|
193
223
|
});
|
|
194
224
|
|
package/lib/process-post.js
CHANGED
|
@@ -62,15 +62,13 @@ const processAuthor = ({pAuthor}) => {
|
|
|
62
62
|
data: {
|
|
63
63
|
name: pAuthor.textContent,
|
|
64
64
|
slug: href.replace(/.*?@(.*?)$/, (m, p) => p.toLowerCase()),
|
|
65
|
-
roles: [
|
|
66
|
-
'Contributor'
|
|
67
|
-
]
|
|
65
|
+
roles: ['Contributor']
|
|
68
66
|
}
|
|
69
67
|
};
|
|
70
68
|
};
|
|
71
69
|
|
|
72
70
|
const processTags = ({tagLinks}) => {
|
|
73
|
-
return tagLinks.map(
|
|
71
|
+
return tagLinks.map(tag => {
|
|
74
72
|
const href = tag.getAttribute('href');
|
|
75
73
|
return {
|
|
76
74
|
url: href,
|
|
@@ -83,7 +81,7 @@ const processTags = ({tagLinks}) => {
|
|
|
83
81
|
};
|
|
84
82
|
|
|
85
83
|
const processFeatureImage = ({html, post, options}) => {
|
|
86
|
-
return domUtils.processFragment(html,
|
|
84
|
+
return domUtils.processFragment(html, parsed => {
|
|
87
85
|
// Look for data-is-featured
|
|
88
86
|
let featured = parsed.$('[data-is-featured]')[0] || null;
|
|
89
87
|
|
|
@@ -92,7 +90,7 @@ const processFeatureImage = ({html, post, options}) => {
|
|
|
92
90
|
let foundImg = false;
|
|
93
91
|
let preImageTags = [];
|
|
94
92
|
|
|
95
|
-
allSections.forEach(
|
|
93
|
+
allSections.forEach(el => {
|
|
96
94
|
if (!foundImg) {
|
|
97
95
|
preImageTags.push(el.tagName.toLowerCase());
|
|
98
96
|
}
|
|
@@ -133,7 +131,7 @@ const processFeatureImage = ({html, post, options}) => {
|
|
|
133
131
|
};
|
|
134
132
|
|
|
135
133
|
export default ({name, html, globalUser, options}) => {
|
|
136
|
-
return domUtils.processFragment(html,
|
|
134
|
+
return domUtils.processFragment(html, parsed => {
|
|
137
135
|
let post = processMeta({name, parsed, options});
|
|
138
136
|
|
|
139
137
|
// Process author
|
package/lib/process-profile.js
CHANGED
|
@@ -23,19 +23,17 @@ const mediumToGhost = {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
export default ({html}) => {
|
|
26
|
-
return processFragment(html,
|
|
26
|
+
return processFragment(html, parsed => {
|
|
27
27
|
let profile = {
|
|
28
28
|
url: parsed.$('.u-url')[0]?.getAttribute('href'),
|
|
29
29
|
data: {
|
|
30
30
|
name: parsed.$('.p-name')[0]?.textContent || '',
|
|
31
31
|
profile_image: parsed.$('.u-photo')[0]?.getAttribute('src'),
|
|
32
|
-
roles: [
|
|
33
|
-
'Contributor'
|
|
34
|
-
]
|
|
32
|
+
roles: ['Contributor']
|
|
35
33
|
}
|
|
36
34
|
};
|
|
37
35
|
|
|
38
|
-
parsed.$('ul li').forEach(
|
|
36
|
+
parsed.$('ul li').forEach(el => {
|
|
39
37
|
let [item, value] = el.textContent.split(': ');
|
|
40
38
|
let key = mediumToGhost[item.toLowerCase()] || null;
|
|
41
39
|
|
package/lib/read-zip.js
CHANGED
|
@@ -5,10 +5,10 @@ import {_base as debugFactory} from '@tryghost/debug';
|
|
|
5
5
|
|
|
6
6
|
const debug = debugFactory('migrate:medium:lib:read-zip');
|
|
7
7
|
|
|
8
|
-
const contentStats = async
|
|
8
|
+
const contentStats = async zipPath => {
|
|
9
9
|
const entries = await fsUtils.readZipEntries(zipPath);
|
|
10
10
|
const posts = entries.filter(value => /^posts\/.*\.html$/.test(value)).length;
|
|
11
|
-
const users = entries.filter(value =>
|
|
11
|
+
const users = entries.filter(value => value.startsWith('profile/profile.html')).length;
|
|
12
12
|
|
|
13
13
|
return {
|
|
14
14
|
posts: posts,
|
|
@@ -21,16 +21,16 @@ const readMediumZip = ({content, zipPath, options, skippedFileCount}) => {
|
|
|
21
21
|
const entryHtml = zipEntry.getData().toString('utf8');
|
|
22
22
|
|
|
23
23
|
// Catch all HTML files inside `profile/`
|
|
24
|
-
if (
|
|
24
|
+
if (entryName.startsWith('profile/profile.html')) {
|
|
25
25
|
content.profiles.push({data: entryHtml});
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
// Catch all HTML files in `posts/`
|
|
28
28
|
} else if (/^posts\/.*\.html$/.test(entryName)) {
|
|
29
29
|
content.posts.push({
|
|
30
30
|
name: entryName,
|
|
31
31
|
html: entryHtml
|
|
32
32
|
});
|
|
33
|
-
|
|
33
|
+
// Skip if not matched above, and report skipped files if `--verbose`
|
|
34
34
|
} else if (/.html$/.test(entryName)) {
|
|
35
35
|
if (options.verbose) {
|
|
36
36
|
debug(`Skipped: ${entryName}`);
|
|
@@ -63,7 +63,7 @@ export default (zipPath, options) => {
|
|
|
63
63
|
const ifIsDirectory = lstatSync(zipPath).isDirectory();
|
|
64
64
|
|
|
65
65
|
if (ifIsDirectory) {
|
|
66
|
-
readdirSync(zipPath).forEach(
|
|
66
|
+
readdirSync(zipPath).forEach(file => {
|
|
67
67
|
const filePath = join(zipPath, file);
|
|
68
68
|
const zipContent = readMediumZip({content, zipPath: filePath, options, skippedFileCount});
|
|
69
69
|
content = zipContent.content;
|
|
@@ -80,6 +80,4 @@ export default (zipPath, options) => {
|
|
|
80
80
|
return content;
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
-
export {
|
|
84
|
-
contentStats
|
|
85
|
-
};
|
|
83
|
+
export {contentStats};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-medium-export",
|
|
3
|
-
"version": "0.27.
|
|
3
|
+
"version": "0.27.4",
|
|
4
4
|
"description": "tbc",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,20 +19,17 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"c8": "
|
|
23
|
-
"eslint": "8.57.1"
|
|
22
|
+
"c8": "12.0.0"
|
|
24
23
|
},
|
|
25
24
|
"dependencies": {
|
|
26
|
-
"@tryghost/debug": "2.
|
|
25
|
+
"@tryghost/debug": "2.3.6",
|
|
27
26
|
"@tryghost/string": "0.3.5",
|
|
28
27
|
"simple-dom": "1.4.0",
|
|
29
|
-
"@tryghost/mg-fs-utils": "0.26.
|
|
30
|
-
"@tryghost/mg-utils": "0.11.
|
|
28
|
+
"@tryghost/mg-fs-utils": "0.26.4",
|
|
29
|
+
"@tryghost/mg-utils": "0.11.4"
|
|
31
30
|
},
|
|
32
31
|
"scripts": {
|
|
33
32
|
"dev": "echo \"Implement me!\"",
|
|
34
|
-
"test": "c8 --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node --test 'test/**/*.test.js'"
|
|
35
|
-
"posttest": "pnpm lint",
|
|
36
|
-
"lint": "eslint . --ext .js --cache"
|
|
33
|
+
"test": "c8 --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node --test 'test/**/*.test.js'"
|
|
37
34
|
}
|
|
38
35
|
}
|