forked from konsalex/gatsby-wordpress-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
150 lines (131 loc) · 4.24 KB
/
functions.js
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
147
148
149
150
const cheerio = require('cheerio');
const { get, findIndex } = require('lodash');
const chalk = require('chalk');
const moment = require('moment');
const TurndownService = require('turndown');
const writing = require('./writing');
const { log } = console;
const { yellow: progress } = chalk;
/* *********** Turndown Initializing ********** */
const turndownService = new TurndownService({
headingStyle: 'atx',
codeBlockStyle: 'fenced',
});
// Pre tag => PrismJS for gatsby plugin
turndownService.addRule('pre-tags', {
filter: 'pre',
replacement(value) {
// Remove Escape Characters from String
// created by TurndownService.prototype.escape
// Unfortenately node is private in Turndown.js
const content = value.replace(/\\/g, '');
// Check if there is a Newline character to make the comment inline
return content.split('\n').length > 1
? `\n\`\`\`\n${content}\n\`\`\`\n`
: ` \`${content}\` `;
},
});
// Code tag => PrismJS for gatsby plugin
turndownService.addRule('code-tags', {
filter: 'code',
replacement(content) {
return content.split('\n').length > 1
? `\n\`\`\`\n${content}\n\`\`\`\n`
: ` \`${content}\` `;
},
});
// Strong tag fix es
turndownService.addRule('strong', {
filter: 'strong',
replacement(content) {
return `**${content}**`;
},
});
// Strong tag fixes
turndownService.addRule('span', {
filter(node) {
return node.nodeName === 'SPAN' && get(node, 'attributes[0].value');
},
replacement(content, node) {
return `<span style="${node.attributes[0].value}">${content}</span>`;
},
});
/* parseImages(value)
* value : The content of the post with all the tags inside
* return : [{url: <URL of the image>, fileName: <The UUID name generated>},...]
*/
const parseImages = value => {
const content = cheerio.load(value);
const imagesElements = content('img');
const images = imagesElements
.map((index, { attribs: { src: imageURL, ...rest } }) => ({
fileName: imageURL.substring(imageURL.lastIndexOf('/') + 1),
url: imageURL,
...rest,
}))
.toArray();
return images;
};
const dataWrangle = async (data, destination) => {
// Iterate in every Post
data.rss.channel[0].item.map((post, index) => {
log(progress(`Currently Parsing Post No: ${index + 1}`));
const getMeta = (key, defaultMeta = undefined) => {
const metaIndex = findIndex(
post['wp:postmeta'],
meta => meta['wp:meta_key'][0] === key,
);
return metaIndex !== -1
? get(post, `['wp:postmeta'][${metaIndex}]['wp:meta_value'][0]`)
: defaultMeta;
};
let content = post['content:encoded'][0];
let images = parseImages(content);
images.forEach(image => {
content = content.replace(
new RegExp(image.url, 'g'),
`./${image.fileName}`,
);
});
const thumbnail = getMeta('essb_cached_image');
images =
thumbnail !== undefined
? [
{
url: thumbnail,
fileName: thumbnail.substring(thumbnail.lastIndexOf('/') + 1),
},
...images,
]
: images;
content = turndownService.turndown(content);
const categories = post.category
? `[${post.category.map(
(category, categoriesIndex) =>
`${categoriesIndex > 0 ? ' ' : ''}"${category._}"`,
)}]`
: '';
const header = {
layout: 'post',
title: `"${get(post, 'title[0]')}"`,
image: thumbnail
? `./${thumbnail.substring(thumbnail.lastIndexOf('/') + 1)}`
: undefined,
author: get(post, `['dc:creator'][0]`),
date: moment(get(post, 'pubDate[0]')).format(),
categories,
slug: get(post, `['wp:post_name'][0]`) || undefined,
excerpt: get(post, `['excerpt:encoded'][0]`)
? `"${get(post, `['excerpt:encoded'][0]`)}"`
: undefined,
draft: get(post, `['wp:status'][0]`) !== 'publish',
meta_title: `"${getMeta('_yoast_wpseo_title', get(post, 'title[0]'))}"`,
twitter_shares: getMeta('essb_c_twitter'),
facebook_shares: getMeta('essb_c_facebook'),
kksr_ratings: getMeta('_kksr_ratings'),
kksr_casts: getMeta('_kksr_casts'),
};
return writing(header, images, content, destination);
});
};
module.exports = { dataWrangle };