fixed the imports
This commit is contained in:
parent
c78aa2da0d
commit
e3d9912378
|
@ -11,6 +11,8 @@ export default async function importFromHTMLFile(
|
||||||
const dom = new JSDOM(rawData);
|
const dom = new JSDOM(rawData);
|
||||||
const document = dom.window.document;
|
const document = dom.window.document;
|
||||||
|
|
||||||
|
// console.log(document.querySelectorAll("A").length);
|
||||||
|
|
||||||
const bookmarks = document.querySelectorAll("A");
|
const bookmarks = document.querySelectorAll("A");
|
||||||
const totalImports = bookmarks.length;
|
const totalImports = bookmarks.length;
|
||||||
|
|
||||||
|
@ -29,49 +31,54 @@ export default async function importFromHTMLFile(
|
||||||
};
|
};
|
||||||
|
|
||||||
const folders = document.querySelectorAll("H3");
|
const folders = document.querySelectorAll("H3");
|
||||||
|
let unorganizedCollectionId: number | null = null;
|
||||||
|
|
||||||
|
if (folders.length === 0) {
|
||||||
|
const unorganizedCollection = await prisma.collection.findFirst({
|
||||||
|
where: {
|
||||||
|
name: "Imported",
|
||||||
|
ownerId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!unorganizedCollection) {
|
||||||
|
const newUnorganizedCollection = await prisma.collection.create({
|
||||||
|
data: {
|
||||||
|
name: "Imported",
|
||||||
|
description:
|
||||||
|
"Automatically created collection for imported bookmarks.",
|
||||||
|
ownerId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
unorganizedCollectionId = newUnorganizedCollection.id;
|
||||||
|
} else {
|
||||||
|
unorganizedCollectionId = unorganizedCollection.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
createFolder({ filePath: `archives/${unorganizedCollectionId}` });
|
||||||
|
}
|
||||||
|
|
||||||
await prisma
|
await prisma
|
||||||
.$transaction(
|
.$transaction(
|
||||||
async () => {
|
async () => {
|
||||||
// @ts-ignore
|
if (unorganizedCollectionId) {
|
||||||
for (const folder of folders) {
|
// @ts-ignore
|
||||||
const findCollection = await prisma.user.findUnique({
|
|
||||||
where: {
|
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
collections: {
|
|
||||||
where: {
|
|
||||||
name: folder.textContent.trim(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const checkIfCollectionExists = findCollection?.collections[0];
|
|
||||||
|
|
||||||
let collectionId = findCollection?.collections[0]?.id;
|
|
||||||
|
|
||||||
if (!checkIfCollectionExists || !collectionId) {
|
|
||||||
const newCollection = await prisma.collection.create({
|
|
||||||
data: {
|
|
||||||
name: folder.textContent.trim(),
|
|
||||||
description: "",
|
|
||||||
color: "#0ea5e9",
|
|
||||||
isPublic: false,
|
|
||||||
ownerId: userId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
createFolder({ filePath: `archives/${newCollection.id}` });
|
|
||||||
|
|
||||||
collectionId = newCollection.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
createFolder({ filePath: `archives/${collectionId}` });
|
|
||||||
|
|
||||||
const bookmarks = folder.nextElementSibling.querySelectorAll("A");
|
|
||||||
for (const bookmark of bookmarks) {
|
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({
|
await prisma.link.create({
|
||||||
data: {
|
data: {
|
||||||
name: bookmark.textContent.trim(),
|
name: bookmark.textContent.trim(),
|
||||||
|
@ -103,14 +110,103 @@ export default async function importFromHTMLFile(
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
description: bookmark.getAttribute("DESCRIPTION")
|
description: description,
|
||||||
? bookmark.getAttribute("DESCRIPTION")
|
collectionId: unorganizedCollectionId,
|
||||||
: "",
|
|
||||||
collectionId: collectionId,
|
|
||||||
createdAt: new Date(),
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// @ts-ignore
|
||||||
|
for (const folder of folders) {
|
||||||
|
const findCollection = await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
id: userId,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
collections: {
|
||||||
|
where: {
|
||||||
|
name: folder.textContent.trim(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const checkIfCollectionExists = findCollection?.collections[0];
|
||||||
|
|
||||||
|
let collectionId = findCollection?.collections[0]?.id;
|
||||||
|
|
||||||
|
if (!checkIfCollectionExists || !collectionId) {
|
||||||
|
const newCollection = await prisma.collection.create({
|
||||||
|
data: {
|
||||||
|
name: folder.textContent.trim(),
|
||||||
|
description: "",
|
||||||
|
color: "#0ea5e9",
|
||||||
|
isPublic: false,
|
||||||
|
ownerId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
createFolder({ filePath: `archives/${newCollection.id}` });
|
||||||
|
|
||||||
|
collectionId = newCollection.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
createFolder({ filePath: `archives/${collectionId}` });
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ timeout: 30000 }
|
{ timeout: 30000 }
|
||||||
|
|
Ŝarĝante…
Reference in New Issue