diff --git a/lib/api/controllers/migration/importFromHTMLFile.ts b/lib/api/controllers/migration/importFromHTMLFile.ts index d47af91..6a8b298 100644 --- a/lib/api/controllers/migration/importFromHTMLFile.ts +++ b/lib/api/controllers/migration/importFromHTMLFile.ts @@ -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 (
) 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
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 (
) 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
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 (
) 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
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, + }, + }); +};