From 79287ceb0c27a9ff4f3503b44fc63aa6cbe21b6e Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 16 Oct 2023 16:56:42 +0000 Subject: [PATCH] fix extractNavigate --- app/web/src/render/live/logic/route.ts | 32 ++++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/app/web/src/render/live/logic/route.ts b/app/web/src/render/live/logic/route.ts index 30a61e66..c671bd03 100644 --- a/app/web/src/render/live/logic/route.ts +++ b/app/web/src/render/live/logic/route.ts @@ -191,27 +191,35 @@ const streamPage = (p: PG, id: string) => { }; export const extractNavigate = (str: string) => { + return [ + ...findBetween(str, `navigate(`, `)`), + ...findBetween(str, `href = `, `;`), + ]; +}; + +const findBetween = (text: string, opener: string, closer: string) => { let i = 0; - const nstr = "navigate("; + let last = 0; const founds: string[] = []; - let lasti = 0; while (true) { - const start = str.indexOf(nstr, i); - lasti = i; - if (start >= 0) { - const char = str[start + nstr.length]; + const startIndex = text.indexOf(opener, i); + last = i; + if (startIndex >= 0) { + const char = text[startIndex + opener.length]; if (char === '"' || char === "'" || char === "`") { - const end = str.indexOf(`${char})`, start + nstr.length + 1); - const text = str.substring(start + nstr.length + 1, end); - i = end + 3; - founds.push(text); + const end = text.indexOf( + `${char}${closer}`, + startIndex + opener.length + 1 + ); + const found = text.substring(startIndex + opener.length + 1, end); + i = end + 2 + closer.length; + founds.push(found); } } - if (lasti === i) { + if (last === i) { break; } } - return founds; };