adds description and tags parsing for pinboard html import

This commit is contained in:
Paul Hovey 2024-03-23 14:57:34 -05:00
parent 388b9d9184
commit 495af0a752

View File

@ -74,7 +74,7 @@ async function processBookmarks(
} else if (item.type === "element" && item.tagName === "a") { } else if (item.type === "element" && item.tagName === "a") {
// process link // process link
const linkUrl = item?.attributes.find((e) => e.key === "href")?.value; const linkUrl = item?.attributes.find((e) => e.key.toLowerCase() === "href")?.value;
const linkName = ( const linkName = (
item?.children.find((e) => e.type === "text") as TextNode item?.children.find((e) => e.type === "text") as TextNode
)?.content; )?.content;
@ -82,14 +82,41 @@ async function processBookmarks(
.find((e) => e.key === "tags") .find((e) => e.key === "tags")
?.value.split(","); ?.value.split(",");
// set date if available
const linkDateValue = item?.attributes.find((e) => e.key.toLowerCase() === "add_date")?.value;
let linkDate = Date.now();
if (linkDateValue) {
try {
linkDate = Number.parseInt(linkDateValue);
// use the year 2000 as an arbitrary cutoff to determine if a link is in seconds or milliseconds
const year2000ms = 946684800000;
if ((linkDate > 0) && (linkDate < year2000ms)) {
linkDate = linkDate * 1000; // turn epoch seconds into milliseconds
}
} catch (error) {
// just ignore the error if it happens
}
}
let linkDesc = "";
const descNode = data.children.find((e) => (e as Element).tagName?.toLowerCase() === "dd") as Element;
if (descNode && descNode.children.length > 0) {
try {
linkDesc = (descNode.children[0] as TextNode).content;
} catch (error) {
// just ignore the error if it happens
}
}
if (linkUrl && parentCollectionId) { if (linkUrl && parentCollectionId) {
await createLink( await createLink(
userId, userId,
linkUrl, linkUrl,
parentCollectionId, parentCollectionId,
linkName, linkName,
"", linkDesc,
linkTags linkTags,
linkDate
); );
} else if (linkUrl) { } else if (linkUrl) {
// create a collection named "Imported Bookmarks" and add the link to it // create a collection named "Imported Bookmarks" and add the link to it
@ -100,8 +127,9 @@ async function processBookmarks(
linkUrl, linkUrl,
collectionId, collectionId,
linkName, linkName,
"", linkDesc,
linkTags linkTags,
linkDate
); );
} }
@ -136,10 +164,10 @@ const createCollection = async (
name: collectionName, name: collectionName,
parent: parentId parent: parentId
? { ? {
connect: { connect: {
id: parentId, id: parentId,
}, },
} }
: undefined, : undefined,
owner: { owner: {
connect: { connect: {
@ -160,39 +188,41 @@ const createLink = async (
collectionId: number, collectionId: number,
name?: string, name?: string,
description?: string, description?: string,
tags?: string[] tags?: string[],
createdAt?: number,
) => { ) => {
await prisma.link.create({ await prisma.link.create({
data: { data: {
name: name || "", name: name || "",
url, type: url,
description, description: description,
collectionId, collectionId: collectionId,
tags: tags:
tags && tags[0] tags && tags[0]
? { ? {
connectOrCreate: tags.map((tag: string) => { connectOrCreate: tags.map((tag: string) => {
return ( return (
{ {
where: { where: {
name_ownerId: { name_ownerId: {
name: tag.trim(),
ownerId: userId,
},
},
create: {
name: tag.trim(), name: tag.trim(),
owner: { ownerId: userId,
connect: { },
id: userId, },
}, create: {
name: tag.trim(),
owner: {
connect: {
id: userId,
}, },
}, },
} || undefined },
); } || undefined
}), );
} }),
}
: undefined, : undefined,
createdAt: createdAt
}, },
}); });
}; };