feat: add url shortner

This commit is contained in:
gosangam
2026-05-11 16:14:08 +05:30
parent 179b7d8862
commit 8cd1d2e724
9 changed files with 631 additions and 8 deletions
+31
View File
@@ -249,6 +249,37 @@ export function buildHighlightUrl(selectedText: string): string {
return url.toString();
}
/**
* Build a shortened, shareable URL for the current selection. POSTs the
* `?hl=…` URL to the BFF, which stores the mapping in Postgres (Redis-queued)
* and returns a short code. The destination `/share/<code>` page resolves
* back to the same `?hl=…` URL the existing TextHighlighter already knows
* how to process — no change to highlighting behavior.
*
* Falls back to the long URL on any failure so sharing never silently breaks.
*
* `buildHighlightUrl` (sync) is intentionally left in place so other callers
* (e.g. search-result navigation) keep working unchanged.
*/
export async function buildShortHighlightUrl(
selectedText: string,
): Promise<string> {
const longUrl = buildHighlightUrl(selectedText);
let u: URL;
try {
u = new URL(longUrl);
} catch {
return longUrl;
}
const relativePath = u.pathname + u.search + u.hash;
const { createShortLink } = await import("@/lib/share");
const code = await createShortLink(relativePath);
if (!code) return longUrl;
return new URL(`/share/${code}`, u.origin).toString();
}
/**
* Find and highlight text in the DOM that matches a decoded text fragment.
* Returns true if found and highlighted.