Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps-dev): bump @types/showdown from 2.0.3 to 2.0.4 #2200

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
164 changes: 79 additions & 85 deletions lib/middleware/template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { art, json } = require('@/utils/render');
const { art, json, rss3_ums } = require('@/utils/render');
const path = require('path');
const config = require('@/config').value;
const typeRegex = /\.(atom|rss|debug\.json|json|\d+\.debug\.html)$/;
const typeRegex = /\.(atom|rss|ums|debug\.json|json|\d+\.debug\.html)$/;
const { collapseWhitespace, convertDateToISO8601 } = require('@/utils/common-utils');

module.exports = async (ctx, next) => {
Expand All @@ -16,111 +16,105 @@ module.exports = async (ctx, next) => {

const outputType = ctx.state.type[1] || 'rss';

if (outputType === 'debug.json' && config.debugInfo) {
ctx.set({
'Content-Type': 'application/json; charset=UTF-8',
});
if (ctx.state.json) {
ctx.body = JSON.stringify(ctx.state.json, null, 4);
} else {
ctx.body = JSON.stringify({ message: 'plugin does not set debug json' });
}
}
if (outputType.endsWith('.debug.html') && config.debugInfo) {
ctx.set({
'Content-Type': 'text/html; charset=UTF-8',
});

const index = parseInt(outputType.match(/(\d+)\.debug\.html$/)[1]);
if (!(ctx.state.data && ctx.state.data.item && ctx.state.data.item[index])) {
ctx.body = `ctx.state.data.item[${index}] not found`;
} else {
ctx.body = ctx.state.data.item[index].description;
// only enable when debugInfo=true
if (config.debugInfo) {
if (outputType === 'debug.json') {
ctx.set({
'Content-Type': 'application/json; charset=UTF-8',
});
if (ctx.state.json) {
ctx.body = JSON.stringify(ctx.state.json, null, 4);
} else {
ctx.body = JSON.stringify({ message: 'plugin does not set debug json' });
}
}
}

if (outputType === 'json') {
ctx.set({ 'Content-Type': 'application/feed+json; charset=UTF-8' });
if (outputType.endsWith('.debug.html')) {
ctx.set({
'Content-Type': 'text/html; charset=UTF-8',
});

const index = parseInt(outputType.match(/(\d+)\.debug\.html$/)[1]);
if (!(ctx.state.data && ctx.state.data.item && ctx.state.data.item[index])) {
ctx.body = `ctx.state.data.item[${index}] not found`;
} else {
ctx.body = ctx.state.data.item[index].description;
}
}
}

if (!ctx.body) {
let template;

switch (outputType) {
case 'atom':
template = path.resolve(__dirname, '../views/atom.art');
break;
case 'rss':
template = path.resolve(__dirname, '../views/rss.art');
break;
default:
template = path.resolve(__dirname, '../views/rss.art');
break;
}
const templateName = outputType === 'atom' ? 'atom.art' : 'rss.art';
const template = path.resolve(__dirname, `../views/${templateName}`);

if (ctx.state.data) {
ctx.state.data.title = collapseWhitespace(ctx.state.data.title);
ctx.state.data.subtitle = collapseWhitespace(ctx.state.data.subtitle);
ctx.state.data.author = collapseWhitespace(ctx.state.data.author);

ctx.state.data.item &&
ctx.state.data.item.forEach((item) => {
if (item.title) {
item.title = collapseWhitespace(item.title);
// trim title length
for (let length = 0, i = 0; i < item.title.length; i++) {
length += Buffer.from(item.title[i]).length !== 1 ? 2 : 1;
if (length > config.titleLengthLimit) {
item.title = `${item.title.slice(0, i)}...`;
break;
}
}
const collapseWhitespaceForProperties = (properties, obj) => {
properties.forEach((prop) => {
if (obj[prop]) {
obj[prop] = collapseWhitespace(obj[prop]);
}

if (typeof item.author === 'string') {
item.author = collapseWhitespace(item.author);
} else if (typeof item.author === 'object' && item.author !== null) {
for (const a of item.author) {
a.name = collapseWhitespace(a.name);
}
if (outputType !== 'json') {
item.author = item.author.map((a) => a.name).join(', ');
});
};

collapseWhitespaceForProperties(['title', 'subtitle', 'author'], ctx.state.data);

ctx.state.data.item?.forEach((item) => {
if (item.title) {
item.title = collapseWhitespace(item.title);
// trim title length
for (let length = 0, i = 0; i < item.title.length; i++) {
length += Buffer.from(item.title[i]).length !== 1 ? 2 : 1;
if (length > config.titleLengthLimit) {
item.title = `${item.title.slice(0, i)}...`;
break;
}
}

if (item.itunes_duration && ((typeof item.itunes_duration === 'string' && item.itunes_duration.indexOf(':') === -1) || (typeof item.itunes_duration === 'number' && !isNaN(item.itunes_duration)))) {
item.itunes_duration = +item.itunes_duration;
item.itunes_duration =
Math.floor(item.itunes_duration / 3600) + ':' + (Math.floor((item.itunes_duration % 3600) / 60) / 100).toFixed(2).slice(-2) + ':' + (((item.itunes_duration % 3600) % 60) / 100).toFixed(2).slice(-2);
}

if (outputType !== 'rss') {
item.pubDate = convertDateToISO8601(item.pubDate);
item.updated = convertDateToISO8601(item.updated);
}

if (typeof item.author === 'string') {
item.author = collapseWhitespace(item.author);
} else if (typeof item.author === 'object' && item.author !== null) {
item.author.forEach((a) => (a.name = collapseWhitespace(a.name)));
if (outputType !== 'json') {
item.author = item.author.map((a) => a.name).join(', ');
}
});
}

if (item.itunes_duration && ((typeof item.itunes_duration === 'string' && item.itunes_duration.indexOf(':') === -1) || (typeof item.itunes_duration === 'number' && !isNaN(item.itunes_duration)))) {
item.itunes_duration = +item.itunes_duration;
item.itunes_duration =
Math.floor(item.itunes_duration / 3600) + ':' + (Math.floor((item.itunes_duration % 3600) / 60) / 100).toFixed(2).slice(-2) + ':' + (((item.itunes_duration % 3600) % 60) / 100).toFixed(2).slice(-2);
}

if (outputType !== 'rss') {
item.pubDate = convertDateToISO8601(item.pubDate);
item.updated = convertDateToISO8601(item.updated);
}
});
}

const routeTtl = (config.cache.routeExpire / 60) | 0;

const currentDate = new Date();
const data = {
lastBuildDate: new Date().toUTCString(),
updated: new Date().toISOString(),
ttl: routeTtl,
lastBuildDate: currentDate.toUTCString(),
updated: currentDate.toISOString(),
ttl: (config.cache.routeExpire / 60) | 0,
atomlink: ctx.request.href,
...ctx.state.data,
};

if (config.isPackage) {
ctx.body = data;
return;
}
if (!template) {
return;
}
if (outputType !== 'json') {

if (outputType === 'ums') {
ctx.set({ 'Content-Type': 'application/json; charset=UTF-8' });
ctx.body = rss3_ums(data);
} else if (outputType === 'json') {
ctx.set({ 'Content-Type': 'application/feed+json; charset=UTF-8' });
ctx.body = json(data);
} else {
ctx.body = art(template, data);
return;
}
ctx.body = json(data);
}
};
2 changes: 2 additions & 0 deletions lib/utils/render.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const art = require('art-template');
const json = require('@/views/json');
const rss3_ums = require('@/views/rss3-ums');

// We may add more control over it later

module.exports = {
art,
json, // This should be used by RSSHub middleware only.
rss3_ums,
};
61 changes: 61 additions & 0 deletions lib/views/rss3-ums.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const dayjs = require('dayjs');

/**
* This function should be used by RSSHub middleware only.
* @param {object} data ctx.state.data
* @returns `JSON.stringify`-ed [UMS Result](https://docs.rss3.io/docs/unified-metadata-schemas)
*/

const rss3_ums = (data) => {
const network = 'RSS';
const tag = 'RSS';
const type = 'article';
const currentUnixTsp = dayjs().unix();
const umsResult = {
data: data.item.map((item) => {
const owner = getOwnershipFieldFromURL(item);
return {
owner,
id: item.link,
network,
from: owner,
to: owner,
tag,
type,
direction: 'out',
feeValue: '0',
actions: [
{
tag,
type,
platform: owner,
from: owner,
to: owner,
metadata: {
authors: typeof item.author === 'string' ? [{ name: item.author }] : item.author,
description: item.description,
pubDate: item.pubDate,
tags: typeof item.category === 'string' ? [item.category] : item.category,
title: item.title,
},
related_urls: [item.link],
},
],
timestamp: dayjs(item.updated).unix() || currentUnixTsp,
};
}),
};
return JSON.stringify(umsResult, null, 4);
};

// we treat the domain as the owner of the content
function getOwnershipFieldFromURL(item) {
try {
const urlObj = new URL(item.link);
return urlObj.hostname;
} catch (e) {
return item.link;
}
}

module.exports = rss3_ums;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@
"@types/pidusage": "2.0.4",
"@types/plist": "3.0.4",
"@types/request-promise-native": "1.0.20",
"@types/require-all": "3.0.5",
"@types/showdown": "2.0.3",
"@types/require-all": "3.0.6",
"@types/showdown": "2.0.4",
"@types/supertest": "2.0.15",
"@types/tiny-async-pool": "2.0.1",
"@types/tough-cookie": "4.0.4",
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading