-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.ts
145 lines (138 loc) · 4.26 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
type InlineQueryResultArticle,
type InputTextMessageContent,
type MessageEntity,
} from "./deps.ts";
import { type Hit } from "./search.ts";
const MAX_DESC_LEN = 300; // max number of characters in result descriptions
const ZWSP = "\u200b"; // zero-width space character
const TYPE = { type: "text_link" } as const;
const IV_RHASH = "d32ab671cec0eb";
export interface QueryHit {
query: { query: string; label?: string };
hit: Hit;
}
export interface Link {
text?: string;
url: string;
}
/** Efficiently computes a 4-byte hash of an int32 array */
function hash(str: string): string {
const nums = Array.from(str).map((c) => c.codePointAt(0)!);
// Inspired by JDK7's hashCode with different primes for a better distribution
let hash = 17;
for (const n of nums) hash = ((hash << 5) + (hash << 2) + hash + n) >>> 0; // hash = 37 * hash + n
const b = 0xff; // mask for lowest byte
const bytes = [hash >>> 24, (hash >> 16) & b, (hash >> 8) & b, hash & b];
return new TextDecoder().decode(Uint8Array.from(bytes)); // turn bytes into string
}
export function renderSingle(h: Hit): InlineQueryResultArticle {
const link = getLink(h);
const title = link.text;
const message = renderSingleInputMessageContent(link);
return {
id: hash(h.objectID),
type: "article",
title,
description: `${title}: ${h.content ?? "Title matches the search query"}`
.substring(0, MAX_DESC_LEN),
input_message_content: message,
};
}
function renderSingleInputMessageContent({ text, url }: Required<Link>) {
const message_text = `${text}${ZWSP}\n\n${url}`;
const entities: MessageEntity[] = [
{ type: "bold", offset: 0, length: text.length },
{ ...TYPE, offset: text.length, length: 1, url: toIV(url) },
];
return { message_text, entities };
}
export function render(
texts: string[],
hits: QueryHit[],
): InlineQueryResultArticle {
const content = renderInputMessageContent(texts, hits);
return {
id: crypto.randomUUID(),
type: "article",
title: `Share ${hits.length === 1 ? "link" : `${hits.length} links`}`,
description: content.message_text.substring(0, MAX_DESC_LEN),
input_message_content: content,
};
}
export function renderInputMessageContent(
texts: string[],
hits: QueryHit[],
): InputTextMessageContent {
let message = "";
const entities: MessageEntity[] = [];
if (hits.length > 0) {
message += ZWSP;
entities.push({
...TYPE,
offset: 0,
length: 1,
url: toIV(hits[0].hit.url),
});
}
for (let i = 0; i < texts.length; i++) {
message += texts[i];
if (i < hits.length) {
const offset = message.length;
const { query, hit } = hits[i];
const text = query.label ?? getLabel(query.query, hit);
message += text;
entities.push({ ...TYPE, offset, length: text.length, url: hit.url });
}
}
return { message_text: message, entities };
}
export function renderNext(
existing: InputTextMessageContent,
{ query, hit }: QueryHit,
): InlineQueryResultArticle {
const { message_text, entities = [] } = existing;
const text = query.label ?? getLabel(query.query, hit);
const resultText = message_text + text;
return {
id: crypto.randomUUID(),
type: "article",
title: text,
description: resultText.substring(0, MAX_DESC_LEN),
input_message_content: {
message_text: resultText,
entities: [...entities, {
type: "text_link",
offset: message_text.length,
length: text.length,
url: hit.url,
}],
},
};
}
function getLabel(query: string, hit: Hit) {
if (query.endsWith("!")) {
return query.substring(0, query.length - 1);
} else if (query.endsWith("/")) {
return hit.url;
} else {
return getTitle(hit);
}
}
function getLink(hit: Hit, strip = !hit.hierarchy.lvl2): Required<Link> {
const text = getTitle(hit);
const url = strip ? stripAnchor(hit.url) : hit.url;
return { text, url };
}
function getTitle(hit: Hit) {
const h = hit.hierarchy;
const headers = [h.lvl1, h.lvl2, h.lvl3, h.lvl4, h.lvl5, h.lvl6];
return headers.filter((t) => !!t).join(" / ");
}
function stripAnchor(url: string) {
const index = url.lastIndexOf("#");
return index > 0 ? url.substring(0, index) : url;
}
function toIV(url: string) {
return `https://t.me/iv?rhash=${IV_RHASH}&url=${url}`;
}