fedimbed.bluesky: i may be stupid

This commit is contained in:
Cynthia Foxwell 2024-11-11 10:37:47 -07:00
parent 315341088c
commit e4bcdf6f8a

View file

@ -239,39 +239,45 @@ async function bluesky(msg, url, spoiler = false) {
}; };
if (post.record.facets?.length > 0) { if (post.record.facets?.length > 0) {
const splitString = [];
let start = 0;
for (const facet of post.record.facets) { for (const facet of post.record.facets) {
let replacement = mainEmbed.description.substring(facet.index.byteStart, facet.index.byteEnd); splitString.push({text: mainEmbed.description.substring(start, facet.index.byteStart)});
const original = replacement;
for (const feature of facet.features) { splitString.push({
text: mainEmbed.description.substring(facet.index.byteStart, facet.index.byteEnd),
features: facet.features,
});
start = facet.index.byteEnd;
}
splitString.push({text: mainEmbed.description.substring(start)});
for (const part of splitString) {
for (const feature of part.features) {
switch (feature.$type) { switch (feature.$type) {
case "app.bsky.richtext.facet#link": { case "app.bsky.richtext.facet#link": {
if (replacement == feature.uri.replace(/^https?:\/\//, "")) { if (part.text == feature.uri.replace(/^https?:\/\//, "")) {
replacement = feature.uri; part.text = feature.uri;
} else { } else {
replacement = `[${replacement}](${feature.uri})`; part.text = `[${part.text}](${feature.uri})`;
} }
break; break;
} }
case "app.bsky.richtext.facet#mention": { case "app.bsky.richtext.facet#mention": {
replacement = `[${replacement}](https://bsky.app/profile/${feature.did})`; part.text = `[${part.text}](https://bsky.app/profile/${feature.did})`;
break; break;
} }
case "app.bsky.richtext.facet#tag": { case "app.bsky.richtext.facet#tag": {
replacement = `[${replacement}](https://bsky.app/hashtag/${feature.tag})`; part.text = `[${part.text}](https://bsky.app/hashtag/${feature.tag})`;
break; break;
} }
default: default:
break; break;
} }
} }
if (replacement !== original)
mainEmbed.description = `${mainEmbed.description.substring(
0,
facet.index.byteStart
)}${replacement}${mainEmbed.description.substring(facet.index.byteEnd)}`;
} }
mainEmbed.description = splitString.map((part) => part.text).join("");
} }
if (data.thread.parent) { if (data.thread.parent) {