-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This fixes isssue #6 when "/" exists in folder names.
- Loading branch information
1 parent
e9e3f52
commit 5a52dc2
Showing
3 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,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]); | ||
} | ||
} |
This file contains 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 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