import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import { DEFAULT_HOSTED_APP_URL } from "./connectAuth.ts"; export const THREAD_LINK_PROTOCOL = "t3-thread"; const V1_PREFIX = `${THREAD_LINK_PROTOCOL}://v1/`; const V2_PREFIX = `${THREAD_LINK_PROTOCOL}://v2/`; const HOSTED_PREFIX = `${DEFAULT_HOSTED_APP_URL}/threads/`; const HOSTED_SEGMENT = "[A-Za-z0-9_%-]+"; const HOSTED_LINK = `${HOSTED_PREFIX.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&")}${HOSTED_SEGMENT}/${HOSTED_SEGMENT}`; const THREAD_HREF = String.raw`t3-thread:\/\/v[12]\/`; // A label stops at any bracket, so a run of unclosed brackets is scanned once, not once per bracket. const LABEL = String.raw`[^[\]\n]*`; // Earlier alternatives claim text so later ones cannot match inside it: an escaped character opens // nothing, code is literal, and a link to anywhere else keeps a thread URL in its label or href. const THREAD_LINK_OUTSIDE_CODE = new RegExp( [ String.raw`\\[!-/:-@[-\`{-~]`, String.raw`(?(\`{3,}|~{3,})[\s\S]*?(?:\2|$))`, String.raw`(?(\`+)[^\n]*?\4)`, // A labeled link's destination may be angle-bracketed, as CommonMark allows: [label](). String.raw`\[(?