feat: add explorer sidebar, account menu and preview preferences
This commit is contained in:
1 parent
b87f8cb961
commit
fa1832dfa7
22 files changed
+929
-226
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseBookmarks } from "../bookmarks";
|
||||
describe("folder bookmarks", () => {
|
||||
it("handles unavailable, corrupt or unrelated storage", () => {
|
||||
for (const value of [null, "broken", "{}", '"string"'])
|
||||
expect(parseBookmarks(value)).toEqual([]);
|
||||
});
|
||||
it("keeps unique file locations and rejects external URLs", () => {
|
||||
expect(
|
||||
parseBookmarks(
|
||||
JSON.stringify([
|
||||
{ name: "Scans", url: "/files/scans/" },
|
||||
{ name: "Duplicate", url: "/files/scans/" },
|
||||
{ name: "Wrong", url: "https://example.com" },
|
||||
{ name: "Wrong", url: "/files-other/" },
|
||||
{ name: "Wrong", url: "/files/a/?secret=1" },
|
||||
null,
|
||||
])
|
||||
)
|
||||
).toEqual([{ name: "Scans", url: "/files/scans/" }]);
|
||||
});
|
||||
it("bounds the number and length of bookmarks", () => {
|
||||
const entries = parseBookmarks(
|
||||
JSON.stringify(
|
||||
Array.from({ length: 80 }, (_, i) => ({
|
||||
name: "a".repeat(500),
|
||||
url: `/files/${i}/`,
|
||||
}))
|
||||
)
|
||||
);
|
||||
expect(entries).toHaveLength(40);
|
||||
expect(entries[0].name).toHaveLength(255);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
export interface Bookmark {
|
||||
name: string;
|
||||
url: string;
|
||||
}
|
||||
export function parseBookmarks(value: string | null): Bookmark[] {
|
||||
try {
|
||||
const entries: unknown = JSON.parse(value || "[]");
|
||||
if (!Array.isArray(entries)) return [];
|
||||
const seen = new Set<string>();
|
||||
return entries
|
||||
.filter((entry): entry is Bookmark => {
|
||||
if (
|
||||
!entry ||
|
||||
typeof entry.name !== "string" ||
|
||||
typeof entry.url !== "string" ||
|
||||
!/^\/files(?:\/|$)/.test(entry.url) ||
|
||||
/[?#\r\n]/.test(entry.url) ||
|
||||
seen.has(entry.url)
|
||||
)
|
||||
return false;
|
||||
seen.add(entry.url);
|
||||
return true;
|
||||
})
|
||||
.slice(0, 40)
|
||||
.map((entry) => ({ name: entry.name.slice(0, 255), url: entry.url }));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user