feat: add url shortner
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user