@tryghost/mg-medium-export 0.17.28 → 0.18.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 CHANGED
@@ -79,7 +79,7 @@ This will process all posts from the zip file, and all authors will have an emai
79
79
 
80
80
  ## Develop
81
81
 
82
- This is a mono repository, managed with [lerna](https://lerna.js.org).
82
+ This is a mono repository, managed with [Nx](https://nx.dev/) and yarn workspaces.
83
83
 
84
84
  Follow the instructions for the top-level repo.
85
85
  1. `git clone` this repo & `cd` into it as usual
@@ -1,4 +1,4 @@
1
- import * as cheerio from 'cheerio';
1
+ import {domUtils} from '@tryghost/mg-utils';
2
2
  import SimpleDom from 'simple-dom';
3
3
  import galleryCard from '@tryghost/kg-default-cards/lib/cards/gallery.js';
4
4
  import bookmarkCard from '@tryghost/kg-default-cards/lib/cards/bookmark.js';
@@ -8,10 +8,10 @@ const doReplace = (str) => {
8
8
  const replaceParts = [
9
9
  [/[\s\n]/g, ''], // Remove spaces
10
10
  [/\.$/, ''], // Trim trailing full stop
11
- [/‘/, '\''], // Convert single opening curly quote to straight quote
12
- [/’/, '\''], // Convert single closing curly quote to straight quote
13
- [/“/, '"'], // Convert double opening curly quote to straight quote
14
- [/”/, '"'] // Convert double closing curly quote to straight quote
11
+ [/\u2018/, '\''], // Convert single opening curly quote to straight quote
12
+ [/\u2019/, '\''], // Convert single closing curly quote to straight quote
13
+ [/\u201c/, '"'], // Convert double opening curly quote to straight quote
14
+ [/\u201d/, '"'] // Convert double closing curly quote to straight quote
15
15
  ];
16
16
 
17
17
  replaceParts.forEach((re) => {
@@ -28,17 +28,14 @@ const equivalentTitles = (title1, title2) => {
28
28
  return title1 === title2;
29
29
  };
30
30
 
31
- export default ({content, post}) => {
32
- // Load the content HTML to get a Cheerio instance we can use
33
- // Uses default cheerio options (parse5) to decode entities and preserve empty attributes
34
- const $ = cheerio.load(content.html(), {}, false);
35
- let $content = $.root();
31
+ export default ({html, post}) => {
32
+ const parsed = domUtils.parseFragment(html);
36
33
 
37
34
  // Detect if post is likely to be a comment. If so, set as draft and add tag
38
35
  // Based on being published, content having 1 paragraph, and having no images
39
- let childrenCount = $content.children().length;
40
- let pCount = $content.find('p').length;
41
- let imgCount = $content.find('img').length;
36
+ let childrenCount = parsed.body.children.length;
37
+ let pCount = parsed.$('p').length;
38
+ let imgCount = parsed.$('img').length;
42
39
 
43
40
  if (post.data.status === 'published' && childrenCount <= 1 && pCount <= 1 && imgCount === 0) {
44
41
  post.data.status = 'draft';
@@ -53,38 +50,38 @@ export default ({content, post}) => {
53
50
 
54
51
  // Medium usually has an hr divider at the very beginning of the content
55
52
  // We don't need this so remove it if we find it
56
- let $firstSection = $content.find('.section--first');
57
- if ($firstSection.children().first().hasClass('section-divider')) {
58
- $firstSection.children().first().remove();
53
+ let firstSection = parsed.$('.section--first')[0];
54
+ if (firstSection) {
55
+ let firstChild = firstSection.firstElementChild;
56
+ if (firstChild && firstChild.classList.contains('section-divider')) {
57
+ firstChild.remove();
58
+ }
59
59
  }
60
60
 
61
61
  // Sometimes Medium has a duplicate header at the beginning of the content
62
62
  // Don't need this either so remove it
63
- let firstTitle = $content.find('h1, h2, h3, h4, blockquote').first();
64
- if (equivalentTitles(firstTitle.text(), post.data.title)) {
65
- $content.find(firstTitle).remove();
63
+ let firstTitle = parsed.$('h1, h2, h3, h4, blockquote')[0];
64
+ if (firstTitle && equivalentTitles(firstTitle.textContent, post.data.title)) {
65
+ firstTitle.remove();
66
66
  }
67
67
 
68
68
  // Remove the subtitle if it's the same as the excerpt
69
- $content.find('.graf--subtitle').each((i, el) => {
70
- if (equivalentTitles($(el).text(), post.data.custom_excerpt)) {
71
- $(el).remove();
69
+ parsed.$('.graf--subtitle').forEach((el) => {
70
+ if (equivalentTitles(el.textContent, post.data.custom_excerpt)) {
71
+ el.remove();
72
72
  }
73
73
  });
74
74
 
75
75
  // Convert galleries
76
- $content.find('.section-inner.sectionLayout--outsetRow').each((i, el) => {
76
+ parsed.$('.section-inner.sectionLayout--outsetRow').forEach((el) => {
77
77
  // Check we're only working with a group of <figure> elements
78
- const children = $(el).children();
79
- const childElementTags = children.map((ii, ell) => {
80
- return ell.tagName.toLowerCase();
81
- }).get();
82
- const allFigures = childElementTags.every(e => e === 'figure');
78
+ const children = Array.from(el.children);
79
+ const allFigures = children.every(child => child.tagName.toLowerCase() === 'figure');
83
80
 
84
81
  // If it's all figures
85
82
  if (allFigures) {
86
- let hasCaption = $(el).find('figcaption');
87
- let caption = hasCaption.length > 0 ? hasCaption.html().trim() : null;
83
+ let figcaption = el.querySelector('figcaption');
84
+ let caption = figcaption ? domUtils.serializeChildren(figcaption).trim() : null;
88
85
 
89
86
  let cardOpts = {
90
87
  env: {dom: new SimpleDom.Document()},
@@ -94,105 +91,108 @@ export default ({content, post}) => {
94
91
  }
95
92
  };
96
93
 
97
- $(el).find('figure').each((iii, elll) => { // eslint-disable-line no-shadow
98
- let img = $(elll).find('img');
94
+ el.querySelectorAll('figure').forEach((figure) => { // eslint-disable-line no-shadow
95
+ let img = figure.querySelector('img');
99
96
  cardOpts.payload.images.push({
100
97
  row: 0,
101
- fileName: img.attr('data-image-id'),
102
- src: img.attr('src'),
103
- width: img.attr('data-width'),
104
- height: img.attr('data-height')
98
+ fileName: img.getAttribute('data-image-id'),
99
+ src: img.getAttribute('src'),
100
+ width: img.getAttribute('data-width'),
101
+ height: img.getAttribute('data-height')
105
102
  });
106
103
  });
107
104
 
108
105
  const galleryHtml = serializer.serialize(galleryCard.render(cardOpts));
109
106
 
110
- $(el).replaceWith(galleryHtml);
107
+ domUtils.replaceWith(el, galleryHtml);
111
108
  }
112
109
  });
113
110
 
114
111
  // Convert Medium bookmarks
115
- $content.find('.graf--mixtapeEmbed').each((i, el) => {
112
+ parsed.$('.graf--mixtapeEmbed').forEach((el) => {
116
113
  let cardOpts = {
117
114
  env: {dom: new SimpleDom.Document()},
118
115
  payload: {}
119
116
  };
120
117
 
121
- let href = $(el).find('.markup--anchor').attr('href');
118
+ let href = el.querySelector('.markup--anchor')?.getAttribute('href');
122
119
  cardOpts.payload.url = href;
123
120
 
124
121
  cardOpts.payload.metadata = {
125
122
  url: href,
126
- title: $(el).find('.markup--strong').text(),
127
- description: $(el).find('.markup--em').text()
123
+ title: el.querySelector('.markup--strong')?.textContent || '',
124
+ description: el.querySelector('.markup--em')?.textContent || ''
128
125
  };
129
126
 
130
- const bngImage = $(el).find('.js-mixtapeImage').css('background-image');
127
+ const mixtapeImage = el.querySelector('.js-mixtapeImage');
128
+ const bngImage = mixtapeImage ? mixtapeImage.style.backgroundImage : null;
131
129
  if (bngImage) {
132
- let src = bngImage.replace(/url\((.*?)\)/, (m, p) => p);
130
+ let src = bngImage.replace(/url\(["']?(.*?)["']?\)/, (m, p) => p);
133
131
  cardOpts.payload.metadata.thumbnail = src;
134
132
  }
135
133
 
136
134
  const bookmarkHtml = serializer.serialize(bookmarkCard.render(cardOpts));
137
135
 
138
- $(el).replaceWith(`<!--kg-card-begin: html-->\n${bookmarkHtml}\n<!--kg-card-end: html-->`);
136
+ domUtils.replaceWith(el, `<!--kg-card-begin: html-->\n${bookmarkHtml}\n<!--kg-card-end: html-->`);
139
137
  });
140
138
 
141
139
  // Handle blockquotes made of 2 elements
142
- $content.find('blockquote.graf--pullquote, blockquote.graf--blockquote').each((i, bq) => {
143
- $(bq).removeAttr('name');
144
- $(bq).removeAttr('id');
145
- $(bq).removeAttr('class');
146
-
147
- $(bq).find('a').each((ii, a) => {
148
- $(a).removeAttr('data-href');
149
- $(a).removeAttr('class');
140
+ parsed.$('blockquote.graf--pullquote, blockquote.graf--blockquote').forEach((bq) => {
141
+ bq.removeAttribute('name');
142
+ bq.removeAttribute('id');
143
+ bq.removeAttribute('class');
144
+
145
+ bq.querySelectorAll('a').forEach((a) => {
146
+ a.removeAttribute('data-href');
147
+ a.removeAttribute('class');
150
148
  });
151
149
 
152
150
  let textElements = [];
153
- textElements.push($(bq).html().trim());
151
+ textElements.push(domUtils.serializeChildren(bq).trim());
154
152
 
155
- if ($(bq).next('.graf-after--pullquote')) {
156
- const nextElem = $(bq).next('.graf-after--pullquote');
157
- const nextText = $(nextElem).html();
153
+ const nextElem = bq.nextElementSibling;
154
+ if (nextElem && nextElem.matches('.graf-after--pullquote')) {
155
+ const nextText = domUtils.serializeChildren(nextElem);
158
156
  if (nextText && nextText.length > 0) {
159
157
  textElements.push(nextText.trim());
160
- $(nextElem).remove();
158
+ nextElem.remove();
161
159
  }
162
160
  }
163
161
 
164
- $(bq).replaceWith(`<blockquote><p>${textElements.join('<br><br>')}</p></blockquote>`);
162
+ domUtils.replaceWith(bq, `<blockquote><p>${textElements.join('<br><br>')}</p></blockquote>`);
165
163
  });
166
164
 
167
- $content.find('pre.graf--pre').each((i, pre) => {
168
- $(pre).removeAttr('name');
169
- $(pre).removeAttr('id');
170
- $(pre).removeAttr('class');
171
- $(pre).removeAttr('data-code-block-mode');
172
- $(pre).removeAttr('spellcheck');
173
- $(pre).html($(pre).html().trim());
174
-
175
- const lang = $(pre).attr('data-code-block-lang');
176
- $(pre).removeAttr('data-code-block-lang');
177
-
178
- $(pre).find('span.pre--content').each((ii, span) => {
179
- span.name = 'code';
165
+ parsed.$('pre.graf--pre').forEach((pre) => {
166
+ pre.removeAttribute('name');
167
+ pre.removeAttribute('id');
168
+ pre.removeAttribute('class');
169
+ pre.removeAttribute('data-code-block-mode');
170
+ pre.removeAttribute('spellcheck');
171
+ pre.innerHTML = domUtils.serializeChildren(pre).trim();
172
+
173
+ const lang = pre.getAttribute('data-code-block-lang');
174
+ pre.removeAttribute('data-code-block-lang');
175
+
176
+ pre.querySelectorAll('span.pre--content').forEach((span) => {
177
+ const code = parsed.document.createElement('code');
178
+ code.innerHTML = span.innerHTML;
179
+ span.parentNode.replaceChild(code, span);
180
180
  });
181
181
 
182
- $(pre).find('code').each((iii, code) => {
183
- $(code).removeAttr('name');
184
- $(code).removeAttr('id');
185
- $(code).removeAttr('class');
182
+ pre.querySelectorAll('code').forEach((code) => {
183
+ code.removeAttribute('name');
184
+ code.removeAttribute('id');
185
+ code.removeAttribute('class');
186
186
 
187
187
  if (lang && lang.length > 0) {
188
- $(code).addClass(`language-${lang}`);
188
+ code.classList.add(`language-${lang}`);
189
189
  }
190
190
 
191
- $(code).html($(code).html().replace(/<br>/g, ' \n').trim());
191
+ code.innerHTML = domUtils.serializeChildren(code).replace(/<br>/g, ' \n').trim();
192
192
  });
193
193
  });
194
194
 
195
- post.data.html = $content.html().trim();
195
+ post.data.html = parsed.html().trim();
196
196
 
197
197
  return post;
198
198
  };
@@ -1,10 +1,10 @@
1
- import * as cheerio from 'cheerio';
1
+ import {domUtils} from '@tryghost/mg-utils';
2
2
  import string from '@tryghost/string';
3
3
  import processContent from './process-content.js';
4
4
 
5
5
  const sectionTags = ['aside', 'blockquote', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'img'];
6
6
 
7
- const processMeta = ({name, $post, options}) => {
7
+ const processMeta = ({name, parsed, options}) => {
8
8
  const mediumAsCanonical = options?.mediumAsCanonical ?? false;
9
9
 
10
10
  let urlInfo;
@@ -12,16 +12,17 @@ const processMeta = ({name, $post, options}) => {
12
12
  const dateNow = new Date();
13
13
 
14
14
  const post = {
15
- url: $post('.p-canonical').attr('href'),
15
+ url: parsed.$('.p-canonical')[0]?.getAttribute('href'),
16
16
  data: {
17
- title: $post('.p-name').text(),
18
- custom_excerpt: $post('.p-summary').text().trim()
17
+ title: parsed.$('.p-name')[0]?.textContent || '',
18
+ custom_excerpt: (parsed.$('.p-summary')[0]?.textContent || '').trim()
19
19
  }
20
20
  };
21
21
 
22
22
  if (/^(?:posts\/)?draft/.test(name)) {
23
23
  urlInfo = name.match(/_(.*?)-([0-9a-f]+)\.html/);
24
- post.url = $post('footer p a').attr('href');
24
+ const footerLink = parsed.$('footer p a')[0];
25
+ post.url = footerLink?.getAttribute('href');
25
26
  post.data.status = 'draft';
26
27
  post.data.created_at = dateNow;
27
28
  post.data.updated_at = dateNow;
@@ -35,23 +36,17 @@ const processMeta = ({name, $post, options}) => {
35
36
  }
36
37
 
37
38
  post.data.status = 'published';
38
- post.data.created_at = $post('.dt-published').attr('datetime') || dateNow;
39
- post.data.published_at = $post('.dt-published').attr('datetime') || dateNow;
40
- post.data.updated_at = $post('.dt-published').attr('datetime') || dateNow;
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;
41
43
  }
42
44
 
43
45
  if (mediumAsCanonical) {
44
- const canonicalUrl = $post('.p-canonical').attr('href');
46
+ const canonicalUrl = parsed.$('.p-canonical')[0]?.getAttribute('href');
45
47
  post.data.canonical_url = canonicalUrl;
46
48
  }
47
49
 
48
- // $('img').map(async (i, el) => {
49
- // let $image = $(el);
50
- // let type = $image.attr('src') === undefined ? 'data-src' : 'src';
51
- // let newSrc = await this.downloadImage($image.attr(type));
52
- // $image.attr(type, newSrc);
53
- // }).get();
54
-
55
50
  // Ensure the slug is clean and valid according to Ghost
56
51
  post.data.slug = string.slugify(urlInfo[1]);
57
52
  // Store the medium ID in the comment ID legacy field - not sure if this is useful
@@ -60,12 +55,13 @@ const processMeta = ({name, $post, options}) => {
60
55
  return post;
61
56
  };
62
57
 
63
- const processAuthor = ({$author}) => {
58
+ const processAuthor = ({pAuthor}) => {
59
+ const href = pAuthor.getAttribute('href');
64
60
  return {
65
- url: $author.attr('href'),
61
+ url: href,
66
62
  data: {
67
- name: $author.text(),
68
- slug: $author.attr('href').replace(/.*?@(.*?)$/, (m, p) => p.toLowerCase()),
63
+ name: pAuthor.textContent,
64
+ slug: href.replace(/.*?@(.*?)$/, (m, p) => p.toLowerCase()),
69
65
  roles: [
70
66
  'Contributor'
71
67
  ]
@@ -73,123 +69,116 @@ const processAuthor = ({$author}) => {
73
69
  };
74
70
  };
75
71
 
76
- const processTags = ({$tags}) => {
77
- const tags = [];
78
- $tags.each((i) => {
79
- let $tag = $tags.eq(i);
80
- tags.push({
81
- url: $tag.attr('href'),
72
+ const processTags = ({tagLinks}) => {
73
+ return tagLinks.map((tag) => {
74
+ const href = tag.getAttribute('href');
75
+ return {
76
+ url: href,
82
77
  data: {
83
- name: $tag.text(),
84
- slug: $tag.attr('href').replace(/.*\/(.*?)$/, (m, p) => p.toLowerCase())
78
+ name: tag.textContent,
79
+ slug: href.replace(/.*\/(.*?)$/, (m, p) => p.toLowerCase())
85
80
  }
86
- });
81
+ };
87
82
  });
88
- return tags;
89
83
  };
90
84
 
91
85
  const processFeatureImage = ({html, post, options}) => {
92
- const $html = cheerio.load(html, {
93
- xml: {
94
- xmlMode: false,
95
- decodeEntities: false
96
- }
97
- }, false);
98
-
99
- // Look for data-is-featured
100
- let featured = $html('[data-is-featured]')[0];
86
+ return domUtils.processFragment(html, (parsed) => {
87
+ // Look for data-is-featured
88
+ let featured = parsed.$('[data-is-featured]')[0] || null;
89
+
90
+ // Look for an image that appears before content
91
+ let allSections = parsed.$(sectionTags.join(','));
92
+ let foundImg = false;
93
+ let preImageTags = [];
94
+
95
+ allSections.forEach((el) => {
96
+ if (!foundImg) {
97
+ preImageTags.push(el.tagName.toLowerCase());
98
+ }
101
99
 
102
- // Look for an image that appears before content
103
- let allSections = $html(sectionTags.join(','));
104
- let foundImg = false;
105
- let preImageTags = [];
100
+ if (!foundImg && el.tagName.toLowerCase() === 'img') {
101
+ foundImg = el;
102
+ }
103
+ });
106
104
 
107
- allSections.each((i, el) => {
108
- if (!foundImg) {
109
- preImageTags.push(el.tagName);
105
+ // We don't have a designated feature image, but there's an image above the content so use that image instead
106
+ if (!featured && !preImageTags.includes('p')) {
107
+ featured = foundImg;
108
+
109
+ if (options?.addPlatformTag) {
110
+ // tag it with #auto-feature-image so we can tell the difference
111
+ post.data.tags.push({
112
+ data: {
113
+ name: '#auto-feature-image'
114
+ }
115
+ });
116
+ }
110
117
  }
111
118
 
112
- if (!foundImg && el.tagName === 'img') {
113
- return foundImg = el;
119
+ if (featured) {
120
+ post.data.feature_image = featured.getAttribute('src');
121
+ post.data.feature_image_alt = featured.getAttribute('alt') || null;
122
+ const figure = featured.closest('figure');
123
+ const figcaption = figure ? figure.querySelector('figcaption') : null;
124
+ post.data.feature_image_caption = figcaption ? domUtils.serializeChildren(figcaption).trim() : null;
125
+
126
+ if (figure) {
127
+ figure.remove();
128
+ }
114
129
  }
130
+
131
+ return parsed.html().trim();
115
132
  });
133
+ };
116
134
 
117
- // We don't have a designated feature image, but there's an image above the content so use that image instead
118
- if (!featured && !preImageTags.includes('p')) {
119
- featured = foundImg;
135
+ export default ({name, html, globalUser, options}) => {
136
+ return domUtils.processFragment(html, (parsed) => {
137
+ let post = processMeta({name, parsed, options});
138
+
139
+ // Process author
140
+ const pAuthor = parsed.$('.p-author')[0];
141
+ if (pAuthor) {
142
+ post.data.author = processAuthor({pAuthor});
143
+ } else if (globalUser) {
144
+ post.data.author = globalUser;
145
+ }
120
146
 
121
- if (options?.addPlatformTag) {
122
- // tag it with #auto-feature-image so we can tell the difference
147
+ post.data.tags = [];
148
+
149
+ if (options?.addTag) {
123
150
  post.data.tags.push({
151
+ url: 'migrator-added-tag',
124
152
  data: {
125
- name: '#auto-feature-image'
153
+ name: options.addTag
126
154
  }
127
155
  });
128
156
  }
129
- }
130
-
131
- if (featured) {
132
- post.data.feature_image = $html(featured).attr('src');
133
- post.data.feature_image_alt = $html(featured).attr('alt') || null;
134
- post.data.feature_image_caption = $html(featured).parents('figure').find('figcaption').html() || null;
135
157
 
136
- $html(featured).parents('figure').remove();
137
- }
138
-
139
- return $html.html().trim();
140
- };
141
-
142
- export default ({name, html, globalUser, options}) => {
143
- const $post = cheerio.load(html, {
144
- xml: {
145
- xmlMode: false,
146
- decodeEntities: false,
147
- scriptingEnabled: false
158
+ // Process tags
159
+ const tagLinks = parsed.$('.p-tags a');
160
+ if (tagLinks.length) {
161
+ post.data.tags = [...post.data.tags, ...processTags({tagLinks})];
148
162
  }
149
- }, false); // This `false` is `isDocument`. If `true`, <html>, <head>, and <body> elements are introduced
150
163
 
151
- let post = processMeta({name, $post, options});
152
-
153
- // Process author
154
- if ($post('.p-author').length) {
155
- post.data.author = processAuthor({$author: $post('.p-author')});
156
- // @TODO check if this is the global user and use that?
157
- } else if (globalUser) {
158
- post.data.author = globalUser;
159
- }
160
-
161
- post.data.tags = [];
162
-
163
- if (options?.addTag) {
164
- post.data.tags.push({
165
- url: 'migrator-added-tag',
166
- data: {
167
- name: options.addTag
168
- }
169
- });
170
- }
171
-
172
- // Process tags
173
- if ($post('.p-tags a').length) {
174
- post.data.tags = [...post.data.tags, ...processTags({$tags: $post('.p-tags a')})];
175
- }
176
-
177
- if (options?.addPlatformTag) {
178
- post.data.tags.push({
179
- url: 'migrator-added-platform-tag',
180
- data: {
181
- name: '#medium'
182
- }
183
- });
184
- }
164
+ if (options?.addPlatformTag) {
165
+ post.data.tags.push({
166
+ url: 'migrator-added-platform-tag',
167
+ data: {
168
+ name: '#medium'
169
+ }
170
+ });
171
+ }
185
172
 
186
- // Process content
187
- // post.data.html = processContent({content: $post('.e-content'), post});
188
- post = processContent({content: $post('.e-content'), post});
173
+ // Process content
174
+ const eContent = parsed.$('.e-content')[0];
175
+ const contentHtml = eContent ? domUtils.serializeChildren(eContent) : '';
176
+ post = processContent({html: contentHtml, post});
189
177
 
190
- // Grab the featured image
191
- // Do this last so that we can add tags to indicate feature image style
192
- post.data.html = processFeatureImage({html: post.data.html, post, options});
178
+ // Grab the featured image
179
+ // Do this last so that we can add tags to indicate feature image style
180
+ post.data.html = processFeatureImage({html: post.data.html, post, options});
193
181
 
194
- return post;
182
+ return post;
183
+ });
195
184
  };
@@ -1,4 +1,5 @@
1
- import * as cheerio from 'cheerio';
1
+ import {domUtils} from '@tryghost/mg-utils';
2
+ const {processFragment} = domUtils;
2
3
 
3
4
  // Keys we've seen so far
4
5
  // Profile
@@ -22,26 +23,27 @@ const mediumToGhost = {
22
23
  };
23
24
 
24
25
  export default ({html}) => {
25
- let $ = cheerio.load(html);
26
- let profile = {
27
- url: $('.u-url').attr('href'),
28
- data: {
29
- name: $('.p-name').text(),
30
- profile_image: $('.u-photo').attr('src'),
31
- roles: [
32
- 'Contributor'
33
- ]
34
- }
35
- };
26
+ return processFragment(html, (parsed) => {
27
+ let profile = {
28
+ url: parsed.$('.u-url')[0]?.getAttribute('href'),
29
+ data: {
30
+ name: parsed.$('.p-name')[0]?.textContent || '',
31
+ profile_image: parsed.$('.u-photo')[0]?.getAttribute('src'),
32
+ roles: [
33
+ 'Contributor'
34
+ ]
35
+ }
36
+ };
36
37
 
37
- $('ul li').each((i, el) => {
38
- let [item, value] = $(el).text().split(': ');
39
- let key = mediumToGhost[item.toLowerCase()] || null;
38
+ parsed.$('ul li').forEach((el) => {
39
+ let [item, value] = el.textContent.split(': ');
40
+ let key = mediumToGhost[item.toLowerCase()] || null;
40
41
 
41
- if (key) {
42
- profile.data[key] = value;
43
- }
44
- });
42
+ if (key) {
43
+ profile.data[key] = value;
44
+ }
45
+ });
45
46
 
46
- return profile;
47
+ return profile;
48
+ });
47
49
  };
package/lib/process.js CHANGED
@@ -11,7 +11,13 @@ export default (input, options) => {
11
11
  let globalUser = output.users && output.users.length === 1 ? output.users[0] : null;
12
12
 
13
13
  if (input.posts && input.posts.length > 0) {
14
- output.posts = input.posts.map(post => processPost({name: post.name, html: post.html, globalUser, options}));
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
+ }
15
21
  }
16
22
 
17
23
  return output;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-medium-export",
3
- "version": "0.17.28",
3
+ "version": "0.18.0",
4
4
  "description": "tbc",
5
5
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-medium-export",
6
6
  "author": "Ghost Foundation",
@@ -9,9 +9,9 @@
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "dev": "echo \"Implement me!\"",
12
- "test": "node --test 'test/**/*.test.js'",
13
- "lint": "eslint . --ext .js --cache",
14
- "posttest": "yarn lint"
12
+ "test": "c8 --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node --test 'test/**/*.test.js'",
13
+ "posttest": "yarn lint",
14
+ "lint": "eslint . --ext .js --cache"
15
15
  },
16
16
  "files": [
17
17
  "index.js",
@@ -21,15 +21,15 @@
21
21
  "access": "public"
22
22
  },
23
23
  "devDependencies": {
24
+ "c8": "11.0.0",
24
25
  "eslint": "8.57.1"
25
26
  },
26
27
  "dependencies": {
27
- "@tryghost/debug": "2.0.1",
28
- "@tryghost/kg-default-cards": "10.2.8",
29
- "@tryghost/mg-fs-utils": "0.16.6",
28
+ "@tryghost/debug": "2.0.3",
29
+ "@tryghost/kg-default-cards": "10.2.10",
30
+ "@tryghost/mg-fs-utils": "0.17.0",
31
+ "@tryghost/mg-utils": "0.2.0",
30
32
  "@tryghost/string": "0.3.1",
31
- "cheerio": "1.2.0",
32
33
  "simple-dom": "1.4.0"
33
- },
34
- "gitHead": "e1680568191322d738c188f03298910aa3622735"
34
+ }
35
35
  }