@tryghost/mg-medium-export 0.16.2 → 0.16.3

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.
@@ -1,8 +1,29 @@
1
1
  import $ from 'cheerio';
2
+ import SimpleDom from 'simple-dom';
3
+ import galleryCard from '@tryghost/kg-default-cards/lib/cards/gallery.js';
4
+ import bookmarkCard from '@tryghost/kg-default-cards/lib/cards/bookmark.js';
5
+ const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
6
+
7
+ const doReplace = (str) => {
8
+ const replaceParts = [
9
+ [/[\s\n]/g, ''], // Remove spaces
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
15
+ ];
16
+
17
+ replaceParts.forEach((re) => {
18
+ str = str.replace(re[0], re[1]);
19
+ });
20
+
21
+ return str;
22
+ };
2
23
 
3
24
  const equivalentTitles = (title1, title2) => {
4
- title1 = title1.replace(/[\s\n]/g, '');
5
- title2 = title2.replace(/[\s\n]/g, '');
25
+ title1 = doReplace(title1);
26
+ title2 = doReplace(title2);
6
27
 
7
28
  return title1 === title2;
8
29
  };
@@ -24,6 +45,76 @@ export default ({content, post}) => {
24
45
  $content.find(firstTitle).remove();
25
46
  }
26
47
 
48
+ // Remove the subtitle if it's the same as the excerpt
49
+ $content.find('.graf--subtitle').each((i, el) => {
50
+ if (equivalentTitles($(el).text(), post.data.custom_excerpt)) {
51
+ $(el).remove();
52
+ }
53
+ });
54
+
55
+ // Convert galleries
56
+ $content.find('.section-inner.sectionLayout--outsetRow').each((i, el) => {
57
+ // Check we're only working with a group of <figure> elements
58
+ const children = $(el).children();
59
+ const childElementTags = children.map((ii, ell) => {
60
+ return ell.tagName.toLowerCase();
61
+ }).get();
62
+ const allFigures = childElementTags.every(e => e === 'figure');
63
+
64
+ // If it's all figures
65
+ if (allFigures) {
66
+ let hasCaption = $(el).find('figcaption');
67
+ let caption = hasCaption.length > 0 ? hasCaption.html().trim() : null;
68
+
69
+ let cardOpts = {
70
+ env: {dom: new SimpleDom.Document()},
71
+ payload: {
72
+ images: [],
73
+ caption: caption
74
+ }
75
+ };
76
+
77
+ $(el).find('figure').each((iii, elll) => { // eslint-disable-line no-shadow
78
+ let img = $(elll).find('img');
79
+ cardOpts.payload.images.push({
80
+ row: 0,
81
+ fileName: img.attr('data-image-id'),
82
+ src: img.attr('src'),
83
+ width: img.attr('data-width'),
84
+ height: img.attr('data-height')
85
+ });
86
+ });
87
+
88
+ const galleryHtml = serializer.serialize(galleryCard.render(cardOpts));
89
+
90
+ $(el).replaceWith(galleryHtml);
91
+ }
92
+ });
93
+
94
+ // Convert Medium bookmarks
95
+ $content.find('.graf--mixtapeEmbed').each((i, el) => {
96
+ let cardOpts = {
97
+ env: {dom: new SimpleDom.Document()},
98
+ payload: {}
99
+ };
100
+
101
+ let href = $(el).find('.markup--anchor').attr('href');
102
+ cardOpts.payload.url = href;
103
+
104
+ let src = $(el).find('.js-mixtapeImage').css('background-image').replace(/url\((.*?)\)/, (m, p) => p);
105
+ cardOpts.payload.metadata = {
106
+ url: href,
107
+ title: $(el).find('.markup--strong').text(),
108
+ description: $(el).find('.markup--em').text(),
109
+ thumbnail: src
110
+ };
111
+
112
+ const bookmarkHtml = serializer.serialize(bookmarkCard.render(cardOpts));
113
+
114
+ $(el).replaceWith(`<!--kg-card-begin: html-->\n${bookmarkHtml}\n<!--kg-card-end: html-->`);
115
+ });
116
+
117
+ // Handle blockquotes made of 2 elements
27
118
  $content.find('blockquote.graf--pullquote, blockquote.graf--blockquote').each((i, bq) => {
28
119
  $(bq).removeAttr('name');
29
120
  $(bq).removeAttr('id');
@@ -34,7 +125,19 @@ export default ({content, post}) => {
34
125
  $(a).removeAttr('class');
35
126
  });
36
127
 
37
- $(bq).html(`<p>${$(bq).html()}</p>`);
128
+ let textElements = [];
129
+ textElements.push($(bq).html().trim());
130
+
131
+ if ($(bq).next('.graf-after--pullquote')) {
132
+ const nextElem = $(bq).next('.graf-after--pullquote');
133
+ const nextText = $(nextElem).html();
134
+ if (nextText && nextText.length > 0) {
135
+ textElements.push(nextText.trim());
136
+ $(nextElem).remove();
137
+ }
138
+ }
139
+
140
+ $(bq).replaceWith(`<blockquote><p>${textElements.join('<br><br>')}</p></blockquote>`);
38
141
  });
39
142
 
40
143
  $content.find('pre.graf--pre').each((i, pre) => {
@@ -133,8 +133,9 @@ const processFeatureImage = ({html, post, options}) => {
133
133
 
134
134
  export default ({name, html, globalUser, options}) => {
135
135
  const $post = $.load(html, {
136
- decodeEntities: false
137
- }, false);
136
+ decodeEntities: false,
137
+ scriptingEnabled: false
138
+ }, false); // This `false` is `isDocument`. If `true`, <html>, <head>, and <body> elements are introduced
138
139
 
139
140
  const post = processMeta({name, $post, options});
140
141
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-medium-export",
3
- "version": "0.16.2",
3
+ "version": "0.16.3",
4
4
  "description": "tbc",
5
5
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-medium-export",
6
6
  "author": "Ghost Foundation",
@@ -21,16 +21,18 @@
21
21
  "access": "public"
22
22
  },
23
23
  "devDependencies": {
24
- "eslint": "8.52.0",
24
+ "eslint": "8.54.0",
25
25
  "jest": "29.7.0",
26
26
  "jest-extended": "4.0.2"
27
27
  },
28
28
  "dependencies": {
29
29
  "@tryghost/debug": "0.1.26",
30
- "@tryghost/mg-fs-utils": "^0.12.19",
31
- "@tryghost/string": "0.2.8",
30
+ "@tryghost/kg-default-cards": "9.1.9",
31
+ "@tryghost/mg-fs-utils": "^0.12.20",
32
+ "@tryghost/string": "0.2.10",
32
33
  "cheerio": "1.0.0-rc.12",
33
- "date-fns": "2.30.0"
34
+ "date-fns": "2.30.0",
35
+ "simple-dom": "1.4.0"
34
36
  },
35
- "gitHead": "726ec1832b8bdd045e32b7b591f441a410565464"
37
+ "gitHead": "87eac9f1a90b9e8935ce70d9459db6f8ffec6133"
36
38
  }