Skip to content

Commit

Permalink
Add new file format v3
Browse files Browse the repository at this point in the history
This fixes isssue #6 when "/" exists in folder names.
  • Loading branch information
1nfiniteloop committed Jan 12, 2025
1 parent e9e3f52 commit 5a52dc2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
76 changes: 76 additions & 0 deletions bookmark/BookmarkFormatterV3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Bookmark } from "./Bookmark.js";
import { BuiltinBookmark } from "./BuiltinBookmark.js";


class PathFormatter
{
encode(paths)
{
return paths.map((path) => path.replace(/\//g, "\\\/")).join("/"); // replace "/" with "\/" in folder names
}

decode(url)
{
const paths = url.split(/(?<!\\)\//g); // split on "/", but not on escaped slashes "\/" in folder names
return paths.map((path) => path.replace(/\\\//g, "/")); // replace "\/" with "/"
}
}


export class BookmarkFormatterV3
{
#builtinBookmark = new BuiltinBookmark();
#version = 3;

async init()
{
await this.#builtinBookmark.init();
}

read(rawJson)
{
let bookmarks = [];
for (let obj of rawJson)
{
let fmt = new PathFormatter();
let path = fmt.decode(obj["path"]);
this.#expandRootNodeIn(path);
bookmarks.push(new Bookmark(
obj["title"],
obj["url"],
path));
}
return bookmarks;
}

write(bookmarks)
{
let rawJson = [];
for (let bookmark of bookmarks)
{
this.#substituteRootNodeIn(bookmark.path);
let fmt = new PathFormatter();
rawJson.push({
title: bookmark.title,
url: bookmark.url,
path: fmt.encode(bookmark.path)
});
}
return rawJson;
}

getVersion()
{
return this.#version;
}

#expandRootNodeIn(path)
{
path[1] = this.#builtinBookmark.expand(path[1]);
}

#substituteRootNodeIn(path)
{
path[1] = this.#builtinBookmark.substitute(path[1]);
}
}
4 changes: 2 additions & 2 deletions bookmark/TabViewExport.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BookmarkExporter } from "./BookmarkExporter.js";
import { BookmarkTreeExport } from "./BookmarkTreeExport.js";
import { BookmarkFile } from "./BookmarkFile.js";
import { BookmarkFormatterV2 } from "./BookmarkFormatterV2.js";
import { BookmarkFormatterV3 } from "./BookmarkFormatterV3.js";

export class TabViewExport
{
Expand Down Expand Up @@ -46,7 +46,7 @@ export class TabViewExport
const exporter = new BookmarkExporter();
exporter.setBookmarkTree(this.#bookmarkTree)
exporter.setSelection(this.#tree.getSelectedBookmarksId());
let formatter = new BookmarkFormatterV2();
let formatter = new BookmarkFormatterV3();
await formatter.init();
let bookmarks = formatter.write(exporter.export());
let fileWriter = new BookmarkFile();
Expand Down
9 changes: 9 additions & 0 deletions bookmark/TabViewImport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BookmarkFile } from "./BookmarkFile.js";
import { BookmarkFormatterV1 } from "./BookmarkFormatterV1.js";
import { BookmarkFormatterV2 } from "./BookmarkFormatterV2.js";
import { BookmarkFormatterV3 } from "./BookmarkFormatterV3.js";
import { BookmarkImporter } from "./BookmarkImporter.js";
import { BookmarkTreeImport } from "./BookmarkTreeImport.js";

Expand Down Expand Up @@ -75,6 +76,7 @@ export class TabViewImport
const formatter = {
"1": new BookmarkFormatterV1(),
"2": await this.#newBookmarkFormatterV2(),
"3": await this.#newBookmarkFormatterV3(),
};
if (version in formatter)
{
Expand All @@ -94,6 +96,13 @@ export class TabViewImport
return formatter;
}

async #newBookmarkFormatterV3()
{
const formatter = new BookmarkFormatterV3();
await formatter.init();
return formatter;
}

#showImportedStatus(newBookmarks, existingBookmarks)
{
const type = "success";
Expand Down

0 comments on commit 5a52dc2

Please sign in to comment.