import { getCollectionTitle } from "@/entrypoints/sidepanel/utils/getCollectionTitle"; import { getCollections } from "@/features/collectionStorage"; import { CollectionItem, GroupItem } from "@/models/CollectionModels"; export default async function exportBookmarks(): Promise { const [collections] = await getCollections(); const lines: string[] = [ "", "", "", "Bookmarks", "

Bookmarks

", "

" ]; for (const collection of collections) lines.push(...createFolder(collection)); lines.push("

"); const data: string = lines.join("\n"); const blob: Blob = new Blob([data], { type: "text/html" }); const element: HTMLAnchorElement = document.createElement("a"); element.style.display = "none"; element.href = URL.createObjectURL(blob); element.setAttribute("download", "collections.html"); document.body.appendChild(element); element.click(); URL.revokeObjectURL(element.href); document.body.removeChild(element); } function createFolder(item: CollectionItem | GroupItem): string[] { const lines: string[] = []; const title: string = item.type === "collection" ? (item.title ?? getCollectionTitle(item)) : (item.pinned ? i18n.t("groups.pinned") : (item.title ?? "")); lines.push(`

${sanitizeString(title)}

`); lines.push("

"); for (const subItem of item.items) { if (subItem.type === "tab") lines.push(`

${sanitizeString(subItem.title || subItem.url)}`); else if (subItem.type === "group") lines.push(...createFolder(subItem)); } lines.push("

"); return lines; } function sanitizeString(str: string): string { return str .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); }