Files
RSSHub/lib/routes/dedao/knowledge.tsx
T
dependabot[bot]andTony 417ab04f06 chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 (#22310)
* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 68.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix no-useless-coercion

* style: fix prefer-boolean-return

* style: fix no-useless-logical-operand

* style: fix prefer-single-replace

* style: fix consistent-conditional-object-spread

* style: fix no-useless-else

* style: fix prefer-unicode-code-point-escapes

* style: fix operator-assignment

* style: fix prefer-continue

* style: fix prefer-math-constants

* style: fix prefer-hoisting-branch-code

* style: fix prefer-array-slice

* style: fix consistent-class-member-order

* style: fix no-return-array-push

* style: fix prefer-promise-with-resolvers

* style: fix no-array-concat-in-loop

* style: fix no-loop-iterable-mutation

* style: fix no-invalid-argument-count

* style: fix no-duplicate-if-branches

* style: fix prefer-else-if

* style: fix no-unreadable-for-of-expression

* style: fix logical-assignment-operators

* style: fix default-export-style

* style: revert false positve in prefer-type-literal-last in #22274

* style: fix arrow-body-style

* style: add v68 recommended preset

* style: refactor GraphQL queries to use template literals for better readability

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-20 23:11:27 +08:00

125 lines
4.3 KiB
TypeScript

import { raw } from 'hono/html';
import { renderToString } from 'hono/jsx/dom/server';
import type { Route } from '@/types';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
const mentionPattern = /<\u{2267}\u{2746}>\{"name":"(.*?)","uid":"\d+","at":"1"\}<\/\u{2266}\u{2746}>/gu;
const formatNoteText = (text = '') => text.replaceAll('\n\n', '</p><p>').replaceAll(mentionPattern, ' @$1');
const extractImageUrl = (value?: string) => value?.match(/"url":"(.*?)"/)?.[1];
export const route: Route = {
path: '/knowledge/:topic?/:type?',
categories: ['new-media'],
example: '/dedao/knowledge',
parameters: { topic: '话题 id,可在对应话题页 URL 中找到', type: '分享类型,`true` 指精选,`false` 指最新,默认为精选' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['dedao.cn/knowledge/topic/:topic', 'dedao.cn/knowledge', 'dedao.cn/'],
},
],
name: '知识城邦',
maintainers: ['nczitzk'],
handler,
};
async function handler(ctx) {
const topic = ctx.req.param('topic') ?? '';
const type = /t|y/i.test(ctx.req.param('type') ?? 'true');
const count = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 100;
const rootUrl = 'https://www.dedao.cn';
const currentUrl = `${rootUrl}/knowledge${topic === '' ? '' : '/topic/' + topic}`;
const apiUrl = `${rootUrl}/pc/ledgers/${topic === '' ? 'notes/friends_timeline' : 'topic/notes/list'}`;
const detailUrl = `${rootUrl}/pc/ledgers/topic/detail`;
let title = '',
description = '';
if (topic !== '') {
const detailResponse = await got({
method: 'post',
url: detailUrl,
json: {
incr_view_count: false,
topic_id_hazy: topic,
},
});
title = detailResponse.data.c.name;
description = detailResponse.data.c.intro;
}
const response = await got({
method: 'post',
url: apiUrl,
json: {
count,
load_chain: true,
is_elected: type,
page_id: 0,
topic_id_hazy: topic,
version: 2,
},
});
const items = (topic === '' ? response.data.c.notes : response.data.c.note_detail_list).map((item) => ({
title: item.f_part.note,
author: item.f_part.nick_name,
link: `${rootUrl}/knowledge/note/${item.f_part.note_id_hazy}`,
pubDate: parseDate(item.f_part.time_desc, 'MM-DD'),
description: renderToString(
<>
{item.f_part.note ? <p>{raw(formatNoteText(item.f_part.note))}</p> : null}
{item.f_part.images ? (
<>
<br />
{item.f_part.images.map((image) => {
const imageUrl = extractImageUrl(image);
return imageUrl ? <img src={imageUrl} /> : null;
})}
</>
) : null}
{item.s_part ? (
<>
<br />
<p>
引用 {item.s_part.nick_name}
{item.s_part.v_info ? ` (${item.s_part.v_info})` : ''}:
</p>
<p>{raw(formatNoteText(item.s_part.note))}</p>
<a href={`${rootUrl}/knowledge/note/${item.s_part.note_id_hazy}`}>[查看原文]</a>
{item.s_part.images ? (
<>
<br />
{item.s_part.images.map((image) => {
const imageUrl = extractImageUrl(image);
return imageUrl ? <img src={imageUrl} /> : null;
})}
</>
) : null}
</>
) : null}
</>
),
}));
return {
title: `得到 - 知识城邦${title === '' ? '' : ' - ' + title}`,
link: currentUrl,
item: items,
description,
};
}