Skip to content

Commit ba4f670

Browse files
committed
Add new file format v3
This fixes isssue #6 when "/" exists in folder names.
1 parent e9e3f52 commit ba4f670

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

bookmark/BookmarkFormatterV3.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Bookmark } from "./Bookmark.js";
2+
import { BuiltinBookmark } from "./BuiltinBookmark.js";
3+
4+
5+
class PathFormatter
6+
{
7+
encode(paths)
8+
{
9+
return paths.map((path) => path.replace(/\//g, "\\\/")).join("/"); // replace "/" with "\/" in folder names
10+
}
11+
12+
decode(url)
13+
{
14+
const paths = url.split(/(?<!\\)\//g); // split on "/", but not on escaped slashes "\/" in folder names
15+
return paths.map((path) => path.replace(/\\\//g, "/")); // replace "\/" with "/"
16+
}
17+
}
18+
19+
20+
export class BookmarkFormatterV3
21+
{
22+
#builtinBookmark = new BuiltinBookmark();
23+
#version = 3;
24+
25+
async init()
26+
{
27+
await this.#builtinBookmark.init();
28+
}
29+
30+
read(rawJson)
31+
{
32+
let bookmarks = [];
33+
for (let obj of rawJson)
34+
{
35+
let fmt = new PathFormatter();
36+
let path = fmt.decode(obj["path"]);
37+
this.#expandRootNodeIn(path);
38+
bookmarks.push(new Bookmark(
39+
obj["title"],
40+
obj["url"],
41+
path));
42+
}
43+
return bookmarks;
44+
}
45+
46+
write(bookmarks)
47+
{
48+
let rawJson = [];
49+
for (let bookmark of bookmarks)
50+
{
51+
this.#substituteRootNodeIn(bookmark.path);
52+
let fmt = new PathFormatter();
53+
rawJson.push({
54+
title: bookmark.title,
55+
url: bookmark.url,
56+
path: fmt.encode(bookmark.path)
57+
});
58+
}
59+
return rawJson;
60+
}
61+
62+
getVersion()
63+
{
64+
return this.#version;
65+
}
66+
67+
#expandRootNodeIn(path)
68+
{
69+
path[1] = this.#builtinBookmark.expand(path[1]);
70+
}
71+
72+
#substituteRootNodeIn(path)
73+
{
74+
path[1] = this.#builtinBookmark.substitute(path[1]);
75+
}
76+
}

bookmark/TabViewExport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BookmarkExporter } from "./BookmarkExporter.js";
22
import { BookmarkTreeExport } from "./BookmarkTreeExport.js";
33
import { BookmarkFile } from "./BookmarkFile.js";
4-
import { BookmarkFormatterV2 } from "./BookmarkFormatterV2.js";
4+
import { BookmarkFormatterV3 } from "./BookmarkFormatterV3.js";
55

66
export class TabViewExport
77
{
@@ -46,7 +46,7 @@ export class TabViewExport
4646
const exporter = new BookmarkExporter();
4747
exporter.setBookmarkTree(this.#bookmarkTree)
4848
exporter.setSelection(this.#tree.getSelectedBookmarksId());
49-
let formatter = new BookmarkFormatterV2();
49+
let formatter = new BookmarkFormatterV3();
5050
await formatter.init();
5151
let bookmarks = formatter.write(exporter.export());
5252
let fileWriter = new BookmarkFile();

bookmark/TabViewImport.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BookmarkFile } from "./BookmarkFile.js";
22
import { BookmarkFormatterV1 } from "./BookmarkFormatterV1.js";
33
import { BookmarkFormatterV2 } from "./BookmarkFormatterV2.js";
4+
import { BookmarkFormatterV3 } from "./BookmarkFormatterV3.js";
45
import { BookmarkImporter } from "./BookmarkImporter.js";
56
import { BookmarkTreeImport } from "./BookmarkTreeImport.js";
67

@@ -75,6 +76,7 @@ export class TabViewImport
7576
const formatter = {
7677
"1": new BookmarkFormatterV1(),
7778
"2": await this.#newBookmarkFormatterV2(),
79+
"3": await this.#newBookmarkFormatterV3(),
7880
};
7981
if (version in formatter)
8082
{
@@ -94,6 +96,13 @@ export class TabViewImport
9496
return formatter;
9597
}
9698

99+
async #newBookmarkFormatterV3()
100+
{
101+
const formatter = new BookmarkFormatterV3();
102+
await formatter.init();
103+
return formatter;
104+
}
105+
97106
#showImportedStatus(newBookmarks, existingBookmarks)
98107
{
99108
const type = "success";

0 commit comments

Comments
 (0)