Files
jackwener__opencli/clis/linux-do/topic.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

53 lines
1.7 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { fetchLinuxDoJson } from './feed.js';
function toLocalTime(utcStr) {
if (!utcStr)
return '';
const date = new Date(utcStr);
return Number.isNaN(date.getTime()) ? utcStr : date.toLocaleString();
}
function strip(html) {
return (html || '')
.replace(/<br\s*\/?>/gi, ' ')
.replace(/<\/(p|div|li|blockquote|h[1-6])>/gi, ' ')
.replace(/<[^>]+>/g, '')
.replace(/&nbsp;/g, ' ')
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#(?:(\d+)|x([0-9a-fA-F]+));/g, (_, dec, hex) => {
try {
return String.fromCodePoint(dec !== undefined ? Number(dec) : parseInt(hex, 16));
}
catch {
return '';
}
})
.replace(/\s+/g, ' ')
.trim();
}
cli({
site: 'linux-do',
name: 'topic',
description: 'linux.do 帖子首页摘要和回复(首屏)',
domain: 'linux.do',
strategy: Strategy.COOKIE,
browser: true,
args: [
{ name: 'id', type: 'int', required: true, positional: true, help: 'Topic ID' },
{ name: 'limit', type: 'int', default: 20, help: 'Number of posts' },
],
columns: ['author', 'content', 'likes', 'created_at'],
func: async (page, kwargs) => {
const data = await fetchLinuxDoJson(page, `/t/${kwargs.id}.json`);
const posts = (data?.post_stream?.posts || []);
return posts.slice(0, kwargs.limit).map((p) => ({
author: p.username,
content: strip(p.cooked).slice(0, 200),
likes: p.like_count,
created_at: toLocalTime(p.created_at),
}));
},
});