minor cleanup

This commit is contained in:
daniel31x13 2024-02-14 10:35:59 -05:00
parent e3d9912378
commit c3f72c4be8

View File

@ -11,8 +11,6 @@ export default async function importFromHTMLFile(
const dom = new JSDOM(rawData);
const document = dom.window.document;
// console.log(document.querySelectorAll("A").length);
const bookmarks = document.querySelectorAll("A");
const totalImports = bookmarks.length;
@ -64,56 +62,7 @@ export default async function importFromHTMLFile(
if (unorganizedCollectionId) {
// @ts-ignore
for (const bookmark of bookmarks) {
// Move up to the parent node (<DT>) and then find the next sibling
let parentDT = bookmark.parentNode;
let nextSibling = parentDT ? parentDT.nextSibling : null;
let description = "";
// Loop through siblings to skip any potential text nodes or whitespace
while (nextSibling && nextSibling.nodeType !== 1) {
nextSibling = nextSibling.nextSibling;
}
// Check if the next sibling element is a <DD> tag and use its content as the description
if (nextSibling && nextSibling.tagName === "DD") {
description = nextSibling.textContent.trim();
}
await prisma.link.create({
data: {
name: bookmark.textContent.trim(),
url: bookmark.getAttribute("HREF"),
tags: bookmark.getAttribute("TAGS")
? {
connectOrCreate: bookmark
.getAttribute("TAGS")
.split(",")
.map((tag: string) =>
tag
? {
where: {
name_ownerId: {
name: tag.trim(),
ownerId: userId,
},
},
create: {
name: tag.trim(),
owner: {
connect: {
id: userId,
},
},
},
}
: undefined
),
}
: undefined,
description: description,
collectionId: unorganizedCollectionId,
},
});
createBookmark(userId, bookmark, unorganizedCollectionId);
}
} else {
// @ts-ignore
@ -155,56 +104,7 @@ export default async function importFromHTMLFile(
const bookmarks = folder.nextElementSibling.querySelectorAll("A");
for (const bookmark of bookmarks) {
// Move up to the parent node (<DT>) and then find the next sibling
let parentDT = bookmark.parentNode;
let nextSibling = parentDT ? parentDT.nextSibling : null;
let description = "";
// Loop through siblings to skip any potential text nodes or whitespace
while (nextSibling && nextSibling.nodeType !== 1) {
nextSibling = nextSibling.nextSibling;
}
// Check if the next sibling element is a <DD> tag and use its content as the description
if (nextSibling && nextSibling.tagName === "DD") {
description = nextSibling.textContent.trim();
}
await prisma.link.create({
data: {
name: bookmark.textContent.trim(),
url: bookmark.getAttribute("HREF"),
tags: bookmark.getAttribute("TAGS")
? {
connectOrCreate: bookmark
.getAttribute("TAGS")
.split(",")
.map((tag: string) =>
tag
? {
where: {
name_ownerId: {
name: tag.trim(),
ownerId: userId,
},
},
create: {
name: tag.trim(),
owner: {
connect: {
id: userId,
},
},
},
}
: undefined
),
}
: undefined,
description: description,
collectionId: collectionId,
},
});
createBookmark(userId, bookmark, collectionId);
}
}
}
@ -215,3 +115,60 @@ export default async function importFromHTMLFile(
return { response: "Success.", status: 200 };
}
const createBookmark = async (
userId: number,
bookmark: any,
collectionId: number
) => {
// Move up to the parent node (<DT>) and then find the next sibling
let parentDT = bookmark.parentNode;
let nextSibling = parentDT ? parentDT.nextSibling : null;
let description = "";
// Loop through siblings to skip any potential text nodes or whitespace
while (nextSibling && nextSibling.nodeType !== 1) {
nextSibling = nextSibling.nextSibling;
}
// Check if the next sibling element is a <DD> tag and use its content as the description
if (nextSibling && nextSibling.tagName === "DD") {
description = nextSibling.textContent.trim();
}
await prisma.link.create({
data: {
name: bookmark.textContent.trim(),
url: bookmark.getAttribute("HREF"),
tags: bookmark.getAttribute("TAGS")
? {
connectOrCreate: bookmark
.getAttribute("TAGS")
.split(",")
.map((tag: string) =>
tag
? {
where: {
name_ownerId: {
name: tag.trim(),
ownerId: userId,
},
},
create: {
name: tag.trim(),
owner: {
connect: {
id: userId,
},
},
},
}
: undefined
),
}
: undefined,
description,
collectionId,
},
});
};