Skip to content

Commit c773df5

Browse files
joahgHertzclaude
committed
fix(conversation): hide spoiler links from previews; resolve reference links
Links inside a spoiler no longer get a card that would reveal them; the delimiters pair per parent as remarkSpoilers pairs them. Reference links resolve through their definitions, and the HTTPS fast path ignores case. AGNTOPS-773 Co-authored-by: Hertz <c6741012416ef5c49a865db79682113385c5fb998e8c890234fb442b59e22748@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Joah Gerstenberg <[email protected]>
1 parent 64418b6 commit c773df5

2 files changed

Lines changed: 79 additions & 17 deletions

File tree

‎src/features/messages/preview-links.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@ describe("previewLinks", () => {
2727
).toEqual(["https://kept.example.com/"]);
2828
});
2929

30+
it("keeps spoiler links hidden and shows the ones around them", () => {
31+
expect(
32+
previewLinks(
33+
[
34+
"||[twist](https://plot.example.com/twist)|| and https://after.example.com",
35+
"before ||https://bare.example.com/hidden|| https://shown.example.com",
36+
"*||https://emphasis.example.com||* https://also.example.com",
37+
"|| unclosed https://open.example.com",
38+
].join("\n\n"),
39+
),
40+
).toEqual([
41+
"https://after.example.com/",
42+
"https://shown.example.com/",
43+
"https://also.example.com/",
44+
"https://open.example.com/",
45+
]);
46+
});
47+
48+
it("resolves reference links through their definitions", () => {
49+
expect(
50+
previewLinks(
51+
"[docs][guide], [Guide] and [missing][nope]\n\n[guide]: https://example.org/docs",
52+
),
53+
).toEqual(["https://example.org/docs"]);
54+
});
55+
56+
it("reads the HTTPS scheme in any case", () => {
57+
expect(previewLinks("HTTPS://example.org/docs")).toEqual([
58+
"https://example.org/docs",
59+
]);
60+
});
61+
3062
it("finds nothing in prose without links", () => {
3163
expect(previewLinks("hello there")).toEqual([]);
3264
});

‎src/features/messages/preview-links.ts‎

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { messageLinkParts } from "./message-link-parts";
55
type Node = {
66
type: string;
77
url?: string;
8+
identifier?: string;
89
value?: string;
910
children?: Node[] | undefined;
1011
position?: { start: { offset?: number }; end: { offset?: number } };
@@ -17,7 +18,6 @@ const skipped = new Set([
1718
"definition",
1819
"image",
1920
"imageReference",
20-
"linkReference",
2121
]);
2222

2323
const https = (value: string) => {
@@ -31,38 +31,68 @@ const https = (value: string) => {
3131
}
3232
};
3333

34-
/** The HTTPS links a message's prose names, in order, once each: bare and
35-
* labeled links, but not `<url>`, which asks for no preview, nor links in
36-
* code or naming one of `exclude`. */
34+
/** The HTTPS links a message's prose names, in order, once each: bare,
35+
* labeled and reference links, but not `<url>`, which asks for no preview,
36+
* nor links in code or spoilers, nor ones naming one of `exclude`. */
3737
export function previewLinks(
3838
content: string,
3939
exclude: readonly string[] = [],
4040
): readonly string[] {
4141
const prepared = prepareMarkdown(content);
42-
if (prepared.kind === "plain" || !content.includes("https://")) return [];
42+
if (prepared.kind === "plain" || !/https:\/\//i.test(content)) return [];
4343
const scan = scanMarkdown(prepared.content);
4444
if (scan.tooDeep) return [];
4545
const source = prepared.content;
46+
const delimiters = prepared.spoilerDelimiters;
4647
const excluded = new Set(exclude.map((url) => https(url) ?? url));
4748
const found: string[] = [];
48-
const add = (raw: string) => {
49-
const url = https(raw);
49+
const add = (raw: string | undefined) => {
50+
const url = raw && https(raw);
5051
if (url && !excluded.has(url) && !found.includes(url)) found.push(url);
5152
};
52-
const visit = (node: Node) => {
53+
const span = (node: Node) => [
54+
node.position?.start.offset,
55+
node.position?.end.offset,
56+
];
57+
// Spoilers pair `||` among one parent's text children, as remarkSpoilers does.
58+
const hiddenRanges = (children: readonly Node[]) => {
59+
const own = children.flatMap((child) => {
60+
const [start, end] = span(child);
61+
return child.type === "text" && start !== undefined && end !== undefined
62+
? delimiters.filter((at) => at >= start && at + 2 <= end)
63+
: [];
64+
});
65+
const ranges: [number, number][] = [];
66+
for (let i = 0; i + 1 < own.length; i += 2)
67+
ranges.push([own[i] ?? 0, (own[i + 1] ?? 0) + 2]);
68+
return ranges;
69+
};
70+
const visit = (node: Node, hidden: readonly [number, number][]) => {
5371
if (skipped.has(node.type)) return;
54-
if (node.type === "link") {
55-
const start = node.position?.start.offset;
56-
if (start !== undefined && source[start] !== "<" && node.url)
57-
add(node.url);
72+
const [start, end] = span(node);
73+
const inside = (at: number) =>
74+
hidden.some(([from, to]) => at >= from && at < to);
75+
if (node.type === "link" || node.type === "linkReference") {
76+
if (start === undefined || source[start] === "<" || inside(start)) return;
77+
add(
78+
node.type === "link"
79+
? node.url
80+
: scan.definitions.get(node.identifier?.toLowerCase() ?? ""),
81+
);
5882
return;
5983
}
60-
if (node.type === "text" && node.value)
61-
messageLinkParts(node.value, undefined, (start, end, url) => {
62-
if (node.value?.[start] !== "<" && end > start) add(url);
84+
if (node.type === "text") {
85+
if (start === undefined || end === undefined || !node.value) return;
86+
const text = hidden.length ? source.slice(start, end) : node.value;
87+
messageLinkParts(text, undefined, (from, to, url) => {
88+
if (text[from] !== "<" && to > from && !inside(start + from)) add(url);
6389
});
64-
for (const child of node.children ?? []) visit(child);
90+
return;
91+
}
92+
const children = node.children ?? [];
93+
const own = hiddenRanges(children);
94+
for (const child of children) visit(child, own);
6595
};
66-
visit(scan.tree as Node);
96+
visit(scan.tree as Node, []);
6797
return found;
6898
}

0 commit comments

Comments
 (0)