@tryghost/mg-tinynews 0.10.8-0 → 0.11.0-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.
Files changed (42) hide show
  1. package/README.md +112 -30
  2. package/build/index.d.ts +8 -0
  3. package/build/index.js +5 -0
  4. package/build/index.js.map +1 -0
  5. package/build/lib/ingest.d.ts +18 -0
  6. package/build/lib/ingest.js +91 -0
  7. package/build/lib/ingest.js.map +1 -0
  8. package/build/lib/json-to-html.d.ts +35 -0
  9. package/build/lib/json-to-html.js +238 -0
  10. package/build/lib/json-to-html.js.map +1 -0
  11. package/build/lib/map.d.ts +26 -0
  12. package/build/lib/map.js +209 -0
  13. package/build/lib/map.js.map +1 -0
  14. package/build/lib/process.d.ts +76 -0
  15. package/build/lib/process.js +235 -0
  16. package/build/lib/process.js.map +1 -0
  17. package/build/lib/text.d.ts +6 -0
  18. package/build/lib/text.js +60 -0
  19. package/build/lib/text.js.map +1 -0
  20. package/build/lib/types.d.ts +112 -0
  21. package/build/lib/types.js +3 -0
  22. package/build/lib/types.js.map +1 -0
  23. package/build/test/helpers.d.ts +25 -0
  24. package/build/test/helpers.js +74 -0
  25. package/build/test/helpers.js.map +1 -0
  26. package/build/test/ingest.test.d.ts +1 -0
  27. package/build/test/ingest.test.js +134 -0
  28. package/build/test/ingest.test.js.map +1 -0
  29. package/build/test/json-to-html.test.d.ts +1 -0
  30. package/build/test/json-to-html.test.js +662 -0
  31. package/build/test/json-to-html.test.js.map +1 -0
  32. package/build/test/map.test.d.ts +1 -0
  33. package/build/test/map.test.js +367 -0
  34. package/build/test/map.test.js.map +1 -0
  35. package/build/test/process.test.d.ts +1 -0
  36. package/build/test/process.test.js +261 -0
  37. package/build/test/process.test.js.map +1 -0
  38. package/package.json +24 -10
  39. package/index.js +0 -7
  40. package/lib/json-to-html.js +0 -324
  41. package/lib/mapper.js +0 -368
  42. package/lib/text.js +0 -95
@@ -1,324 +0,0 @@
1
- import SimpleDom from 'simple-dom';
2
- import {cardUtils} from '@tryghost/mg-utils';
3
- import {TextNode} from './text.js';
4
-
5
- const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
6
- const imageCard = cardUtils.getCard('image');
7
-
8
- // From https://gist.github.com/takien/4077195
9
- const getYouTubeID = url => {
10
- const arr = url.split(/(vi\/|v%3D|v=|\/v\/|youtu\.be\/|\/embed\/)/);
11
- return undefined !== arr[2] ? arr[2].split(/[^\w-]/i)[0] : arr[0];
12
- };
13
-
14
- const formatChildStyles = child => {
15
- let html = [];
16
-
17
- if (child?.link) {
18
- html.push(`<a href="${child.link}">`);
19
- }
20
-
21
- if (child?.style?.bold) {
22
- html.push('<strong>');
23
- }
24
-
25
- if (child?.style?.italic) {
26
- html.push('<em>');
27
- }
28
-
29
- if (child?.style?.underline) {
30
- html.push('<u>');
31
- }
32
-
33
- html.push(child.content);
34
-
35
- if (child?.style?.underline) {
36
- html.push('</u>');
37
- }
38
-
39
- if (child?.style?.italic) {
40
- html.push('</em>');
41
- }
42
-
43
- if (child?.style?.bold) {
44
- html.push('</strong>');
45
- }
46
-
47
- if (child?.link) {
48
- html.push(`</a>`);
49
- }
50
-
51
- return html.join('');
52
- };
53
-
54
- const EntryList = block => {
55
- const listTypeEl = block.listType === 'NUMBER' ? 'ol' : 'ul';
56
-
57
- let html = [];
58
-
59
- html.push(`<${listTypeEl}>`);
60
-
61
- block.items.forEach(item => {
62
- item.children.forEach(child => {
63
- html.push(`<li>${formatChildStyles(child)}</li>`);
64
- });
65
- });
66
-
67
- html.push(`</${listTypeEl}>`);
68
-
69
- return html.join('');
70
- };
71
-
72
- const EntryText = block => {
73
- return TextNode({node: block});
74
- };
75
-
76
- const EntryImage = block => {
77
- if (block.children.length === 0) {
78
- return;
79
- }
80
-
81
- let html = [];
82
-
83
- block.children.forEach(child => {
84
- let cardOpts = {
85
- env: {dom: new SimpleDom.Document()},
86
- payload: {
87
- src: child.imageUrl,
88
- alt: child.imageAlt,
89
- caption: child.imageAlt
90
- }
91
- };
92
-
93
- if (block?.link) {
94
- cardOpts.payload.href = block.link;
95
- }
96
-
97
- html.push(serializer.serialize(imageCard.render(cardOpts)));
98
- });
99
-
100
- return html.join('');
101
- };
102
-
103
- const EntryHeading = block => {
104
- return TextNode({node: block});
105
- };
106
-
107
- const EntryBlockquote = block => {
108
- if (block.children[0].content.trim() === '') {
109
- return;
110
- }
111
-
112
- let html = [];
113
-
114
- html.push('<blockquote><p>');
115
-
116
- block.children.forEach(child => {
117
- if (child.content.trim() === '') {
118
- return;
119
- }
120
-
121
- html.push(formatChildStyles(child));
122
- });
123
-
124
- html.push('</p></blockquote>');
125
-
126
- return html.join('');
127
- };
128
-
129
- const EntryHorizontalRule = () => {
130
- let html = [];
131
- html.push('<hr>');
132
- return html.join('');
133
- };
134
-
135
- const EntryTwitterEmbed = block => {
136
- const blockHtml = `<figure class="kg-card kg-embed-card"><blockquote class="twitter-tweet"><a href="${block.link}"></a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></figure>`;
137
-
138
- return blockHtml;
139
- };
140
-
141
- const EntryTikTokEmbed = block => {
142
- const usernameRegexp = new RegExp('(?<username>@[a-zA-Z0-9-_]+)');
143
- const usernameMatches = block.link.match(usernameRegexp);
144
-
145
- const videoIDRegexp = new RegExp('(?<videoID>[0-9]{10,})');
146
- const videoIDMatches = block.link.match(videoIDRegexp);
147
-
148
- if (usernameMatches?.groups?.username && videoIDMatches?.groups?.videoID) {
149
- return `<figure class="kg-card kg-embed-card"><blockquote class="tiktok-embed" cite="https://www.tiktok.com/${usernameMatches.groups.username}/video/${videoIDMatches.groups.videoID}" data-video-id="${videoIDMatches.groups.videoID}" style="max-width: 605px;min-width: 325px;"><section><a target="_blank" title="${usernameMatches.groups.username}" href="https://www.tiktok.com/${usernameMatches.groups.username}?refer=embed">${usernameMatches.groups.username}</a></section></blockquote><script async src="https://www.tiktok.com/embed.js"></script></figure>`;
150
- } else {
151
- return `<p><a href="${block.link}">${block.link}</a></p>`;
152
- }
153
- };
154
-
155
- const EntryFacebookEmbed = block => {
156
- if (block?.html) {
157
- const blockHtml = `<!--kg-card-begin: html-->${block.html}<!--kg-card-end: html-->`;
158
- return blockHtml;
159
- } else {
160
- return `<p><a href="${block.link}">${block.link}</a></p>`;
161
- }
162
- };
163
-
164
- const EntryVimeoEmbed = block => {
165
- const videoIDRegexp = new RegExp('(?<videoID>[0-9]{3,})');
166
- const videoIDMatches = block.link.match(videoIDRegexp);
167
-
168
- if (videoIDMatches?.groups?.videoID) {
169
- const blockHtml = `<iframe src="https://player.vimeo.com/video/${videoIDMatches.groups.videoID}" width="160" height="90" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>`;
170
-
171
- return blockHtml;
172
- } else {
173
- return `<p><a href="${block.link}">${block.link}</a></p>`;
174
- }
175
- };
176
-
177
- const EntryYoutubeEmbed = block => {
178
- const videoID = getYouTubeID(block.link);
179
-
180
- const blockHtml = `<iframe width="160" height="90" src="https://www.youtube.com/embed/${videoID}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`;
181
-
182
- return blockHtml;
183
- };
184
-
185
- const EntryApplePodcastsEmbed = block => {
186
- const iframeSrc = block.link.replace('https://podcasts.apple.com', 'https://embed.podcasts.apple.com');
187
- const blockHtml = `<iframe allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;border-radius:10px;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="${iframeSrc}"></iframe>`;
188
-
189
- return blockHtml;
190
- };
191
-
192
- const EntryInstagramPostEmbed = block => {
193
- const blockHtml = `<iframe src="${block.link}embed/captioned/" class="instagram-media" allowtransparency="true" allowfullscreen="true" frameborder="0" scrolling="no" style="background: white; max-width: 658px; width: calc(100% - 2px); border-radius: 3px; border: 1px solid rgb(219, 219, 219); box-shadow: none; display: block; margin: 0px; min-width: 326px; padding: 0px;"></iframe><script async="" src="//www.instagram.com/embed.js"></script>`;
194
-
195
- return blockHtml;
196
- };
197
-
198
- const EntryInstagramReelEmbed = block => {
199
- const blockHtml = `<iframe class="instagram-media" src="${block.link}embed/captioned/" allowtransparency="true" allowfullscreen="true" frameborder="0" scrolling="no" style="background: white; max-width: 540px; width: calc(100% - 2px); border-radius: 3px; border: 1px solid rgb(219, 219, 219); box-shadow: none; display: block; margin: 0px; min-width: 326px; padding: 0px; position: relative;"></iframe><script async="" src="//www.instagram.com/embed.js"></script>`;
200
-
201
- return blockHtml;
202
- };
203
-
204
- const EntryGoogleDocsEmbed = block => {
205
- const blockHtml = `<iframe class="googledocs-embed" src="${block.link}" allowtransparency="true" allowfullscreen="true" frameborder="0" style="width: 100%; height: 500px;"></iframe>`;
206
-
207
- return blockHtml;
208
- };
209
-
210
- const EntrySpotifyEmbed = block => {
211
- let embedUrl = block.link.replace('https://open.spotify.com/episode/', 'https://open.spotify.com/embed/episode/');
212
- embedUrl = embedUrl.replace('https://open.spotify.com/show/', 'https://open.spotify.com/embed/show/');
213
- const blockHtml = `<iframe src="${embedUrl}" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>`;
214
-
215
- return blockHtml;
216
- };
217
-
218
- const jsonToHtml = blocks => {
219
- let html = [];
220
-
221
- if (typeof blocks === 'string') {
222
- return blocks;
223
- }
224
-
225
- blocks.forEach(block => {
226
- const itemType = block.type ?? null;
227
- const itemStyle = block.style ?? null;
228
-
229
- if (itemType === 'mainImage') {
230
- // This element is skipped as we use the top level 'featured_image' instead
231
- // Using this would result in duplicates
232
- return;
233
- }
234
-
235
- if (itemType === 'text') {
236
- if (
237
- itemStyle === 'TITLE' ||
238
- itemStyle === 'SUBTITLE' ||
239
- itemStyle === 'HEADING_1' ||
240
- itemStyle === 'HEADING_2' ||
241
- itemStyle === 'HEADING_3' ||
242
- itemStyle === 'HEADING_4'
243
- ) {
244
- html.push(EntryHeading(block));
245
- } else if (itemStyle === 'NORMAL_TEXT') {
246
- html.push(EntryText(block));
247
- } else {
248
- // eslint-disable-next-line no-console
249
- console.error(`Unhandled text type: ${itemType} - Unhandled style: ${itemStyle}`, {
250
- context: block
251
- });
252
- }
253
- } else if (itemType === 'blockquote') {
254
- html.push(EntryBlockquote(block));
255
- } else if (itemType === 'hr') {
256
- html.push(EntryHorizontalRule());
257
- } else if (itemType === 'list') {
258
- html.push(EntryList(block));
259
- } else if (itemType === 'image') {
260
- html.push(EntryImage(block));
261
- } else if (itemType === 'embed') {
262
- const embedUrl = URL.parse(block.link);
263
- const embedHost = embedUrl?.hostname ?? '';
264
- const embedPath = embedUrl?.pathname ?? '';
265
-
266
- if (embedHost === 'twitter.com') {
267
- html.push(EntryTwitterEmbed(block));
268
- } else if (embedHost === 'www.tiktok.com') {
269
- html.push(EntryTikTokEmbed(block));
270
- } else if (embedHost === 'www.facebook.com') {
271
- html.push(EntryFacebookEmbed(block));
272
- } else if (embedHost === 'vimeo.com') {
273
- html.push(EntryVimeoEmbed(block));
274
- } else if (embedHost === 'www.youtube.com' || embedHost === 'youtu.be') {
275
- html.push(EntryYoutubeEmbed(block));
276
- } else if (embedHost === 'podcasts.apple.com') {
277
- html.push(EntryApplePodcastsEmbed(block));
278
- } else if (
279
- embedHost === 'open.spotify.com' &&
280
- (embedPath.startsWith('/episode/') || embedPath.startsWith('/show/'))
281
- ) {
282
- html.push(EntrySpotifyEmbed(block));
283
- } else if (embedHost === 'www.instagram.com' && embedPath.startsWith('/p/')) {
284
- html.push(EntryInstagramPostEmbed(block));
285
- } else if (embedHost === 'www.instagram.com' && embedPath.startsWith('/reel/')) {
286
- html.push(EntryInstagramReelEmbed(block));
287
- } else if (embedHost === 'docs.google.com') {
288
- html.push(EntryGoogleDocsEmbed(block));
289
- } else {
290
- // eslint-disable-next-line no-console
291
- console.error(`Unhandled embed: ${block.link}`, {
292
- context: block
293
- });
294
- }
295
- } else {
296
- // eslint-disable-next-line no-console
297
- console.error(`Unhandled type: ${itemType} - Unhandled style: ${itemStyle}`, {
298
- context: block
299
- });
300
- }
301
- });
302
-
303
- return html.join('');
304
- };
305
-
306
- export {
307
- jsonToHtml,
308
- formatChildStyles,
309
- EntryList,
310
- EntryText,
311
- EntryImage,
312
- EntryHeading,
313
- EntryBlockquote,
314
- EntryHorizontalRule,
315
- EntryTwitterEmbed,
316
- EntryTikTokEmbed,
317
- EntryFacebookEmbed,
318
- EntryVimeoEmbed,
319
- EntryYoutubeEmbed,
320
- EntryApplePodcastsEmbed,
321
- EntryInstagramPostEmbed,
322
- EntryInstagramReelEmbed,
323
- EntryGoogleDocsEmbed
324
- };
package/lib/mapper.js DELETED
@@ -1,368 +0,0 @@
1
- import {readFile} from 'node:fs/promises';
2
- import sanitizeHtml from 'sanitize-html';
3
- import {domUtils} from '@tryghost/mg-utils';
4
- import {slugify} from '@tryghost/string';
5
- import {_base as debugFactory} from '@tryghost/debug';
6
- import {jsonToHtml} from './json-to-html.js';
7
-
8
- const debug = debugFactory('migrate:tinynews:lib:mapper');
9
-
10
- const textOnly = htmlContent => {
11
- const cleanedContent = sanitizeHtml(htmlContent, {
12
- allowedTags: [],
13
- allowedAttributes: {}
14
- });
15
- return cleanedContent.trim();
16
- };
17
-
18
- const processAuthor = (author, authorsData) => {
19
- const name = [author.first_names, author.last_name].join(' ').trim();
20
- const slug = slugify(name);
21
- const email = author?.email && author.email.length > 0 ? author.email : `${slug}@example.com`;
22
-
23
- let authorObject = {
24
- url: `/author/${slug}`,
25
- data: {
26
- slug: slug,
27
- name: name,
28
- email: email
29
- }
30
- };
31
-
32
- if (author?.photoUrl) {
33
- authorObject.data.profile_image = author.photoUrl;
34
- }
35
-
36
- if (author?.translations?.length && author.translations[0].bio) {
37
- authorObject.data.bio = textOnly(author.translations[0].bio);
38
- }
39
-
40
- // If we have a global authorsData object
41
- if (authorsData?.data) {
42
- // Find th author by first & last name
43
- const thisAuthorInfo = authorsData.data.authors.find(filterAuthor => {
44
- return filterAuthor.first_names === author.first_names && filterAuthor.last_name === author.last_name;
45
- });
46
-
47
- // Update values from the globally supplied authorsData to the individual author object
48
- if (thisAuthorInfo) {
49
- authorObject.data.email = thisAuthorInfo.email;
50
- authorObject.data.bio = textOnly(thisAuthorInfo.translations[0].bio);
51
- authorObject.data.profile_image = thisAuthorInfo.photoUrl;
52
- }
53
- }
54
-
55
- return authorObject;
56
- };
57
-
58
- const mapPostPageConfig = args => {
59
- const {postData, authorsData, postType} = args;
60
-
61
- debug(`Mapping data for ${postData.slug}`);
62
-
63
- const dateNow = new Date().toISOString();
64
-
65
- const mappedData = {
66
- url: postData.live_url,
67
- data: {
68
- slug: postData.slug,
69
- published_at: postData.translations[0].first_published_at ?? dateNow,
70
- updated_at: postData.translations[0].last_published_at ?? dateNow,
71
- created_at: postData.translations[0].first_published_at ?? dateNow,
72
- title: postData.translations[0].headline,
73
- type: postType === 'page' ? 'page' : 'post',
74
- html: null,
75
- status: postData.translations[0].published ? 'published' : 'draft',
76
- og_title: postData.translations[0].facebook_title ?? null,
77
- og_description: postData.translations[0].facebook_description ?? null,
78
- twitter_title: postData.translations[0].twitter_title ?? null,
79
- twitter_description: postData.translations[0].twitter_description ?? null,
80
- meta_title: postData.translations[0].search_title ?? null,
81
- meta_description: postData.translations[0].search_description ?? null,
82
- custom_excerpt: postData.translations[0].search_description ?? null,
83
- visibility: 'public',
84
- tags: []
85
- }
86
- };
87
-
88
- if (postType !== 'newsletter' && postData.translations[0].content) {
89
- mappedData.data.html = jsonToHtml(postData.translations[0].content);
90
- }
91
-
92
- if (mappedData.data.html.includes('class="fb-post')) {
93
- mappedData.data.codeinjection_head =
94
- '<div id="fb-root"></div><script async defer src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2"></script>';
95
- }
96
-
97
- if (postData?.translations[0].main_image?.children && postData?.translations[0].main_image?.children[0]?.imageUrl) {
98
- mappedData.data.feature_image = postData.translations[0].main_image.children[0].imageUrl;
99
- mappedData.data.feature_image_alt = postData.translations[0].main_image.children[0]?.imageAlt ?? null;
100
- mappedData.data.feature_image_caption = postData.translations[0].main_image.children[0]?.imageAlt ?? null;
101
- }
102
-
103
- // Handle mainImage from pages (its in the content object)
104
- if (postType === 'page' && typeof postData.translations[0].content === 'object') {
105
- const hasMainImageInContent = postData.translations[0].content.find(block => {
106
- return block.type === 'mainImage';
107
- });
108
- if (hasMainImageInContent?.children[0]?.imageUrl) {
109
- mappedData.data.feature_image = hasMainImageInContent.children[0].imageUrl;
110
- }
111
- }
112
-
113
- if (postData.author && postData.author.length) {
114
- mappedData.data.author = processAuthor(postData.author[0].author, authorsData);
115
- } else if (postData.translations[0].created_by_email) {
116
- const authorEmailName = postData.translations[0].created_by_email.split('@')[0];
117
- mappedData.data.author = {
118
- url: `/author/${authorEmailName}`,
119
- data: {
120
- slug: authorEmailName,
121
- name: authorEmailName,
122
- email: postData.translations[0].created_by_email
123
- }
124
- };
125
- } else if (authorsData?.data?.authors?.length === 1) {
126
- mappedData.data.author = processAuthor(authorsData.data.authors[0]);
127
- } else {
128
- mappedData.data.author = {
129
- url: `/author/author`,
130
- data: {
131
- slug: `author`,
132
- name: 'Author',
133
- email: 'author@example.com'
134
- }
135
- };
136
- }
137
-
138
- if (postData.category) {
139
- const categoryPublished = postData.category.published;
140
- let categoryName = postData.category.title ?? postData.category.slug;
141
- let categorySlug = postData.category.slug;
142
-
143
- if (categoryPublished && categoryPublished === false) {
144
- categoryName = `#${categoryName}`;
145
- categorySlug = `hash-${categorySlug}`;
146
- }
147
-
148
- mappedData.data.tags.push({
149
- url: `/tag/${categorySlug}`,
150
- data: {
151
- slug: categorySlug,
152
- name: categoryName
153
- }
154
- });
155
- }
156
-
157
- if (postData.tags) {
158
- postData.tags.forEach(tag => {
159
- const tagData = tag.tag;
160
- const tagPublished = tagData.published;
161
- let tagName = tagData.tag_translations[0].title;
162
- let tagSlug = tagData.slug;
163
-
164
- if (tagPublished && tagPublished === false) {
165
- tagName = `#${tagName}`;
166
- tagSlug = `hash-${tagSlug}`;
167
- }
168
-
169
- mappedData.data.tags.push({
170
- url: `/tag/${tagSlug}`,
171
- data: {
172
- slug: tagSlug,
173
- name: tagName
174
- }
175
- });
176
- });
177
- }
178
-
179
- mappedData.data.tags.push({
180
- url: `migrator-added-tag`,
181
- data: {
182
- slug: `hash-tinynews`,
183
- name: `#tinynews`
184
- }
185
- });
186
-
187
- return mappedData;
188
- };
189
-
190
- const processNewsletterContent = (html, options) => {
191
- // eslint-disable-line no-unused-vars
192
- const cleanedContent = sanitizeHtml(html, {
193
- allowedTags: [
194
- 'b',
195
- 'i',
196
- 'em',
197
- 'strong',
198
- 'a',
199
- 'p',
200
- 'br',
201
- 'ul',
202
- 'ol',
203
- 'li',
204
- 'blockquote',
205
- 'figure',
206
- 'figcaption',
207
- 'img',
208
- 'h1',
209
- 'h2',
210
- 'h3',
211
- 'h4',
212
- 'h5',
213
- 'h6',
214
- 'div'
215
- ],
216
- allowedAttributes: {
217
- a: ['href', 'title', 'rel', 'target'],
218
- img: ['src', 'alt', 'title']
219
- },
220
- transformTags: {
221
- div: 'p' // eslint-disable-line quote-props
222
- }
223
- });
224
-
225
- const cleanedHTML = domUtils.processFragment(cleanedContent, parsed => {
226
- parsed.$('p').forEach(el => {
227
- const innerHtml = domUtils.serializeChildren(el).trim();
228
- if (!innerHtml.length || innerHtml === '<br>' || innerHtml === '<br/>') {
229
- el.remove();
230
- }
231
- });
232
-
233
- // :contains is a jQuery-specific selector, so filter manually
234
- parsed.$('a').forEach(el => {
235
- if ((el.textContent || '').includes('Made with Letterhead')) {
236
- el.remove();
237
- }
238
- });
239
-
240
- return parsed.html().trim();
241
- });
242
-
243
- return `<!--kg-card-begin: html-->${cleanedHTML}<!--kg-card-end: html-->`;
244
- };
245
-
246
- const mapNewsletterConfig = args => {
247
- // eslint-disable-line no-unused-vars
248
- const {postData, options} = args;
249
-
250
- debug(`Mapping data for ${postData.slug}`);
251
-
252
- const dateNow = new Date().toISOString();
253
-
254
- const mappedData = {
255
- url: `${options.url}/newsletters/${postData.slug}`,
256
- data: {
257
- slug: postData.slug,
258
- published_at: postData.newsletter_published_at ?? dateNow,
259
- updated_at: postData.newsletter_published_at ?? dateNow,
260
- created_at: postData.newsletter_created_at ?? dateNow,
261
- title: postData.headline,
262
- custom_excerpt: postData.subheadline ?? null,
263
- type: 'post',
264
- html: null,
265
- status: 'published',
266
- visibility: 'public',
267
- tags: []
268
- }
269
- };
270
-
271
- mappedData.data.html = processNewsletterContent(postData.content, options);
272
-
273
- mappedData.data.tags.push({
274
- url: `migrator-added-tag-newsletter`,
275
- data: {
276
- slug: `hash-newsletter`,
277
- name: `#newsletter`
278
- }
279
- });
280
-
281
- mappedData.data.tags.push({
282
- url: `migrator-added-tag`,
283
- data: {
284
- slug: `hash-tinynews`,
285
- name: `#tinynews`
286
- }
287
- });
288
-
289
- mappedData.data.author = {
290
- url: `/author/newsletter-author`,
291
- data: {
292
- slug: `newsletter`,
293
- name: 'Newsletter',
294
- email: 'newsletter@example.com'
295
- }
296
- };
297
-
298
- return mappedData;
299
- };
300
-
301
- const mapContent = async args => {
302
- const {options} = args;
303
- const {articles, pages, newsletters, authors} = options;
304
-
305
- const output = {
306
- posts: []
307
- };
308
-
309
- let authorDataJSON = null;
310
- if (authors) {
311
- debug(`Parsing Author info from ${authors}`);
312
- const authorDataJSONString = await readFile(authors, 'utf8');
313
- authorDataJSON = JSON.parse(authorDataJSONString);
314
- }
315
-
316
- if (articles) {
317
- debug(`Parsing Article info from ${articles}`);
318
- const articlesJSONString = await readFile(articles, 'utf8');
319
- const articlesJSON = JSON.parse(articlesJSONString);
320
-
321
- await articlesJSON.forEach(data => {
322
- output.posts.push(
323
- mapPostPageConfig({
324
- postData: data,
325
- authorsData: authorDataJSON,
326
- postType: 'post',
327
- options
328
- })
329
- );
330
- });
331
- }
332
-
333
- if (pages) {
334
- debug(`Parsing Pages info from ${pages}`);
335
- const pagesJSONString = await readFile(pages, 'utf8');
336
- const pagesJSON = JSON.parse(pagesJSONString);
337
-
338
- await pagesJSON.forEach(data => {
339
- output.posts.push(
340
- mapPostPageConfig({
341
- postData: data,
342
- authorsData: authorDataJSON,
343
- postType: 'page',
344
- options
345
- })
346
- );
347
- });
348
- }
349
-
350
- if (newsletters) {
351
- debug(`Parsing Newsletters info from ${newsletters}`);
352
- const newslettersJSONString = await readFile(newsletters, 'utf8');
353
- const newslettersJSON = JSON.parse(newslettersJSONString);
354
-
355
- await newslettersJSON.data.newsletter_editions.forEach(data => {
356
- output.posts.push(
357
- mapNewsletterConfig({
358
- postData: data,
359
- options
360
- })
361
- );
362
- });
363
- }
364
-
365
- return output;
366
- };
367
-
368
- export {mapPostPageConfig, mapContent};