fix(images): Clean up image paths, compress large images, and fix openinary URLs
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
function slugify(text) {
|
||||
return text.toString().toLowerCase()
|
||||
.replace(/ç/g, 'c')
|
||||
.replace(/ğ/g, 'g')
|
||||
.replace(/ı/g, 'i')
|
||||
.replace(/ö/g, 'o')
|
||||
.replace(/ş/g, 's')
|
||||
.replace(/ü/g, 'u')
|
||||
// URL decode first just in case
|
||||
.replace(/%20/g, ' ')
|
||||
.replace(/%c3%b6/g, 'o') // ö
|
||||
.replace(/%c3%bc/g, 'u') // ü
|
||||
.replace(/%c3%a7/g, 'c') // ç
|
||||
.replace(/%c4%b1/g, 'i') // ı
|
||||
.replace(/%c5%9f/g, 's') // ş
|
||||
.replace(/%c4%9f/g, 'g') // ğ
|
||||
.replace(/[^a-z0-9.]/g, '-')
|
||||
.replace(/\-\-+/g, '-')
|
||||
.replace(/^-+/, '')
|
||||
.replace(/-+$/, '');
|
||||
}
|
||||
|
||||
function processPath(urlPath) {
|
||||
// decode url first
|
||||
let decoded = decodeURIComponent(urlPath);
|
||||
// split by /
|
||||
const parts = decoded.split('/');
|
||||
const newParts = parts.map((part, index) => {
|
||||
// don't slugify empty parts or fixed prefixes
|
||||
if (!part) return part;
|
||||
if (part === 'images' || part === 'https:' || part === '' || part === 'media.ayris.tech' || part === 'upload' || part === 'aydogan') return part;
|
||||
|
||||
let newName = slugify(path.parse(part).name);
|
||||
const ext = path.extname(part).toLowerCase();
|
||||
if (ext) {
|
||||
newName += ext;
|
||||
}
|
||||
return newName;
|
||||
});
|
||||
return newParts.join('/');
|
||||
}
|
||||
|
||||
const filesToProcess = [
|
||||
path.resolve(__dirname, '../lib/data.ts'),
|
||||
path.resolve(__dirname, '../lib/gallery_data.ts'),
|
||||
path.resolve(__dirname, '../components/Hero.tsx'),
|
||||
path.resolve(__dirname, '../components/ServicesPreview.tsx')
|
||||
];
|
||||
|
||||
for (const file of filesToProcess) {
|
||||
if (!fs.existsSync(file)) continue;
|
||||
let content = fs.readFileSync(file, 'utf8');
|
||||
|
||||
// Replace "/images/..."
|
||||
content = content.replace(/"(\/images\/[^"]+)"/g, (match, p1) => {
|
||||
return `"${processPath(p1)}"`;
|
||||
});
|
||||
|
||||
// Replace "https://media.ayris.tech/upload/aydogan/..."
|
||||
content = content.replace(/"(https:\/\/media\.ayris\.tech\/upload\/aydogan\/[^"]+)"/g, (match, p1) => {
|
||||
return `"${processPath(p1)}"`;
|
||||
});
|
||||
|
||||
fs.writeFileSync(file, content);
|
||||
console.log(`Updated ${file}`);
|
||||
}
|
||||
Reference in New Issue
Block a user