9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
import { CliError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { wikiFetch } from './utils.js';
|
|
cli({
|
|
site: 'wikipedia',
|
|
name: 'search',
|
|
access: 'read',
|
|
description: 'Search Wikipedia articles',
|
|
strategy: Strategy.PUBLIC,
|
|
browser: false,
|
|
args: [
|
|
{ name: 'query', positional: true, required: true, help: 'Search keyword' },
|
|
{ name: 'limit', type: 'int', default: 10, help: 'Max results' },
|
|
{ name: 'lang', default: 'en', help: 'Language code (e.g. en, zh, ja)' },
|
|
],
|
|
columns: ['title', 'snippet', 'url'],
|
|
func: async (args) => {
|
|
const limit = Math.max(1, Math.min(Number(args.limit), 50));
|
|
const lang = args.lang || 'en';
|
|
const q = encodeURIComponent(args.query);
|
|
const data = (await wikiFetch(lang, `/w/api.php?action=query&list=search&srsearch=${q}&srlimit=${limit}&format=json&utf8=1`));
|
|
const results = data?.query?.search;
|
|
if (!results?.length)
|
|
throw new CliError('NOT_FOUND', 'No articles found', 'Try a different keyword');
|
|
return results.map((r) => ({
|
|
title: r.title,
|
|
snippet: r.snippet.replace(/<[^>]+>/g, '').slice(0, 120),
|
|
url: `https://${lang}.wikipedia.org/wiki/${encodeURIComponent(r.title.replace(/ /g, '_'))}`,
|
|
}));
|
|
},
|
|
});
|