-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_html_entities.mjs
More file actions
71 lines (58 loc) · 1.82 KB
/
fix_html_entities.mjs
File metadata and controls
71 lines (58 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { createClient } from 'next-sanity';
import dotenv from 'dotenv';
dotenv.config({ path: '.env.local' });
const client = createClient({
projectId: '6r5yojda',
dataset: 'production',
apiVersion: '2024-01-01',
token: process.env.SANITY_API_TOKEN,
useCdn: false,
});
// Decode HTML entities
function decodeHtmlEntities(text) {
const entities = {
'’': "'", // right single quotation
'‘': "'", // left single quotation
'“': '"', // left double quotation
'”': '"', // right double quotation
'–': '–', // en-dash
'—': '—', // em-dash
'&': '&', // ampersand
'&': '&', // ampersand
''': "'", // apostrophe
'"': '"', // quotation mark
'<': '<',
'>': '>',
};
let decoded = text;
for (const [entity, char] of Object.entries(entities)) {
decoded = decoded.replaceAll(entity, char);
}
// Also handle numeric entities
decoded = decoded.replace(/&#(\d+);/g, (match, num) => {
return String.fromCharCode(parseInt(num, 10));
});
return decoded;
}
// Get all news items with HTML entities
const news = await client.fetch(`*[_type == 'news'] {
_id,
title,
slug
}`);
const problematic = news.filter(item => /&#\d+;/.test(item.title));
console.log(`🧹 Fixing HTML entities in ${problematic.length} titles...\n`);
let fixed = 0;
for (const item of problematic) {
const decodedTitle = decodeHtmlEntities(item.title);
if (decodedTitle !== item.title) {
try {
await client.patch(item._id).set({ title: decodedTitle }).commit();
console.log(`✅ ${decodedTitle}`);
fixed++;
} catch (err) {
console.log(`❌ ${item.title}: ${err.message}`);
}
}
}
console.log(`\n✅ Fixed ${fixed} titles`);