* feat(route): add f95zone routes * fix(route): fix f95zone threads route for folo parse * fix(route): parse f95zone thread guid from [YYYY-MM-DD] in title * fix(route): use update date in thread title as pubDate * fix(route/f95zone): revert image URL processing * fix(route/f95zone): reenable image URL processing for telegram-bot * feat(route/f95zone): support post and thread route * feat(route/f95zone): optimize title * feat(route/f95zone): optimize image display * feat(route/f95zone): optimize image display * feat(route/f95zone): reoptimize img-elements processing * feat(route/f95zone): reset to the original route pattern * feat(route/f95zone): optimize routes * feat(route/f95zone): optimize content processing * feat(route/f95zone): reduce code * fix(route/f95zone): use parseDate for proper date handling * fix(route/f95zone): remove broken radar * fix(route/f95zone): remove invalid radar config and improve docs - Remove radar config from post route (postId is in URL hash, cannot be extracted) - Add empty radar array to prevent Folo parsing error - Add detailed URL format documentation for both routes - Explain how to convert browser URL to subscription URL * fix(route/f95zone): fix invalid example URL * fix(route/f95zone): use internally generated UA * fix(route/f95zone): only fetch the first and the last page * fix(route/f95zone): remove unnecessary sorting logic
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { load } from 'cheerio';
|
|
|
|
const ALLOWED_TAGS = new Set(['div', 'span', 'p', 'br', 'b', 'strong', 'i', 'em', 'u', 's', 'a', 'img', 'ul', 'ol', 'li', 'blockquote', 'hr', 'pre', 'code']);
|
|
const ALLOWED_ATTRS: Record<string, string[]> = {
|
|
a: ['href', 'target', 'rel'],
|
|
img: ['src', 'alt', 'title', 'style'],
|
|
div: ['style'],
|
|
span: ['style'],
|
|
};
|
|
|
|
export const processContent = (html: string): string => {
|
|
const $ = load(html);
|
|
|
|
// Process images: use original URLs, remove duplicates
|
|
const seenImages = new Set<string>();
|
|
$('img').each((_, el) => {
|
|
const $img = $(el);
|
|
const $parent = $img.parent('a');
|
|
let src = $parent.attr('href') || $img.attr('data-src') || $img.attr('src') || '';
|
|
src = src.replace('/thumb/', '/');
|
|
|
|
if (!src || src.startsWith('data:') || seenImages.has(src)) {
|
|
$img.remove();
|
|
} else {
|
|
seenImages.add(src);
|
|
$img.attr('src', src).removeAttr('data-src');
|
|
if ($parent.length) {
|
|
$parent.replaceWith($img);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Remove unwanted tags completely
|
|
$('button, script, style, noscript').remove();
|
|
|
|
// Remove disallowed tags but keep content
|
|
let changed = true;
|
|
while (changed) {
|
|
changed = false;
|
|
$('*').each((_, el) => {
|
|
if (el.type === 'tag' && !ALLOWED_TAGS.has(el.name)) {
|
|
$(el).replaceWith($(el).html() || '');
|
|
changed = true;
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
// Clean attributes
|
|
$('*').each((_, el) => {
|
|
if (el.type !== 'tag') {
|
|
return;
|
|
}
|
|
const allowed = new Set(ALLOWED_ATTRS[el.name] || []);
|
|
for (const attr of Object.keys(el.attribs || {})) {
|
|
if (!allowed.has(attr)) {
|
|
$(el).removeAttr(attr);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Remove empty divs
|
|
$('div')
|
|
.filter((_, el) => !$(el).html()?.trim())
|
|
.remove();
|
|
|
|
return $.html() || '';
|
|
};
|