feat(route/rsshub/transform): Add support for link prefixes (#17187)

This commit is contained in:
Arnaud Gissinger
2024-10-20 00:40:57 +08:00
committed by GitHub
parent 2fee4250fb
commit cbe0201ca2
+14 -8
View File
@@ -39,14 +39,15 @@ export const route: Route = {
handler,
description: `Specify options (in the format of query string) in parameter \`routeParams\` parameter to extract data from JSON.
| Key | Meaning | Accepted Values | Default |
| ------------- | ---------------------------------------- | --------------- | ------------------------------------------ |
| \`title\` | The title of the RSS | \`string\` | Extracted from home page of current domain |
| \`item\` | The JSON Path as \`item\` element | \`string\` | Entire JSON response |
| \`itemTitle\` | The JSON Path as \`title\` in \`item\` | \`string\` | None |
| \`itemLink\` | The JSON Path as \`link\` in \`item\` | \`string\` | None |
| \`itemDesc\` | The JSON Path as \`description\` in \`item\` | \`string\` | None |
| \`itemPubDate\` | The JSON Path as \`pubDate\` in \`item\` | \`string\` | None |
| Key | Meaning | Accepted Values | Default |
| ------------------ | -------------------------------------------- | ----------------- | ------------------------------------------ |
| \`title\` | The title of the RSS | \`string\` | Extracted from home page of current domain |
| \`item\` | The JSON Path as \`item\` element | \`string\` | Entire JSON response |
| \`itemTitle\` | The JSON Path as \`title\` in \`item\` | \`string\` | None |
| \`itemLink\` | The JSON Path as \`link\` in \`item\` | \`string\` | None |
| \`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 |
:::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\`.
@@ -92,6 +93,11 @@ async function handler(ctx) {
const items = jsonGet(response.data, routeParams.get('item')).map((item) => {
let link = jsonGet(item, routeParams.get('itemLink')).trim();
const linkPrefix = routeParams.get('itemLinkPrefix');
if (link && linkPrefix) {
link = `${linkPrefix}${link}`;
}
// 补全绝对链接
if (link && !link.startsWith('http')) {
link = `${new URL(url).origin}${link}`;