From 57d89ecb0deea3ad00783a3cbb9e9db33f56a3af Mon Sep 17 00:00:00 2001 From: cliftonc Date: Thu, 26 May 2022 14:42:05 +0200 Subject: [PATCH] Allow links to be in either format --- dist/index.js | 27 ++++++++++++++++++++------- src/links.js | 27 ++++++++++++++++++++------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8ac15a0..2ff57fe 100644 --- a/dist/index.js +++ b/dist/index.js @@ -21151,19 +21151,32 @@ const updateLinks = async (linkDatabaseId, links, { notion }) => { const existingLinkRows = await notion.databases.query({ database_id: linkDatabaseId }) + const existingLinks = processRows(existingLinkRows) // Now scan for any that have changed or are new const linkKeys = Object.keys(links) for (const link of linkKeys) { - // Lets see if we can find the row - if (existingLinks[link]) { - const linkId = existingLinks[link].id - const linkUrl = existingLinks[link].url - if (links[link] !== linkUrl) { // url has changed - await updateNotionLink(linkId, link, links[link], { notion }) + // Check if our links are an object or simple attributes + let newLink + let newLinkUrl + if (links[link].url) { + newLinkUrl = links[link].url + newLink = links[link].title + } else { + newLinkUrl = links[link] + newLink = link + } + + // Now lets see if we can find the row + if (existingLinks[newLink]) { + const linkId = existingLinks[newLink].id + const linkUrl = existingLinks[newLink].url + + if (newLinkUrl !== linkUrl) { // url has changed + await updateNotionLink(linkId, newLink, newLinkUrl, { notion }) } } else { - await createNotionLink(linkDatabaseId, link, links[link], { notion }) + await createNotionLink(linkDatabaseId, newLink, newLinkUrl, { notion }) } } } diff --git a/src/links.js b/src/links.js index fa9146c..bd59532 100644 --- a/src/links.js +++ b/src/links.js @@ -56,19 +56,32 @@ const updateLinks = async (linkDatabaseId, links, { notion }) => { const existingLinkRows = await notion.databases.query({ database_id: linkDatabaseId }) + const existingLinks = processRows(existingLinkRows) // Now scan for any that have changed or are new const linkKeys = Object.keys(links) for (const link of linkKeys) { - // Lets see if we can find the row - if (existingLinks[link]) { - const linkId = existingLinks[link].id - const linkUrl = existingLinks[link].url - if (links[link] !== linkUrl) { // url has changed - await updateNotionLink(linkId, link, links[link], { notion }) + // Check if our links are an object or simple attributes + let newLink + let newLinkUrl + if (links[link].url) { + newLinkUrl = links[link].url + newLink = links[link].title + } else { + newLinkUrl = links[link] + newLink = link + } + + // Now lets see if we can find the row + if (existingLinks[newLink]) { + const linkId = existingLinks[newLink].id + const linkUrl = existingLinks[newLink].url + + if (newLinkUrl !== linkUrl) { // url has changed + await updateNotionLink(linkId, newLink, newLinkUrl, { notion }) } } else { - await createNotionLink(linkDatabaseId, link, links[link], { notion }) + await createNotionLink(linkDatabaseId, newLink, newLinkUrl, { notion }) } } }