feat: cleanup the invalid tags in feed xml.

This commit is contained in:
Yufan Sheng 2024-06-17 05:42:12 +08:00
parent 66f7e0fb21
commit 289c033736
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
7 changed files with 47 additions and 15 deletions

View File

@ -103,7 +103,6 @@ if (typeof comments !== 'undefined' && comments !== null) {
const rid = ridInput.value;
ridInput.value = '0';
if (rid !== '0') {
console.log({ rid: rid });
const children = comments.querySelector(`#atk-comment-${rid}`).querySelector('.children');
if (children !== null && children.querySelectorAll('li').length === 0) {
children.remove();

View File

@ -46,10 +46,7 @@ export const loadComments = async (
if (title !== null) {
params = { ...params, title: title };
}
const query = querystring.stringify(params);
console.log(query);
const data = await fetch(urlJoin(server, `/api/v2/comments?${query}`))
const data = await fetch(urlJoin(server, `/api/v2/comments?${querystring.stringify(params)}`))
.then((response) => response.json())
.catch((e) => {
console.log(e);
@ -158,8 +155,11 @@ const parseContent = async (content: string): Promise<string> => {
'li',
],
allowAttributes: {
img: ['src', 'width', 'height'],
a: ['rel', 'target'],
src: ['img'],
width: ['img'],
height: ['img'],
rel: ['a'],
target: ['a'],
},
allowComments: false,
}),

View File

@ -5,7 +5,7 @@ interface Props extends Image {
alt: string;
}
const { alt, ...meta } = Astro.props;
const { alt, src, width, height } = Astro.props;
---
<img src={meta.src} {alt} loading="lazy" width={meta.width} height={meta.height} style={blurStyle(meta)} />
<img {src} {alt} loading="lazy" {width} {height} style={blurStyle(Astro.props)} />

View File

@ -0,0 +1,11 @@
---
import type { Image } from '@/helpers/images';
interface Props extends Image {
alt: string;
}
const { alt, src } = Astro.props;
---
<img {src} {alt} />

View File

@ -1,5 +1,5 @@
---
import Image from '@/components/image/Image.astro';
import UnstyledImage from '@/components/image/UnstyledImage.astro';
import UnstyledMusicPlayer from '@/components/player/UnstyledMusicPlayer.astro';
import { posts } from '@/helpers/schema';
@ -16,4 +16,4 @@ if (!post) {
const { Content } = await post.render();
---
<Content components={{ MusicPlayer: UnstyledMusicPlayer, Image: Image }} />
<Content components={{ MusicPlayer: UnstyledMusicPlayer, Image: UnstyledImage }} />

View File

@ -6,4 +6,4 @@ interface Props extends MusicPlayerProps {}
const { url } = await resolveSong(Astro.props);
---
{url !== '' && <audio controls src={url} />}
{url !== '' && <audio src={url} />}

View File

@ -14,11 +14,28 @@ const cleanupContent = async (html: string) => {
// Make sure images are absolute, some readers are not smart enough to figure it out
if (node.name === 'img' && node.attributes.src?.startsWith('/')) {
node.attributes.src = urlJoin(import.meta.env.SITE, node.attributes.src);
const { src, alt } = node.attributes;
node.attributes = { src, alt };
}
// Make sure links are absolute, some readers are not smart enough to figure it out
if (node.name === 'a' && node.attributes.href?.startsWith('/')) {
node.attributes.href = urlJoin(import.meta.env.SITE, node.attributes.href);
if (node.name === 'a') {
if (node.attributes.href?.startsWith('/')) {
node.attributes.href = urlJoin(import.meta.env.SITE, node.attributes.href);
}
const { href, title } = node.attributes;
const attributes: Record<string, string> = { href };
if (typeof title !== 'undefined') {
attributes.title = title;
}
node.attributes = attributes;
// Remove inner links.
if (href.startsWith('#')) {
const code = node as unknown as TextNode;
code.type = TEXT_NODE;
code.value = '';
}
}
// Remove favicon images, some readers don't know they should be inline and it ends up being a broken image
@ -47,7 +64,12 @@ const cleanupContent = async (html: string) => {
'data-astro-source-file': ['*'],
'data-favicon': ['*'],
'data-image-component': ['img'],
style: ['*'],
'data-language': ['*'],
'data-footnotes': ['*'],
},
allowCustomElements: false,
allowComments: false,
}),
]);
};
@ -87,7 +109,7 @@ export const GET = async () => {
title: post.title,
pubDate: post.date,
description: post.summary,
author: options.author.name,
author: `${options.author.email} (${options.author.name})`,
content: contents.get(post.slug) ?? post.summary,
categories: [post.category, ...post.tags],
})),