-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.js
97 lines (82 loc) · 2.89 KB
/
utils.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
import * as fs from "fs";
import * as path from "path";
import he from "he";
import axios from "axios";
// File to store the last processed Mastodon post ID
const lastProcessedPostIdFile = path.join(path.resolve(), "data", "lastProcessedPostId.txt");
/**
* Load the last processed post ID from the file
* @returns
*/
export const loadLastProcessedPostId = () => {
try {
return fs.readFileSync(lastProcessedPostIdFile, "utf8").trim();
} catch (error) {
console.error("Error loading last processed post ID:", error);
return null;
}
};
/**
* Save the last processed post ID to the file
*/
export const saveLastProcessedPostId = (lastProcessedPostId) => {
try {
fs.writeFileSync(lastProcessedPostIdFile, `${lastProcessedPostId}`);
} catch (error) {
console.error("Error saving last processed post ID:", error);
}
};
export const splitText = (text, maxLength) => {
// Split the text by spaces
const words = text.split(" ");
let result = [];
let currentChunk = "";
for (const word of words) {
// Add the current word to the current chunk
const potentialChunk = `${currentChunk} ${word}`.trim();
if (potentialChunk.length <= maxLength) {
// If the current chunk is still under max length, add the word
currentChunk = potentialChunk;
} else {
// Otherwise, add the current chunk to the result and start a new chunk
result.push(currentChunk);
currentChunk = word;
}
}
// Add the last chunk to the result
result.push(currentChunk);
return result;
};
export const sanitizeHtml = (input) => {
const withLinebreaks = input
.replace(/<br \/>/g, "\r\n")
.replace(/<\/p>/g, "\r\n\n")
.replace(/<p>/g, "");
const withoutHtml = withLinebreaks.replace(/<[^>]*>/g, "");
const decodeQuotes = he.decode(withoutHtml);
const addSpace = decodeQuotes.replace(/(https?:\/\/)/g, " $1");
return addSpace;
};
export const loadAttachments = (item) =>
Array.from(item.getElementsByTagName("media:content")).reduce((list, item) => {
const url = item.getAttribute("url");
const mimeType = item.getAttribute("type");
const medium = item.getAttribute("medium"); // image or video
const descriptionItems = item.getElementsByTagName("media:description");
const altText = descriptionItems.length === 0 ? "" : descriptionItems[0].textContent;
if (!["video", "image"].includes(medium)) return list;
return [
...list,
{
url,
altText,
mimeType,
medium
}
];
}, []);
export const urlToUint8Array = async (url) => {
const response = await axios.get(url, { responseType: "arraybuffer" });
const arrayBuffer = response.data;
return new Uint8Array(arrayBuffer);
};