Skip to content

Commit

Permalink
Allow Epub generation from multiple bookmarks hoarder-app#295
Browse files Browse the repository at this point in the history
switched epub generator
added URL filtering
changed the behavior to only create TOC for more than 1 bookmark
added bulk edit operation + translations
  • Loading branch information
kamtschatka committed Nov 22, 2024
1 parent 5d7df8c commit 5330223
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 38 deletions.
20 changes: 18 additions & 2 deletions apps/web/app/api/epub/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getServerAuthSession } from "@/server/auth";
import epub, { Chapter } from "@epubkit/epub-gen-memory";
import epub, { Chapter } from "@kamtschatka/epub-gen-memory";
import { and, eq, inArray } from "drizzle-orm";

import { db } from "@hoarder/db";
Expand Down Expand Up @@ -50,9 +50,17 @@ export async function GET(request: Request) {
});

const title = getTitle(chapters);
// If there is only 1 bookmark, we can skip the table of contents
const tocInTOC = chapters.length > 1;

const generatedEpub = await epub(
{ title, ignoreFailedDownloads: true },
{
title,
ignoreFailedDownloads: true,
tocInTOC,
version: 3,
urlValidator,
},
chapters,
);

Expand All @@ -65,6 +73,14 @@ export async function GET(request: Request) {
});
}

function urlValidator(url: string): boolean {
const urlParsed = new URL(url);
if (urlParsed.protocol != "http:" && urlParsed.protocol != "https:") {
return true;
}
return ["localhost", "127.0.0.1", "0.0.0.0"].includes(urlParsed.hostname);
}

function createFilename(): string {
const date = new Date();
const year = date.getFullYear();
Expand Down
13 changes: 13 additions & 0 deletions apps/web/components/dashboard/BulkBookmarksAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useBulkActionsStore from "@/lib/bulkActions";
import { useTranslation } from "@/lib/i18n/client";
import {
CheckCheck,
Download,
FileDown,
Hash,
Link,
Expand Down Expand Up @@ -223,6 +224,18 @@ export default function BulkBookmarksAction() {
isPending: recrawlBookmarkMutator.isPending,
hidden: !isBulkEditEnabled,
},
{
name: t("actions.download_epub"),
icon: (
<a
href={`/api/epub?${selectedBookmarks.map((bookmark) => "assetId=" + bookmark.id).join("&")}`}
>
<Download size={18} />
</a>
),
isPending: false,
hidden: !isBulkEditEnabled,
},
{
name: t("actions.refresh"),
icon: <RotateCw size={18} />,
Expand Down
42 changes: 21 additions & 21 deletions apps/web/components/dashboard/bookmarks/BookmarkOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +188,28 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
)}

{bookmark.content.type === BookmarkTypes.LINK && (
<>
<DropdownMenuItem
onClick={() => {
navigator.clipboard.writeText(
(bookmark.content as ZBookmarkedLink).url,
);
toast({
description: t("toasts.bookmarks.clipboard_copied"),
});
}}
>
<Link className="mr-2 size-4" />
<span>{t("actions.copy_link")}</span>
</DropdownMenuItem>
<>
<DropdownMenuItem
onClick={() => {
navigator.clipboard.writeText(
(bookmark.content as ZBookmarkedLink).url,
);
toast({
description: t("toasts.bookmarks.clipboard_copied"),
});
}}
>
<Link className="mr-2 size-4" />
<span>{t("actions.copy_link")}</span>
</DropdownMenuItem>

<DropdownMenuItem>
<Download className="mr-2 size-4"></Download>
<a href={`/api/epub?assetId=${bookmark.id}`}>
Download as EPUB
</a>
</DropdownMenuItem>
</>
<DropdownMenuItem>
<Download className="mr-2 size-4"></Download>
<a href={`/api/epub?assetId=${bookmark.id}`}>
{t("actions.download_epub")}
</a>
</DropdownMenuItem>
</>
)}
<DropdownMenuItem onClick={() => setTagModalIsOpen(true)}>
<Tags className="mr-2 size-4" />
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "Löschen",
"refresh": "Aktualisieren",
"download_full_page_archive": "Vollständiges Seitenarchiv herunterladen",
"download_epub": "EPUB herunterladen",
"edit_tags": "Tags bearbeiten",
"add_to_list": "Zur Liste hinzufügen",
"select_all": "Alle auswählen",
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "Delete",
"refresh": "Refresh",
"download_full_page_archive": "Download Full Page Archive",
"download_epub": "Download EPUB",
"edit_tags": "Edit Tags",
"add_to_list": "Add to List",
"select_all": "Select All",
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "Supprimer",
"refresh": "Rafraîchir",
"download_full_page_archive": "Télécharger l'archive de la page complète",
"download_epub": "Télécharger EPUB",
"edit_tags": "Modifier les tags",
"add_to_list": "Ajouter à la liste",
"select_all": "Tout sélectionner",
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "删除",
"refresh": "刷新",
"download_full_page_archive": "下载完整页面归档",
"download_epub": "下载EPUB",
"edit_tags": "编辑标签",
"add_to_list": "添加到列表",
"select_all": "全选",
Expand Down
3 changes: 1 addition & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"@auth/drizzle-adapter": "^1.4.2",
"@emoji-mart/data": "^1.1.2",
"@emoji-mart/react": "^1.1.1",
"@epubkit/epub-gen-memory": "1.0.10-aplha.8",
"@hoarder/db": "workspace:^0.1.0",
"@hoarder/shared": "workspace:^0.1.0",
"@hoarder/shared-react": "workspace:^0.1.0",
"@hoarder/trpc": "workspace:^0.1.0",
"@hookform/resolvers": "^3.3.4",
"@kamtschatka/epub-gen-memory": "^1.0.0",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
Expand Down Expand Up @@ -51,7 +51,6 @@
"csv-parse": "^5.5.6",
"dayjs": "^1.11.10",
"drizzle-orm": "^0.33.0",
"epub-gen-memory": "^1.0.10",
"fastest-levenshtein": "^1.0.16",
"i18next": "^23.16.5",
"i18next-resources-to-backend": "^1.2.1",
Expand Down
Loading

0 comments on commit 5330223

Please sign in to comment.