From 4c62b352a12f512ccf3939c31061cadcc7f08f1a Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Wed, 11 Sep 2024 15:52:07 +0700 Subject: [PATCH] Repaired URL extraction --- src/actions/askCodaTable.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/actions/askCodaTable.ts b/src/actions/askCodaTable.ts index f2d9fbf..1c11fb8 100644 --- a/src/actions/askCodaTable.ts +++ b/src/actions/askCodaTable.ts @@ -102,8 +102,11 @@ export async function handler({ input }: ActionContext): Promise { } function extractIdsFromUrl(url: string): { docId: string, pageName: string } { + console.log('Extracting IDs from URL:', url); const urlObj = new URL(url); + console.log('URL object:', urlObj); const pathParts = urlObj.pathname.split('/').filter(Boolean); + console.log('Path parts:', pathParts); if (pathParts.length < 2) { throw new Error('Invalid Coda URL format'); @@ -112,12 +115,22 @@ function extractIdsFromUrl(url: string): { docId: string, pageName: string } { let docId = ''; let pageName = ''; - // Check if the URL is in the format https://coda.io/d/_d{docId}/{pageName} - if (pathParts[0] === 'd' && pathParts[1].startsWith('_d')) { - docId = pathParts[1].slice(2); // Remove '_d' prefix - pageName = pathParts[2] || ''; // Page name is the third part, if present + console.log('First path part:', pathParts[0]); + console.log('Second path part:', pathParts[1]); + + if (pathParts[0] === 'd') { + const docIdParts = pathParts[1].split('_'); + if (docIdParts.length > 1) { + docId = docIdParts[docIdParts.length - 1]; // Get the last part after splitting by '_' + docId = docId.startsWith('d') ? docId.slice(1) : docId; // Remove leading 'd' if present + pageName = pathParts[2] || ''; // Page name is the third part, if present + console.log('Extracted Doc ID:', docId); + console.log('Extracted Page Name:', pageName); + } else { + throw new Error('Unable to extract Doc ID from the provided URL'); + } } else { - throw new Error('Unable to extract Doc ID from the provided URL'); + throw new Error('Invalid Coda URL format'); } return { docId, pageName };