Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/routes/rsshub/transform/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ConfigNotFoundError from '@/errors/types/config-not-found';
import type { DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/transform/html/:url/:routeParams',
Expand Down Expand Up @@ -43,6 +44,7 @@ Specify options (in the format of query string) in parameter \`routeParams\` par
| \`itemDescAttr\` | The attributes of \`descrption\` element as description | \`string\` | Element html |
| \`itemPubDate\` | The HTML elements as \`pubDate\` in \`item\` using CSS selector | \`string\` | \`item\` element |
| \`itemPubDateAttr\` | The attributes of \`pubDate\` element as pubDate | \`string\` | Element html |
| \`itemPubDateFmt\` | Date format string for \`day.js\` | \`string\` | |
| \`itemContent\` | The HTML elements as \`description\` in \`item\` using CSS selector ( in \`itemLink\` page for full content ) | \`string\` | |
| \`encoding\` | The encoding of the HTML content | \`string\` | utf-8 |

Expand Down Expand Up @@ -103,7 +105,8 @@ Specify options (in the format of query string) in parameter \`routeParams\` par
const desc = routeParams.get('itemDescAttr') ? descEle.attr(routeParams.get('itemDescAttr')) : descEle.html();

const pubDateEle = routeParams.get('itemPubDate') ? item.find(routeParams.get('itemPubDate')) : item;
const pubDate = routeParams.get('itemPubDateAttr') ? pubDateEle.attr(routeParams.get('itemPubDateAttr')) : pubDateEle.html();
const pubDateRaw = routeParams.get('itemPubDateAttr') ? pubDateEle.attr(routeParams.get('itemPubDateAttr')) : pubDateEle.html();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const pubDateRaw = routeParams.get('itemPubDateAttr') ? pubDateEle.attr(routeParams.get('itemPubDateAttr')) : pubDateEle.html();
const pubDateRaw = routeParams.get('itemPubDateAttr') ? pubDateEle.attr(routeParams.get('itemPubDateAttr')) : pubDateEle.text();

const pubDate = routeParams.get('itemPubDateFmt') ? parseDate(pubDateRaw, routeParams.get('itemPubDateFmt')) : pubDateRaw;

return {
title,
Expand Down
9 changes: 8 additions & 1 deletion lib/routes/rsshub/transform/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
import type { Route } from '@/types';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

function jsonGet(obj, attr) {
if (typeof attr !== 'string') {
Expand Down Expand Up @@ -49,6 +50,8 @@ export const route: Route = {
| \`itemLinkPrefix\` | Optional Prefix for \`itemLink\` value | \`string\` | None |
| \`itemDesc\` | The JSON Path as \`description\` in \`item\` | \`string\` | None |
| \`itemPubDate\` | The JSON Path as \`pubDate\` in \`item\` | \`string\` | None |
| \`itemPubDateFmt\` | Date format string for \`day.js\` | \`string\` | None |


::: tip
JSON Path only supports format like \`a.b.c\`. if you need to access arrays, like \`a[0].b\`, you can write it as \`a.0.b\`.
Expand Down Expand Up @@ -103,11 +106,15 @@ async function handler(ctx) {
if (link && !link.startsWith('http')) {
link = `${new URL(url).origin}${link}`;
}

const pubDateRaw = routeParams.get('itemPubDate') ? jsonGet(item, routeParams.get('itemPubDate')) : '';
const pubDate = routeParams.get('itemPubDateFmt') ? parseDate(pubDateRaw, routeParams.get('itemPubDateFmt')) : pubDateRaw;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also check if pubDateRaw is truthy since parseDate('') simply returns the current time and entry should not return a fake pubDate

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I implemented it with a simple logic or


return {
title: jsonGet(item, routeParams.get('itemTitle')),
link,
description: routeParams.get('itemDesc') ? jsonGet(item, routeParams.get('itemDesc')) : '',
pubDate: routeParams.get('itemPubDate') ? jsonGet(item, routeParams.get('itemPubDate')) : '',
pubDate,
};
});

Expand Down
Loading