Files
jackwener__opencli/clis/medium/utils.ts
jackwener 314bc6f231 refactor: simplify core modules — remove root shims, consolidate error classification, streamline cascade/interceptor, clean up synthesize
1. Remove root-level shim files (errors.ts, logger.ts, registry.ts, types.ts, utils.ts, launcher.ts) — update all ~840 adapter imports to reference src/ directly
2. Consolidate interceptor: reuse shared DISGUISE_FN in tap interceptor instead of reimplementing
3. Unify error classification: single ClassifiedError type with icon/exitCode/hint lookup table, eliminating duplicated pattern matching between resolveExitCode and renderError
4. Simplify cascade probe: replace repetitive switch cases with PROBE_OPTIONS lookup map
12. Clean up synthesize.ts: remove deprecated snake_case field aliases (recommended_args, recommended_columns, recommendedColumnsLegacy) and unnecessary constant aliases

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 03:06:47 +08:00

85 lines
3.1 KiB
TypeScript

import { CommandExecutionError } from '../../src/errors.js';
import type { IPage } from '../../src/types.js';
export function buildMediumTagUrl(topic?: string): string {
return topic ? `https://medium.com/tag/${encodeURIComponent(topic)}` : 'https://medium.com/tag/technology';
}
export function buildMediumSearchUrl(keyword: string): string {
return `https://medium.com/search?q=${encodeURIComponent(keyword)}`;
}
export function buildMediumUserUrl(username: string): string {
return username.startsWith('@') ? `https://medium.com/${username}` : `https://medium.com/@${username}`;
}
export async function loadMediumPosts(page: IPage, url: string, limit: number): Promise<any[]> {
if (!page) throw new CommandExecutionError('Browser session required for medium posts');
await page.goto(url);
await page.wait({ selector: 'article', timeout: 5 });
const data = await page.evaluate(`
(async () => {
await new Promise((resolve) => setTimeout(resolve, 3000));
const limit = ${Math.max(1, Math.min(limit, 50))};
const normalize = (value) => (value || '').replace(/\\s+/g, ' ').trim();
const posts = [];
const seen = new Set();
for (const article of Array.from(document.querySelectorAll('article'))) {
try {
const titleEl = article.querySelector('h2, h3, h1');
const title = normalize(titleEl?.textContent);
if (!title) continue;
const linkEl = titleEl?.closest('a') || article.querySelector('a[href*="/@"], a[href*="/p/"]');
let url = linkEl?.getAttribute('href') || '';
if (!url) continue;
if (!url.startsWith('http')) url = 'https://medium.com' + url;
if (seen.has(url)) continue;
const author = normalize(
Array.from(article.querySelectorAll('a[href^="/@"]'))
.map((node) => normalize(node.textContent))
.find((text) => text && text !== title),
);
const allText = normalize(article.textContent);
const dateEl = article.querySelector('time');
const date = normalize(dateEl?.textContent) ||
dateEl?.getAttribute('datetime') ||
allText.match(/\\b(?:[A-Z][a-z]{2}\\s+\\d{1,2}|\\d+[dhmw]\\s+ago)\\b/)?.[0] ||
'';
const readTime = allText.match(/(\\d+)\\s*min\\s*read/i)?.[0] || '';
const claps = allText.match(/\\b(\\d+(?:\\.\\d+)?[KkMm]?)\\s*claps?\\b/i)?.[1] || '';
const description = normalize(
Array.from(article.querySelectorAll('h3, p'))
.map((node) => normalize(node.textContent))
.find((text) => text && text !== title && text !== author && !/member-only story|response icon/i.test(text)),
);
seen.add(url);
posts.push({
rank: posts.length + 1,
title,
author,
date,
readTime,
claps,
description: description ? description.slice(0, 150) : '',
url,
});
if (posts.length >= limit) break;
} catch {}
}
return posts;
})()
`);
return Array.isArray(data) ? data : [];
}