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
import { logMessage } from "https://esm.town/v/willthereader/logMessage";
export const extractCommentInfo = (comment) => {
// logMessage("debug", `Comment ID: ${comment.id}, Comment Text: ${comment.html}`);
try {
// Validate the comment object to ensure it has necessary properties
if (
!comment || typeof comment !== "object" || !comment.id || !comment.member || !comment.member.name || !comment.html
) {
throw new Error("Invalid comment object");
}
// Declare and initialize createdAt using the created_at property from the comment object
const createdAt = new Date(comment.created_at).toISOString(); // Convert to ISO string format
// logMessage("debug", `extractCommentInfo called with comment: ${JSON.stringify(comment)}`);
const authorName = comment.member.name;
const commentText = comment.html;
let parentCommentText = null;
// Check if there are replies and if so, get the text of the first reply (assuming it's the parent comment)
// console.log("Replies: ", comment.replies);
if (comment.replies && comment.replies.length > 0) {
parentCommentText = comment.replies[0].html;
}
const result = { authorName, commentText, parentCommentText, createdAt };
// logMessage("debug", `extractCommentInfo result: ${JSON.stringify(result)}`);
return result;
} catch (error) {
// logMessage("error", `Error: ${error.message}`);
return { authorName: "Unknown", createdAt: new Date().toISOString() };
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
February 15, 2024