Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
"dependencies": {
"@raycast/api": "^1.104.9",
"@raycast/utils": "^2.2.2",
"js-yaml": "^4.1.0"
"yaml": "^2.8.2"
},
"devDependencies": {
"@raycast/eslint-config": "^2.0.4",
"@types/js-yaml": "^4.0.9",
"@types/node": "22.13.10",
"@types/react": "19.0.10",
"eslint": "^9.22.0",
Expand Down
116 changes: 110 additions & 6 deletions src/search-bookmarks.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import fs from "fs";
import os from "os";
import { useState } from "react";

import { Action, ActionPanel, getPreferenceValues, List, showToast, Toast } from "@raycast/api";
import {
Action,
ActionPanel,
Form,
getPreferenceValues,
Icon,
List,
showToast,
Toast,
useNavigation,
} from "@raycast/api";
import { getFavicon } from "@raycast/utils";
import yaml from "js-yaml";
import YAML from "yaml";

interface Bookmark {
title: string;
Expand All @@ -21,11 +32,15 @@ function expandPath(filePath: string): string {
return filePath;
}

function readConfigFile(configPath: string): string {
const resolvedPath = expandPath(configPath);
return fs.readFileSync(resolvedPath, "utf-8");
}

function loadBookmarks(configPath: string): { bookmarks: Bookmark[]; error?: string } {
try {
const resolvedPath = expandPath(configPath);
const content = fs.readFileSync(resolvedPath, "utf-8");
const config = yaml.load(content) as Config;
const content = readConfigFile(configPath);
const config = YAML.parse(content) as Config;

if (!config || !Array.isArray(config.bookmarks)) {
return { bookmarks: [], error: "Invalid config: 'bookmarks' array not found" };
Expand All @@ -39,14 +54,97 @@ function loadBookmarks(configPath: string): { bookmarks: Bookmark[]; error?: str
}
}

function saveBookmarkEdit(
configPath: string,
oldBookmark: Bookmark,
newBookmark: Bookmark,
): { success: boolean; error?: string } {
try {
const resolvedPath = expandPath(configPath);
const content = readConfigFile(configPath);
const doc = YAML.parseDocument(content);

const bookmarks = doc.get("bookmarks") as YAML.YAMLSeq;
if (!bookmarks || !YAML.isSeq(bookmarks)) {
return { success: false, error: "Invalid config: 'bookmarks' array not found" };
}

let found = false;
for (const item of bookmarks.items) {
if (!YAML.isMap(item)) continue;
const title = item.get("title");
const url = item.get("url");
if (title === oldBookmark.title && url === oldBookmark.url) {
item.set("title", newBookmark.title);
item.set("url", newBookmark.url);
found = true;
break;
}
}

if (!found) {
return { success: false, error: "Could not find bookmark in config file" };
}

fs.writeFileSync(resolvedPath, doc.toString(), "utf-8");
return { success: true };
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
return { success: false, error: message };
}
}

function EditBookmarkForm(props: { bookmark: Bookmark; configPath: string; onEdit: () => void }) {
const { pop } = useNavigation();
const { bookmark, configPath, onEdit } = props;

async function handleSubmit(values: { title: string; url: string }) {
if (!values.title.trim() || !values.url.trim()) {
await showToast({ style: Toast.Style.Failure, title: "Title and URL are required" });
return;
}

const result = saveBookmarkEdit(configPath, bookmark, {
title: values.title.trim(),
url: values.url.trim(),
});

if (result.success) {
await showToast({ style: Toast.Style.Success, title: "Bookmark updated" });
onEdit();
pop();
} else {
await showToast({ style: Toast.Style.Failure, title: "Failed to update bookmark", message: result.error });
}
}

return (
<Form
actions={
<ActionPanel>
<Action.SubmitForm title="Save Changes" onSubmit={handleSubmit} />
</ActionPanel>
}
>
<Form.TextField id="title" title="Title" defaultValue={bookmark.title} />
<Form.TextField id="url" title="URL" defaultValue={bookmark.url} />
</Form>
);
}

export default function Command() {
const { configPath } = getPreferenceValues<Preferences>();
const { bookmarks, error } = loadBookmarks(configPath);

const [{ bookmarks, error }, setState] = useState(() => loadBookmarks(configPath));

if (error) {
void showToast({ style: Toast.Style.Failure, title: "Failed to load bookmarks", message: error });
}

function reloadBookmarks() {
setState(loadBookmarks(configPath));
}

return (
<List searchBarPlaceholder="Search bookmarks..." throttle>
<List.EmptyView
Expand All @@ -63,6 +161,12 @@ export default function Command() {
<ActionPanel>
<Action.OpenInBrowser url={bookmark.url} />
<Action.CopyToClipboard title="Copy URL" content={bookmark.url} />
<Action.Push
title="Edit Bookmark"
icon={Icon.Pencil}
shortcut={{ modifiers: ["cmd"], key: "e" }}
target={<EditBookmarkForm bookmark={bookmark} configPath={configPath} onEdit={reloadBookmarks} />}
/>
</ActionPanel>
}
/>
Expand Down
Loading