-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
146 lines (126 loc) · 4.08 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import fs from 'fs';
import { readFile } from 'fs/promises';
import { glob } from 'glob';
import * as cheerio from 'cheerio';
import { htmlToLexical } from '@tryghost/kg-html-to-lexical';
import {
checkIfImageExists,
initiateProgressBar,
recursivelyRemoveEmptyElements,
} from './lib/utils.mjs';
const titleElement = 'h1.page-header__title';
init();
async function init() {
const jsonObject = {
db: [
{
meta: {
exported_on: Date.now(),
version: '2.14.0',
},
data: {
posts: [],
},
},
],
};
const files = await glob('src/**/*.html');
if (files.length === 0) {
console.log('No files found. Exiting.');
return;
}
const bar = initiateProgressBar();
bar.start(files.length, 0);
for (let x = 0; x < files.length; x++) {
const file = files[x];
const htmlContent = await readFile(file, 'utf-8');
const $ = cheerio.load(htmlContent.replaceAll('http://', 'https://'));
// Get the title & meta data
const title = $(titleElement).text().trim();
bar.update(x + 1, { title: `Migrating: ${title}` });
const meta_title = $('meta[name="og:title"]').attr('content') || title;
const meta_description =
$('meta[name="description"]').attr('content') || null;
// Prepare the date
const authorData = $('#hubspot-author_data').text();
const dateRegex = /\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2}/;
// const dateRegex =
// /\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{1,2},\s\d{4}\s\d{1,2}:\d{2}:\d{2}\s(AM|PM)\b/;
const dateStr = authorData.match(dateRegex)?.[0];
const published_at = dateStr ? new Date(Date.parse(dateStr)) : new Date();
const parentContainer = $('span#hs_cos_wrapper_post_body');
// Featured image
let featureImage = null;
let featureImageAlt = null;
const firstP = $('span#hs_cos_wrapper_post_body p').first();
if (firstP.find('img').length) {
const imgSrc = firstP.find('img').attr('src');
if (await checkIfImageExists(imgSrc)) {
featureImage = imgSrc;
featureImageAlt = firstP.find('img').attr('alt');
}
firstP.remove();
}
// Find the index of the element that contains the text
const targetIndex = parentContainer
.children()
.toArray()
.findIndex((child) => {
return $(child).text().includes('Andrew Wiseman, Wiseman Lawyers');
});
// Remove all elements after the target index
if (targetIndex !== -1) {
parentContainer
.children()
.toArray()
.slice(targetIndex + 1)
.forEach((child) => {
$(child).remove();
});
}
$('.hs-cta-wrapper').parent().remove();
$('p:empty, span:empty').each(function () {
recursivelyRemoveEmptyElements($(this));
});
// Sanatize iframes
$('iframe').each((i, elem) => {
// Get the outer HTML of the iframe
const iframeHtml = $.html(elem);
// Wrap the iframe in an HTML card
const cardHtml = `<!--kg-card-begin: html-->\n${iframeHtml}\n<!--kg-card-end: html-->`;
// Replace the iframe with the card
$(elem).replaceWith(cardHtml);
});
// Now create the postContent variable
let postContent = parentContainer.html();
const lexical = htmlToLexical(postContent);
const post = {
title,
html: postContent,
lexical: JSON.stringify(lexical),
feature_image: featureImage,
feature_image_alt: featureImageAlt,
feature_image_caption: null,
featured: false,
page: 0,
status: 'published',
published_by: 1,
email_only: false,
author_id: 1,
created_by: 1,
updated_by: 1,
meta_title,
meta_description,
published_at: published_at.toISOString(),
created_at: new Date().toISOString(),
};
// Create a new object in the data.posts array
jsonObject.db[0].data.posts.push(post);
}
const migrationName = `migration-${Date.now()}`;
fs.writeFileSync(
`output/${migrationName}.json`,
JSON.stringify(jsonObject, null, 2),
);
console.log(`\nMigration created: ${migrationName}`);
}