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.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
/**
|
|
* Wikipedia adapter utilities.
|
|
*
|
|
* Uses the public MediaWiki REST API and Action API — no key required.
|
|
* REST API: https://en.wikipedia.org/api/rest_v1/
|
|
* Action API: https://en.wikipedia.org/w/api.php
|
|
*/
|
|
import { CliError } from '@jackwener/opencli/errors';
|
|
/** Maximum character length for article extract fields. */
|
|
export const EXTRACT_MAX_LEN = 300;
|
|
/** Maximum character length for short description fields. */
|
|
export const DESC_MAX_LEN = 80;
|
|
export async function wikiFetch(lang, path) {
|
|
const url = `https://${lang}.wikipedia.org${path}`;
|
|
const resp = await fetch(url, {
|
|
headers: { 'User-Agent': 'opencli/1.0 (https://github.com/jackwener/opencli)' },
|
|
});
|
|
if (!resp.ok) {
|
|
throw new CliError('FETCH_ERROR', `Wikipedia API HTTP ${resp.status}`, `Check your title or search term`);
|
|
}
|
|
return resp.json();
|
|
}
|
|
/** Map a WikiSummary API response to the standard output row. */
|
|
export function formatSummaryRow(data, lang) {
|
|
return {
|
|
title: data.title,
|
|
description: data.description ?? '-',
|
|
extract: (data.extract ?? '').slice(0, EXTRACT_MAX_LEN),
|
|
url: data.content_urls?.desktop?.page ?? `https://${lang}.wikipedia.org`,
|
|
};
|
|
}
|