This commit is contained in:
Cynthia Foxwell 2024-11-11 11:09:18 -07:00
parent 7f72b85e2b
commit 1c8d475362

View file

@ -120,6 +120,61 @@ async function signedFetch(url, options) {
return await fetch(url, options); return await fetch(url, options);
} }
// https://tenor.com/view/12766761978490409957
// https://github.com/bluesky-social/atproto/blob/main/packages/api/src/rich-text/unicode.ts
const encoder = new TextEncoder();
const decoder = new TextDecoder();
function utf16Slice(utf8, start, end) {
return decoder.decode(utf8.slice(start, end));
}
function processBlueskyFacets(str, facets) {
const utf16 = str;
const utf8 = encoder.encode(utf16);
const splitString = [];
let start = 0;
for (const facet of facets) {
splitString.push({text: utf16Slice(utf8, start, facet.index.byteStart)});
splitString.push({
text: utf16Slice(utf8, facet.index.byteStart, facet.index.byteEnd),
features: facet.features,
});
start = facet.index.byteEnd;
}
splitString.push({text: utf16Slice(utf8, start)});
for (const part of splitString) {
if (part.features)
for (const feature of part.features) {
switch (feature.$type) {
case "app.bsky.richtext.facet#link": {
if (part.text == feature.uri.replace(/^https?:\/\//, "")) {
part.text = feature.uri;
} else {
part.text = `[${part.text}](${feature.uri})`;
}
break;
}
case "app.bsky.richtext.facet#mention": {
part.text = `[${part.text}](https://bsky.app/profile/${feature.did})`;
break;
}
case "app.bsky.richtext.facet#tag": {
part.text = `[${part.text}](https://bsky.app/hashtag/${feature.tag})`;
break;
}
default:
break;
}
}
}
return splitString.map((part) => part.text).join("");
}
async function blueskyQuoteEmbed(quote) { async function blueskyQuoteEmbed(quote) {
const embeds = []; const embeds = [];
const videos = []; const videos = [];
@ -139,6 +194,10 @@ async function blueskyQuoteEmbed(quote) {
timestamp: quote.value.createdAt, timestamp: quote.value.createdAt,
}; };
if (quote.value.facets?.length > 0) {
mainEmbed.description = processBlueskyFacets(mainEmbed.description, quote.value.facets);
}
if (quote.embeds?.[0]) { if (quote.embeds?.[0]) {
const embed = quote.embeds[0]; const embed = quote.embeds[0];
switch (embed.$type) { switch (embed.$type) {
@ -239,47 +298,7 @@ async function bluesky(msg, url, spoiler = false) {
}; };
if (post.record.facets?.length > 0) { if (post.record.facets?.length > 0) {
const splitString = []; mainEmbed.description = processBlueskyFacets(mainEmbed.description, post.record.facets);
const chars = [...mainEmbed.description]; // emojis...
let start = 0;
for (const facet of post.record.facets) {
splitString.push({text: chars.slice(start, facet.index.byteStart).join("")});
splitString.push({
text: chars.slice(facet.index.byteStart, facet.index.byteEnd).join(""),
features: facet.features,
});
start = facet.index.byteEnd;
}
splitString.push({text: chars.slice(start).join("")});
for (const part of splitString) {
if (part.features)
for (const feature of part.features) {
switch (feature.$type) {
case "app.bsky.richtext.facet#link": {
if (part.text == feature.uri.replace(/^https?:\/\//, "")) {
part.text = feature.uri;
} else {
part.text = `[${part.text}](${feature.uri})`;
}
break;
}
case "app.bsky.richtext.facet#mention": {
part.text = `[${part.text}](https://bsky.app/profile/${feature.did})`;
break;
}
case "app.bsky.richtext.facet#tag": {
part.text = `[${part.text}](https://bsky.app/hashtag/${feature.tag})`;
break;
}
default:
break;
}
}
}
mainEmbed.description = splitString.map((part) => part.text).join("");
} }
if (data.thread.parent) { if (data.thread.parent) {