forked from QuantStack/quantstack.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyml-to-json.js
148 lines (132 loc) · 4.88 KB
/
yml-to-json.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
const imagesDirectory = "../../../static/img/blogposts/";
const yaml = require("js-yaml");
const fs = require("fs");
const https = require("https");
const ymlFileName = "_config.yml";
const jsonFileName = "blogpostsDetails";
let yamlFile = fs.readFileSync(ymlFileName, "utf8");
let obj = yaml.load(yamlFile);
const blogpostsDetailsYml = obj["blog"];
const NUMBER_OF_BLOGS = blogpostsDetailsYml.length;
const sizeOf = require("image-size");
const imageContainerDimensions = [273, 180];
function JSONToFile(data, filename) {
fs.writeFileSync(`${filename}.json`, JSON.stringify(data, null, 2));
}
function defineImagesNames(data) {
const imageUrls = [];
const imagePaths = [];
let fileExtension = "";
data.map((blogpost, index) => {
imageUrls.push(blogpost.image);
if (blogpost.image.includes(".png")) {
fileExtension = ".png";
} else if (blogpost.image.includes(".jpg" || ".jpeg")) {
fileExtension = ".jpg";
} else if (blogpost.image.includes(".webp")) {
fileExtension = ".webp";
} else if (blogpost.image.includes(".gif")) {
fileExtension = ".gif";
} else if (blogpost.image.includes(".svg")) {
fileExtension = ".svg";
}
else {
fileExtension = ".png";
}
let name = blogpost.title;
name = name.split("!").join("");
name = name.split(" - ").join("-");
name = name.split(",").join("");
name = name.split("🎉 ").join("");
name = name.split(" 🎉").join("");
name = name.split(" ❤️ ").join("-");
name = name.split(": ").join("-");
name = name.split(".").join("-");
name = name.split("(").join("");
name = name.split(")").join("");
name = name.replace("à", "a");
name = name.substring(0, 80).split(" ").join("-") + fileExtension;
imagePaths.push(imagesDirectory + name);
});
return [imageUrls, imagePaths];
}
function downloadImage(url, filepath) {
return new Promise((resolve, reject) => {
const request = https.get(url, (res) => {
const protocol = "https";
const hostname = res.socket._host;
if (res.statusCode === 200) {
res
.pipe(fs.createWriteStream(filepath))
.on("error", reject)
.once("close", () => resolve(filepath));
} else if (res.statusCode === 301 && res.headers.location) {
// Handle 301 redirect
const newUrl = protocol + "://" + hostname + res.headers.location;
// Close the current response and retry with the new URL
res.resume();
downloadImage(newUrl, filepath).then(resolve).catch(reject);
} else {
res.resume(); // Consume response data to free up memory
reject(new Error(`Request failed with status code: ${res.statusCode}`));
}
});
request.on("error", (err) => {
reject(err);
});
});
}
function getImageDimensions(blogpost) {
const pathPrefix = "../../../static";
const completePath = pathPrefix + blogpost.image;
const dimensions = sizeOf(completePath);
return [dimensions.width, dimensions.height];
}
function changeBlogpostsDetails(
blogpostsDetailsYml,
paths,
imageContainerDimensions
) {
const data = [...blogpostsDetailsYml];
data.map((blogpost, index) => {
const timeIndex = NUMBER_OF_BLOGS - index; /* starting at 1*/
blogpost.imageID = "blogpost-image-" + timeIndex;
blogpost.image = paths[index].replace("../../../static", "");
const [width, height] = getImageDimensions(blogpost);
blogpost.imageNaturalWidth = width;
blogpost.imageNaturalHeight = height;
const imageAspectRatio = width / height;
const containerWidth = imageContainerDimensions[0];
const containerHeight = imageContainerDimensions[1];
if (imageAspectRatio <= 1.4) {
blogpost.imageRenderedWidth = containerHeight * imageAspectRatio;
blogpost.imageRenderedHeight = containerHeight;
} else {
blogpost.imageRenderedWidth = containerWidth;
blogpost.imageRenderedHeight = containerWidth / imageAspectRatio;
}
if (timeIndex === 114) {
blogpost.url = "/blogs/Fanny";
}
});
return data;
}
function downloadLastImage(urls, paths) {
downloadImage(urls[NUMBER_OF_BLOGS], paths[NUMBER_OF_BLOGS]);
}
/**step 1: load .yml file and convert data to an object called blogpostsDetails *********** */
/**step 2: define path of images from the name of blog posts and their chronological index */
const [urls, paths] = defineImagesNames(blogpostsDetailsYml);
console.log(paths);
/**step 3: download last image of last blogpost */
//downloadLastImage(urls, paths);
/** step 4: change the path to images in blogpostsDetails,
* add their natural dimensions and the rendered ones********** */
const modifiedBlogpostsDetails = changeBlogpostsDetails(
blogpostsDetailsYml,
paths,
imageContainerDimensions
);
/** step 5: save modified blogposts details in a .json file
* these data are the one used for building the react blogposts cards */
JSONToFile(modifiedBlogpostsDetails, jsonFileName);