-
-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add netscape bookmark import/export #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
XFox111
wants to merge
2
commits into
main
Choose a base branch
from
bookmarks-import
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { useDialog } from "@/contexts/DialogProvider"; | ||
| import { Body1, Button, makeStyles, MessageBar, MessageBarBody, Subtitle2, tokens } from "@fluentui/react-components"; | ||
| import { ArrowDownload20Regular, ArrowUpload20Regular } from "@fluentui/react-icons"; | ||
| import importBookmarks from "../utils/importBookmarks"; | ||
| import exportBookmarks from "../utils/exportBookmarks"; | ||
|
|
||
| export default function BookmarksSection(): React.ReactElement | ||
| { | ||
| const cls = useStyles(); | ||
| const dialog = useDialog(); | ||
|
|
||
| const [importResult, setImportResult] = useState<number | null>(null); | ||
|
|
||
| const handleImport = (): void => | ||
| dialog.pushPrompt({ | ||
| title: i18n.t("features.netscape_bookmarks.import_dialog.title"), | ||
| confirmText: i18n.t("options_page.storage.import_prompt.proceed"), | ||
| onConfirm: () => importBookmarks().then(setImportResult), | ||
| content: ( | ||
| <Body1 as="p"> | ||
| { i18n.t("features.netscape_bookmarks.import_dialog.content") } | ||
| </Body1> | ||
| ) | ||
| }); | ||
|
|
||
| return ( | ||
| <div className={ cls.root }> | ||
| <Subtitle2>{ i18n.t("features.netscape_bookmarks.title") }</Subtitle2> | ||
|
|
||
| { importResult !== null && | ||
| <MessageBar intent={ importResult >= 0 ? "success" : "error" } layout="multiline"> | ||
| <MessageBarBody> | ||
| { importResult >= 0 ? | ||
| i18n.t("features.netscape_bookmarks.import_result.success", [importResult]) : | ||
| i18n.t("features.netscape_bookmarks.import_result.error") | ||
| } | ||
| </MessageBarBody> | ||
| </MessageBar> | ||
| } | ||
|
|
||
| <div className={ cls.buttons }> | ||
| <Button icon={ <ArrowDownload20Regular /> } onClick={ exportBookmarks }> | ||
| { i18n.t("features.netscape_bookmarks.export") } | ||
| </Button> | ||
| <Button icon={ <ArrowUpload20Regular /> } onClick={ handleImport }> | ||
| { i18n.t("features.netscape_bookmarks.import") } | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const useStyles = makeStyles({ | ||
| root: | ||
| { | ||
| display: "flex", | ||
| flexFlow: "column", | ||
| gap: tokens.spacingVerticalMNudge | ||
| }, | ||
| buttons: | ||
| { | ||
| display: "flex", | ||
| flexWrap: "wrap", | ||
| gap: tokens.spacingVerticalSNudge | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { CollectionItem, GraphicsStorage, GroupItem, TabItem } from "@/models/CollectionModels"; | ||
| import { Bookmark } from "node-bookmarks-parser/build/interfaces/bookmark"; | ||
|
|
||
| export default function convertBookmarks(bookmarks: Bookmark[]): [CollectionItem[], GraphicsStorage, number] | ||
| { | ||
| let count: number = 0; | ||
| const graphics: GraphicsStorage = {}; | ||
| const items: CollectionItem[] = []; | ||
| const untitled: CollectionItem = { | ||
| items: [], | ||
| timestamp: Date.now(), | ||
| type: "collection" | ||
| }; | ||
|
|
||
| for (const bookmark of bookmarks) | ||
| { | ||
| if (bookmark.type === "bookmark") | ||
| { | ||
| untitled.items.push(getTab(bookmark, graphics)); | ||
| count++; | ||
| } | ||
| else if (bookmark.type === "folder") | ||
| { | ||
| const collection: CollectionItem = getCollection(bookmark, graphics); | ||
| items.push(collection); | ||
| count += collection.items.reduce((acc, item) => | ||
| { | ||
| if (item.type === "tab") | ||
| return acc + 1; | ||
| else if (item.type === "group") | ||
| return acc + item.items.length; | ||
| return acc; | ||
| }, 0); | ||
| } | ||
| } | ||
|
|
||
| if (untitled.items.length > 0) | ||
| items.unshift(untitled); | ||
|
|
||
| return [items, graphics, count]; | ||
| } | ||
|
|
||
| function getTab(bookmark: Bookmark, graphics: GraphicsStorage): TabItem | ||
| { | ||
| if (bookmark.icon) | ||
| graphics[bookmark.url!] = { | ||
| icon: bookmark.icon | ||
| }; | ||
|
|
||
| return { | ||
| type: "tab", | ||
| url: bookmark.url!, | ||
| title: bookmark.title || bookmark.url! | ||
XFox111 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
|
|
||
| function getCollection(bookmark: Bookmark, graphics: GraphicsStorage): CollectionItem | ||
| { | ||
| const collection: CollectionItem = { | ||
| items: [], | ||
| title: bookmark.title, | ||
| timestamp: Date.now(), | ||
| type: "collection" | ||
| }; | ||
|
|
||
| if (bookmark.children) | ||
| for (const child of bookmark.children) | ||
| { | ||
| if (child.type === "bookmark") | ||
| collection.items.push(getTab(child, graphics)); | ||
| else if (child.type === "folder" && child.children) | ||
| collection.items.push(getGroup(child, graphics)); | ||
| } | ||
|
|
||
| return collection; | ||
| } | ||
|
|
||
| function getGroup(bookmark: Bookmark, graphics: GraphicsStorage): GroupItem | ||
| { | ||
| const group: GroupItem = { | ||
| items: [], | ||
| title: bookmark.title, | ||
| pinned: false, | ||
| type: "group", | ||
| color: getRandomColor() | ||
| }; | ||
|
|
||
| if (bookmark.children) | ||
| for (const child of bookmark.children) | ||
| { | ||
| if (child.type === "bookmark") | ||
| group.items.push(getTab(child, graphics)); | ||
| else if (child.type === "folder") | ||
| group.items.push(...getGroup(child, graphics).items); | ||
| } | ||
|
|
||
| return group; | ||
| } | ||
|
|
||
| function getRandomColor(): "blue" | "cyan" | "green" | "grey" | "orange" | "pink" | "purple" | "red" | "yellow" | ||
| { | ||
| const colors = ["blue", "cyan", "green", "grey", "orange", "pink", "purple", "red", "yellow"] as const; | ||
| return colors[Math.floor(Math.random() * colors.length)]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { getCollectionTitle } from "@/entrypoints/sidepanel/utils/getCollectionTitle"; | ||
| import { getCollections } from "@/features/collectionStorage"; | ||
| import { CollectionItem, GroupItem } from "@/models/CollectionModels"; | ||
|
|
||
| export default async function exportBookmarks(): Promise<void> | ||
| { | ||
| const [collections] = await getCollections(); | ||
| const lines: string[] = [ | ||
| "<!DOCTYPE NETSCAPE-Bookmark-file-1>", | ||
| "<!-- This is an automatically generated file.", | ||
| " It will be read and overwritten.", | ||
| " DO NOT EDIT! -->", | ||
| "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">", | ||
| "<TITLE>Bookmarks</TITLE>", | ||
| "<H1>Bookmarks</H1>", | ||
| "<DL><p>" | ||
| ]; | ||
|
|
||
| for (const collection of collections) | ||
| lines.push(...createFolder(collection)); | ||
|
|
||
| lines.push("</DL><p>"); | ||
|
|
||
| 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(`<DT><H3>${sanitizeString(title)}</H3>`); | ||
| lines.push("<DL><p>"); | ||
|
|
||
| for (const subItem of item.items) | ||
| { | ||
| if (subItem.type === "tab") | ||
| lines.push(`<DT><A HREF="${encodeURI(subItem.url).replace(/"/g, "%22")}">${sanitizeString(subItem.title || subItem.url)}</A>`); | ||
| else if (subItem.type === "group") | ||
| lines.push(...createFolder(subItem)); | ||
| } | ||
|
|
||
| lines.push("</DL><p>"); | ||
| return lines; | ||
| } | ||
|
|
||
| function sanitizeString(str: string): string | ||
| { | ||
| return str | ||
| .replace(/&/g, "&") | ||
| .replace(/</g, "<") | ||
| .replace(/>/g, ">") | ||
| .replace(/"/g, """); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.