Files
boris/node_modules/applesauce-content/dist/nast/truncate.js
Gigi 5d53a827e0 feat: initialize markr nostr bookmark client
- Add project structure with TypeScript, React, and Vite
- Implement nostr authentication using browser extension (NIP-07)
- Add NIP-51 compliant bookmark fetching and display
- Create minimal UI with login and bookmark components
- Integrate applesauce-core and applesauce-react libraries
- Add responsive styling with dark/light mode support
- Include comprehensive README with setup instructions

This is a minimal MVP for a nostr bookmark client that allows users to
view their bookmarks according to NIP-51 specification.
2025-10-02 07:17:07 +02:00

49 lines
1.8 KiB
JavaScript

export function truncateContent(tree, maxLength = 256) {
let length = 0;
for (let i = 0; i < tree.children.length; i++) {
const node = tree.children[i];
switch (node.type) {
case "hashtag":
length += 1 + node.hashtag.length;
break;
case "mention":
// guess user names are about 10 long
length += 10;
break;
case "cashu":
length += node.raw.length;
break;
case "gallery":
length += node.links.reduce((t, l) => t + l.length, 0);
break;
case "link":
case "text":
length += node.value.length;
break;
case "emoji":
length += 1;
break;
}
if (length > maxLength) {
if (node.type === "text") {
const children = i > 0 ? tree.children.slice(0, i) : [];
const chunkLength = node.value.length - (length - maxLength);
// find the nearest newline
const newLines = node.value.matchAll(/\n/g);
for (const match of newLines) {
if (match.index && match.index > chunkLength) {
children.push({ type: "text", value: node.value.slice(0, match.index) });
return { ...tree, children, truncated: true };
}
}
// just cut the string
children.push({ type: "text", value: node.value.slice(0, maxLength - length) });
return { ...tree, children, truncated: true };
}
else
return { ...tree, children: tree.children.slice(0, i), truncated: true };
}
}
return tree;
}