feat: enhance image loading and sanitization

- Standardized image loading to lazy for consistency in modal previews
- Expanded allowed attributes for sanitization to support additional image properties
- Introduced an exclusive filter to exclude specific images by class during sanitization
This commit is contained in:
ccbikai
2024-08-10 14:00:53 +08:00
parent 270fac70d4
commit c6fe4742aa
2 changed files with 6 additions and 2 deletions

View File

@@ -41,7 +41,7 @@ function getImages($, item, { staticProxy, id, index, title }) {
<img src="${staticProxy + url}" alt="${title}" loading="${index > 15 ? 'eager' : 'lazy'}" />
</button>
<button class="image-preview-button modal" id="${popoverId}" popovertarget="${popoverId}" popovertargetaction="hide" popover>
<img class="modal-img" src="${staticProxy + url}" alt="${title}" loading="${index > 15 ? 'eager' : 'lazy'}" />
<img class="modal-img" src="${staticProxy + url}" alt="${title}" loading="lazy" />
</button>
`
})?.get()

View File

@@ -25,9 +25,13 @@ export async function GET(Astro) {
content: sanitizeHtml(item.content, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img', 'video', 'audio']),
allowedAttributes: {
...sanitizeHtml.defaults.allowedAttributes,
video: ['src', 'width', 'height', 'poster'],
audio: ['src', 'controls'],
img: ['src', 'width', 'height', 'loading'],
img: ['src', 'srcset', 'alt', 'title', 'width', 'height', 'loading', 'class'],
},
exclusiveFilter(frame) {
return frame.tag === 'img' && frame.attribs?.class?.includes('modal-img')
},
}),
})),