Skip to content

Commit

Permalink
feat: View and edit bookmarks on Pinboard from the context menu (#150)
Browse files Browse the repository at this point in the history
This change also consolidates APIs for additional URLs relating to items (e.g., the Internet Archive URL).
  • Loading branch information
jbmorley authored Jul 18, 2021
1 parent d9a198f commit a83fa1e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/BookmarksCore/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension String {

// TODO: Update to throwing properties when adopting Swift 5.5 #142
// https://github.com/inseven/bookmarks/issues/142
func asUrl() throws -> URL {
public func asUrl() throws -> URL {
guard let url = URL(string: self) else {
throw BookmarksError.invalidURL(string: self)
}
Expand Down
10 changes: 6 additions & 4 deletions core/BookmarksCore/Extensions/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import Foundation

public extension URL {

var internetArchiveUrl: URL {
URL(string: "https://web.archive.org/web/*/")!.appendingPathComponent(self.absoluteString)
}

// TODO: Update to throwing properties when adopting Swift 5.5 #142
// https://github.com/inseven/bookmarks/issues/142
func asComponents() throws -> URLComponents {
Expand All @@ -35,4 +31,10 @@ public extension URL {
return components
}

func settingQueryItems(_ queryItems: [URLQueryItem]) throws -> URL {
var components = try asComponents()
components.queryItems = queryItems
return try components.asUrl()
}

}
2 changes: 1 addition & 1 deletion core/BookmarksCore/Extensions/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension URLComponents {

// TODO: Update to throwing properties when adopting Swift 5.5 #142
// https://github.com/inseven/bookmarks/issues/142
func asUrl() throws -> URL {
public func asUrl() throws -> URL {
guard let url = self.url else {
throw BookmarksError.invalidURL(components: self)
}
Expand Down
18 changes: 18 additions & 0 deletions core/BookmarksCore/Store/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,21 @@ extension Item: CustomStringConvertible {
public var description: String { "\(self.url.absoluteString) (\(self.title))" }

}

extension Item {

// TODO: Update to throwing properties when adopting Swift 5.5 #142
// https://github.com/inseven/bookmarks/issues/142
public func internetArchiveUrl() throws -> URL {
try "https://web.archive.org/web/*/".asUrl().appendingPathComponent(url.absoluteString)
}

// TODO: Update to throwing properties when adopting Swift 5.5 #142
// https://github.com/inseven/bookmarks/issues/142
public func pinboardUrl() throws -> URL {
try "https://pinboard.in/add".asUrl().settingQueryItems([
URLQueryItem(name: "url", value: url.absoluteString)
])
}

}
14 changes: 13 additions & 1 deletion macos/Bookmarks/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,19 @@ struct ContentView: View {
}
Divider()
Button("View on Internet Archive") {
NSWorkspace.shared.open(item.url.internetArchiveUrl)
do {
NSWorkspace.shared.open(try item.internetArchiveUrl())
} catch {
print("Failed to open on the Internet Archive with error \(error)")
}
}
Divider()
Button("Edit on Pinboard") {
do {
NSWorkspace.shared.open(try item.pinboardUrl())
} catch {
print("Failed to edit with error \(error)")
}
}
Divider()
Button("Copy") {
Expand Down

0 comments on commit a83fa1e

Please sign in to comment.