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

threadLinksCopy.ts

46 lines1.6 KB

1import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
2import { mFallback, msg } from "gt-i18n";
3import type { MFunctionType } from "gt-i18n/types";
4​
5import {
6 formatThreadLink,
7 replaceThreadLinks,
8 threadLinkCandidates,
9 type ThreadLink,
10} from "./threadLinks.ts";
11​
12/** @public */
13export function unknownThreadLinkLabel(
14 link: ThreadLink,
15 messageEnvironmentId: EnvironmentId,
16 resolve: MFunctionType = mFallback,
17): string {
18 return link.environmentId === null || link.environmentId === messageEnvironmentId
19 ? resolve(msg("Thread no longer available"))
20 : resolve(msg("Thread on another machine"));
22​
23/**
24 * Writes each link with its thread's current title, a hosted link as a v2 link. A link `title`
25 * cannot resolve becomes plain text, its written label or a neutral one, except a labeled v1
26 * link, which may open elsewhere.
27 */
28export function relabelThreadLinks(
29 markdown: string,
30 messageEnvironmentId: EnvironmentId,
31 title: (ref: ScopedThreadRef, label: string | undefined) => string | undefined,
32 resolve: MFunctionType = mFallback,
33): string {
34 return replaceThreadLinks(markdown, (written, label) => {
35 for (const ref of threadLinkCandidates(written, messageEnvironmentId)) {
36 const current = title(ref, label);
37 if (current === undefined) continue;
38 return formatThreadLink(
39 { environmentId: written.environmentId, threadId: ref.threadId },
40 current.trim() || label || ref.threadId,
41 );
42 }
43 if (label !== undefined && written.environmentId === null) return undefined;
44 return label?.trim() || unknownThreadLinkLabel(written, messageEnvironmentId, resolve);
45 });