Files
RSSHub/lib/routes/codefather/questions.ts
T
jxtan 4c4cbbad1a feat(route): add codefather (编程导航) routes (#20922)
* feat(route): add LinuxDo RSS routes

- Add /linux-do/posts route for latest posts
- Add /linux-do/top/:period route for trending topics with period filter
- Optimize /linux-do/latest route
- Update namespace documentation with descriptions
- Handle Zstandard compression by using Accept-Encoding: identity

* revert: remove linux-do routes (PR rejected)

* feat(route): add codefather (编程导航) routes

- Add posts route with hot/new/recommend sorting and category filter
- Add questions route with new/hot sorting
- Support for 编程导航 programming community

* fix: address all PR review comments

- Reorder path params: category before sort
- Remove config.trueUA (not needed)
- Use upvotes/comments fields instead of description
- Remove title slicing
- Change default sort to 'new'
- Remove title from description
- Remove content truncation
2026-01-22 11:52:22 +08:00

93 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
path: '/questions/:sort?',
categories: ['programming'],
example: '/codefather/questions',
parameters: {
sort: '排序方式,可选 `new`(最新)、`hot`(热门),默认为 `new`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.codefather.cn/qa', 'www.codefather.cn'],
target: '/questions',
},
],
name: '问答',
maintainers: ['JackyST0'],
handler,
description: '获取编程导航社区的问答内容,支持按最新、热门排序。',
};
async function handler(ctx) {
const sort = ctx.req.param('sort') || 'new';
const sortConfig = sort === 'hot' ? { field: 'favourNum', name: '热门' } : { field: 'createTime', name: '最新' };
const response = await ofetch('https://api.codefather.cn/api/qa/list/page/vo', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
current: 1,
pageSize: 20,
sortField: sortConfig.field,
sortOrder: 'descend',
},
});
if (response.code !== 0) {
throw new Error(`API error: ${response.message}`);
}
const records = response.data?.records || [];
const items = records.map((item: Record<string, unknown>) => {
const title = (item.title as string) || '无标题';
const content = (item.content as string) || '';
const user = (item.user as Record<string, unknown>) || {};
const tags = (item.tags as string[]) || [];
const bestComment = item.bestComment as Record<string, unknown> | undefined;
// Build description content
let description = `<div>${content.replaceAll('\n', '<br>')}</div>`;
// Add best answer
if (bestComment) {
const answerUser = (bestComment.user as Record<string, unknown>) || {};
description += '<hr><h4>💡 最佳回答</h4>';
description += `<p><strong>${answerUser.userName || '匿名'}</strong>:</p>`;
description += `<p>${(bestComment.plainTextDescription as string) || ''}</p>`;
}
return {
title,
link: `https://www.codefather.cn/qa/${item.id}`,
description,
pubDate: parseDate(item.createTime as number),
author: user.userName as string,
category: tags,
upvotes: item.thumbNum as number,
comments: item.commentNum as number,
};
});
return {
title: `编程导航 - ${sortConfig.name}问答`,
link: 'https://www.codefather.cn/qa',
description: `编程导航社区${sortConfig.name}问答`,
item: items,
};
}