Skip to content

Commit

Permalink
🐛 No optimization for gif svg images
Browse files Browse the repository at this point in the history
  • Loading branch information
tw93 committed Apr 2, 2024
1 parent 1fd51b6 commit a9debee
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions rehype-customize-image-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { visit } from 'unist-util-visit';
export default function rehypeCustomizeImageSrc() {
return (tree) => {
visit(tree, 'raw', (node) => {
// Match <img> tags using regular expressions
// Match <img> tags excluding .gif and .svg images using regular expressions
const imgRegex =
/<img src="(https:\/\/cdn\.fliggy\.com\/[^"]+|https:\/\/gw\.alipayobjects\.com\/[^"]+)"/g;
/<img src="(https:\/\/cdn\.fliggy\.com\/[^"]+|https:\/\/gw\.alipayobjects\.com\/[^"]+)(?<!\.gif|\.svg|\.GIF|\.SVG)"/g;
// Replace the src attribute of the matched <img> tag
if (imgRegex.test(node.value)) {
node.value = node.value.replace(imgRegex, (match, p1) => {
return `<img src="${p1}?x-oss-process=image/resize,w_3600/format,webp"`;
// Check if the URL already contains query parameters
const separator = p1.includes('?') ? '&' : '?';
return `<img src="${p1}${separator}x-oss-process=image/resize,w_3600/format,webp"`;
});
}
});
Expand Down

0 comments on commit a9debee

Please sign in to comment.