Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadLinks.ts

82 lines3.0 KB

1import { ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4​
5export const THREAD_LINK_PROTOCOL = "t3-thread";
6const THREAD_LINK_HREF_PREFIX = `${THREAD_LINK_PROTOCOL}://v1/`;
7// Code comes first in the alternation so a link written inside a code span or fence is skipped.
8const THREAD_LINK_OUTSIDE_CODE =
9 /(?<fence>(`{3,}|~{3,})[\s\S]*?(?:\2|$))|(?<span>(`+)[^\n]*?\4)|\[[^\]\n]*\]\((?<href>t3-thread:\/\/v1\/[^\s)]+)\)/g;
10​
11const decodeThreadId = Schema.decodeUnknownOption(ThreadId);
12​
13/** The id as written. Thread ids can hold percent escapes of their own, so it is not decoded. */
14export function parseThreadLinkHref(href: string): ThreadId | null {
15 if (!href.startsWith(THREAD_LINK_HREF_PREFIX)) return null;
16 return Option.getOrNull(decodeThreadId(href.slice(THREAD_LINK_HREF_PREFIX.length)));
18​
19export function percentDecodedThreadLinkId(threadId: ThreadId): ThreadId | null {
20 try {
21 const decoded = decodeURIComponent(threadId);
22 return decoded === threadId ? null : Option.getOrNull(decodeThreadId(decoded));
23 } catch {
24 return null;
25 }
27​
28export function resolveThreadLinkRef(
29 source: ScopedThreadRef,
30 refs: ReadonlyArray<ScopedThreadRef>,
31): ScopedThreadRef | null {
32 const decodedId = percentDecodedThreadLinkId(source.threadId);
33 const ids = decodedId === null ? [source.threadId] : [source.threadId, decodedId];
34 for (const threadId of ids) {
35 const local = refs.find(
36 (ref) => ref.environmentId === source.environmentId && ref.threadId === threadId,
37 );
38 if (local) return local;
39 }
40 for (const threadId of ids) {
41 let match: ScopedThreadRef | null = null;
42 for (const ref of refs) {
43 if (ref.threadId !== threadId) continue;
44 if (match !== null) return null;
45 match = ref;
46 }
47 if (match !== null) return match;
48 }
49 return null;
51​
52export function formatThreadLink(threadId: string, label: string): string {
53 const cleaned = label
54 .replace(/[[\]\\\r\n]/g, " ")
55 .replace(/\s+/g, " ")
56 .trim();
57 return `[${cleaned || threadId}](${THREAD_LINK_HREF_PREFIX}${threadId})`;
59​
60export function hasThreadLinks(markdown: string): boolean {
61 return markdown.includes(`](${THREAD_LINK_HREF_PREFIX}`);
63​
64export function relabelThreadLinks(
65 markdown: string,
66 title: (threadId: ThreadId) => string | undefined,
67): string {
68 if (!hasThreadLinks(markdown)) return markdown;
69 return markdown.replace(THREAD_LINK_OUTSIDE_CODE, (source, ...args) => {
70 const href = (args.at(-1) as { href?: string }).href;
71 if (href === undefined) return source;
72 const written = parseThreadLinkHref(href);
73 if (written === null) return source;
74 const decoded = percentDecodedThreadLinkId(written);
75 const threadId =
76 title(written) === undefined && decoded !== null && title(decoded) !== undefined
77 ? decoded
78 : written;
79 const label = title(threadId)?.trim();
80 return label ? formatThreadLink(threadId, label) : source;
81 });