← Complete Projects

阅读中文版本

Build a Tagged Bookmark Manager CLI

A command-line bookmark manager that saves URLs with tags, searches across title, URL, and tags, opens the best match, and persists everything to a JSON file.

beginner~4 hnodejsclisearchproductivity

Project goal

Build a CLI that stores bookmarks with a title, URL, and tags, supports search across all fields, filters by tag, and prints or opens a selected bookmark.

Final features

  • Add a bookmark with a title, URL, and one or more tags.
  • Search bookmarks case-insensitively across title, URL, and tags.
  • Filter the list by tag and sort by most recently added.
  • Open a bookmark in the default browser by id or search term.
  • Persist to a JSON file with atomic writes.

Architecture

A command parser maps argv to operations; a store persists bookmarks to JSON atomically; a search module implements scoring and filtering as pure functions; and an opener shells out to the platform browser command. Search terms are matched and ranked so the most relevant bookmark is easy to open by phrase.

Prerequisites

  • Node.js 20 LTS or later
  • Familiarity with JSON persistence and process spawning
  • Basic understanding of cross-platform differences

Dependencies

  • node >=20 — File system, child_process for opening, and the CLI entry point
  • vitest ^2.1.0 — Search ranking and store tests

Directory structure

bookmarks/
├── src/
│   ├── store.js
│   ├── search.js
│   ├── opener.js
│   └── cli.js
├── test/
│   ├── search.test.js
│   └── store.test.js
└── package.json

Phases

1. Atomic store

Persist the bookmark collection to a JSON file using a temp file and rename so a crash cannot corrupt it.

Files: src/store.js

import { readFile, writeFile, rename } from 'node:fs/promises';

export async function loadStore(path) {
  try { return JSON.parse(await readFile(path, 'utf8')); }
  catch (err) { if (err.code === 'ENOENT') return { bookmarks: [], nextId: 1 }; throw err; }
}

export async function saveStore(path, store) {
  const tmp = path + '.tmp';
  await writeFile(tmp, JSON.stringify(store, null, 2));
  await rename(tmp, path);
}

Expected behavior: A missing file loads an empty store, saving then loading round-trips, and the file always parses after a save.

2. Adding bookmarks

Validate and normalise a new bookmark, assign an id, and reject duplicates by URL.

Files: src/add.js

export function addBookmark(store, { title, url, tags = [] }, now = new Date()) {
  if (!/^https?:\/\//.test(url)) throw new Error('url must start with http:// or https://');
  if (store.bookmarks.some((b) => b.url === url)) throw new Error('bookmark already exists');
  const bookmark = {
    id: store.nextId,
    title: title.trim() || url,
    url,
    tags: [...new Set(tags)],
    createdAt: now.toISOString(),
  };
  store.bookmarks.push(bookmark);
  store.nextId += 1;
  return bookmark;
}

Expected behavior: A valid bookmark gets an id and deduplicated tags, a duplicate URL throws, and an empty title falls back to the URL.

3. Search and ranking

Rank bookmarks by weighted term matches across title, tags, and URL, with tag and recency tie-breakers.

Files: src/search.js

export function search(bookmarks, query, { tag } = {}) {
  const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
  return bookmarks
    .map((b) => {
      const title = b.title.toLowerCase();
      const url = b.url.toLowerCase();
      const tags = b.tags.map((t) => t.toLowerCase());
      let score = 0;
      for (const term of terms) {
        if (title.includes(term)) score += 3;
        if (tags.some((t) => t.includes(term))) score += 2;
        if (url.includes(term)) score += 1;
      }
      return { ...b, score };
    })
    .filter((b) => b.score > 0 && (!tag || b.tags.includes(tag)))
    .sort((a, b) => b.score - a.score || b.createdAt.localeCompare(a.createdAt));
}

Expected behavior: A title match scores three and outranks a URL-only match at one, and a tag filter removes non-matching bookmarks.

4. Browser opener

Open a URL in the platform default browser without shell interpolation of the URL.

Files: src/opener.js

import { spawn } from 'node:child_process';

export function openUrl(url, { platform = process.platform } = {}) {
  const command = platform === 'darwin' ? 'open' : platform === 'win32' ? 'cmd' : 'xdg-open';
  const args = platform === 'win32' ? ['/c', 'start', '', url] : [url];
  return new Promise((resolve, reject) => {
    const child = spawn(command, args, { stdio: 'ignore' });
    child.on('error', reject);
    child.on('exit', (code) => (code === 0 ? resolve(true) : reject(new Error('open failed: ' + code))));
  });
}

Expected behavior: The right command is chosen per platform, and the URL is passed as an argument rather than interpolated into a shell string.

5. CLI surface

Dispatch add, list, search, and open subcommands and print numbered results consistently.

Files: src/cli.js

import { loadStore, saveStore } from './store.js';
import { addBookmark } from './add.js';
import { search } from './search.js';
import { openUrl } from './opener.js';

const path = process.env.BM_FILE ?? './bookmarks.json';
const [command, ...rest] = process.argv.slice(2);
const store = await loadStore(path);

if (command === 'add') {
  const [url, ...tagArgs] = rest;
  const bookmark = addBookmark(store, { url, tags: tagArgs.filter((t) => t.startsWith('#')).map((t) => t.slice(1)) });
  await saveStore(path, store);
  console.log('added ' + bookmark.id);
} else if (command === 'search') {
  for (const bm of search(store.bookmarks, rest.join(' '))) console.log(bm.id + '  ' + bm.title);
} else if (command === 'open') {
  const [best] = search(store.bookmarks, rest.join(' '));
  if (!best) { console.error('no match'); process.exit(1); }
  await openUrl(best.url);
} else {
  console.error('usage: bm <add|list|search|open>');
  process.exit(1);
}

Expected behavior: Add prints the new id, search lists matching bookmarks with ids, and open launches the top match or exits one when nothing matches.

6. Tests and hardening

Cover the store, add validation, and search ranking, then fix the failures the tests expose.

Files: test/search.test.js

import { search } from '../src/search.js';

const data = [
  { id: 1, title: 'Node.js docs', url: 'https://nodejs.org', tags: ['dev'], createdAt: '2026-01-02' },
  { id: 2, title: 'Recipes', url: 'https://nodejs.org/cook', tags: ['food'], createdAt: '2026-01-01' },
];

test('title match outranks url-only match', () => {
  const hits = search(data, 'node');
  expect(hits[0].id).toBe(1);
});

test('tag filter narrows results', () => {
  expect(search(data, 'node', { tag: 'food' }).map((h) => h.id)).toEqual([2]);
});

Expected behavior: The search suite passes, tag filtering composes with text, and adding a duplicate URL throws before any write.

Testing

Run npx vitest. Store tests round-trip bookmarks through a temporary file and assert that atomic writes leave a parseable file. Search tests assert that a title match outranks a URL-only match, that tag filters combine with text, and that a query with no matches returns nothing. An opener test stubs the spawn call and asserts the chosen command per platform.

Troubleshooting

Opening a URL with spaces fails

Pass the URL as a single spawn argument rather than interpolating it into a shell command; argument arrays avoid quoting bugs and command injection.

Two people add the same URL

Reject duplicates by URL at add time and offer an update command to change tags on an existing bookmark; silent duplicates make search results confusing.

Search returns nothing for a partial word

Match with substring includes rather than exact equality so prefixes work; bookmarks are remembered by fragments, not full words.

Deployment

Install as a global CLI and keep the JSON file in a synced folder to share bookmarks across machines. Because the file is plain JSON, it is also easy to back up or edit by hand.

Known limitations

  • There is no cross-device sync beyond whatever syncs the JSON file itself.
  • Search is substring-based and does not stem words, so it will not match synonyms.

Project overview

A bookmark manager is a small tool with an outsized payoff: the search is what makes it worth keeping, so ranking by how well a term matches title, URL, and tags gets the attention it deserves.

Bookmarks persist as one JSON file written atomically, and the open command resolves a free-text query to the single best match so the common case is one keystroke away.

Search scoring is a pure function that weights title matches above URL and tag matches, which is what makes opening by phrase reliable:

export function search(bookmarks, query, { tag } = {}) {
  const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
  return bookmarks
    .map((b) => {
      const title = b.title.toLowerCase();
      const url = b.url.toLowerCase();
      const tags = b.tags.map((t) => t.toLowerCase());
      let score = 0;
      for (const term of terms) {
        if (title.includes(term)) score += 3;
        if (tags.some((t) => t.includes(term))) score += 2;
        if (url.includes(term)) score += 1;
      }
      return { ...b, score };
    })
    .filter((b) => b.score > 0 && (!tag || b.tags.includes(tag)))
    .sort((a, b) => b.score - a.score || b.createdAt.localeCompare(a.createdAt));
}

Because weights are explicit, a title match always beats a mere URL match, which matches how people remember their bookmarks.

Setup

The tool relies only on the standard library, including a platform-aware open command.

Dependency Version Why it is here
node >=20 File system, child_process for opening, and the CLI entry point
vitest ^2.1.0 Search ranking and store tests

Create the project and install:

mkdir bookmarks && cd bookmarks
npm init -y
npm install -D vitest@^2.1.0

Configure the CLI and tests in package.json:

{
  "name": "bookmarks",
  "type": "module",
  "scripts": {
    "bm": "node src/cli.js",
    "test": "vitest run"
  }
}

The store path comes from an environment variable so tests write to a temporary file and leave your real bookmarks untouched.

Phase 1: Atomic store

Persist the bookmark collection to a JSON file using a temp file and rename so a crash cannot corrupt it. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.

import { readFile, writeFile, rename } from 'node:fs/promises';

export async function loadStore(path) {
  try { return JSON.parse(await readFile(path, 'utf8')); }
  catch (err) { if (err.code === 'ENOENT') return { bookmarks: [], nextId: 1 }; throw err; }
}

export async function saveStore(path, store) {
  const tmp = path + '.tmp';
  await writeFile(tmp, JSON.stringify(store, null, 2));
  await rename(tmp, path);
}

Acceptance for this phase: A missing file loads an empty store, saving then loading round-trips, and the file always parses after a save.

Phase 2: Adding bookmarks

Validate and normalise a new bookmark, assign an id, and reject duplicates by URL. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.

export function addBookmark(store, { title, url, tags = [] }, now = new Date()) {
  if (!/^https?:\/\//.test(url)) throw new Error('url must start with http:// or https://');
  if (store.bookmarks.some((b) => b.url === url)) throw new Error('bookmark already exists');
  const bookmark = {
    id: store.nextId,
    title: title.trim() || url,
    url,
    tags: [...new Set(tags)],
    createdAt: now.toISOString(),
  };
  store.bookmarks.push(bookmark);
  store.nextId += 1;
  return bookmark;
}

Acceptance for this phase: A valid bookmark gets an id and deduplicated tags, a duplicate URL throws, and an empty title falls back to the URL.

Phase 3: Search and ranking

Rank bookmarks by weighted term matches across title, tags, and URL, with tag and recency tie-breakers. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.

export function search(bookmarks, query, { tag } = {}) {
  const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
  return bookmarks
    .map((b) => {
      const title = b.title.toLowerCase();
      const url = b.url.toLowerCase();
      const tags = b.tags.map((t) => t.toLowerCase());
      let score = 0;
      for (const term of terms) {
        if (title.includes(term)) score += 3;
        if (tags.some((t) => t.includes(term))) score += 2;
        if (url.includes(term)) score += 1;
      }
      return { ...b, score };
    })
    .filter((b) => b.score > 0 && (!tag || b.tags.includes(tag)))
    .sort((a, b) => b.score - a.score || b.createdAt.localeCompare(a.createdAt));
}

Acceptance for this phase: A title match scores three and outranks a URL-only match at one, and a tag filter removes non-matching bookmarks.

Phase 4: Browser opener

Open a URL in the platform default browser without shell interpolation of the URL. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.

import { spawn } from 'node:child_process';

export function openUrl(url, { platform = process.platform } = {}) {
  const command = platform === 'darwin' ? 'open' : platform === 'win32' ? 'cmd' : 'xdg-open';
  const args = platform === 'win32' ? ['/c', 'start', '', url] : [url];
  return new Promise((resolve, reject) => {
    const child = spawn(command, args, { stdio: 'ignore' });
    child.on('error', reject);
    child.on('exit', (code) => (code === 0 ? resolve(true) : reject(new Error('open failed: ' + code))));
  });
}

Acceptance for this phase: The right command is chosen per platform, and the URL is passed as an argument rather than interpolated into a shell string.

Phase 5: CLI surface

Dispatch add, list, search, and open subcommands and print numbered results consistently. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.

import { loadStore, saveStore } from './store.js';
import { addBookmark } from './add.js';
import { search } from './search.js';
import { openUrl } from './opener.js';

const path = process.env.BM_FILE ?? './bookmarks.json';
const [command, ...rest] = process.argv.slice(2);
const store = await loadStore(path);

if (command === 'add') {
  const [url, ...tagArgs] = rest;
  const bookmark = addBookmark(store, { url, tags: tagArgs.filter((t) => t.startsWith('#')).map((t) => t.slice(1)) });
  await saveStore(path, store);
  console.log('added ' + bookmark.id);
} else if (command === 'search') {
  for (const bm of search(store.bookmarks, rest.join(' '))) console.log(bm.id + '  ' + bm.title);
} else if (command === 'open') {
  const [best] = search(store.bookmarks, rest.join(' '));
  if (!best) { console.error('no match'); process.exit(1); }
  await openUrl(best.url);
} else {
  console.error('usage: bm <add|list|search|open>');
  process.exit(1);
}

Acceptance for this phase: Add prints the new id, search lists matching bookmarks with ids, and open launches the top match or exits one when nothing matches.

Phase 6: Tests and hardening

Cover the store, add validation, and search ranking, then fix the failures the tests expose. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.

import { search } from '../src/search.js';

const data = [
  { id: 1, title: 'Node.js docs', url: 'https://nodejs.org', tags: ['dev'], createdAt: '2026-01-02' },
  { id: 2, title: 'Recipes', url: 'https://nodejs.org/cook', tags: ['food'], createdAt: '2026-01-01' },
];

test('title match outranks url-only match', () => {
  const hits = search(data, 'node');
  expect(hits[0].id).toBe(1);
});

test('tag filter narrows results', () => {
  expect(search(data, 'node', { tag: 'food' }).map((h) => h.id)).toEqual([2]);
});

Acceptance for this phase: The search suite passes, tag filtering composes with text, and adding a duplicate URL throws before any write.

Testing strategy

Run npx vitest. Store tests round-trip bookmarks through a temporary file and assert that atomic writes leave a parseable file. Search tests assert that a title match outranks a URL-only match, that tag filters combine with text, and that a query with no matches returns nothing. An opener test stubs the spawn call and asserts the chosen command per platform.

The ranking test pins the behaviour users notice first: typing words from the title should surface that bookmark above one whose URL merely contains them.

Extending the project

Import from a browser export, duplicate detection by URL, and a tag rename command are all additions around the pure search and the store. A full-text index would replace the search function without touching persistence.