-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfix-script.js
More file actions
127 lines (108 loc) · 4.36 KB
/
fix-script.js
File metadata and controls
127 lines (108 loc) · 4.36 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const filePath = '/Users/neonone/Downloads/moltdirectory/index.html';
let content = fs.readFileSync(filePath, 'utf8');
// Find the start and end of the search script
const scriptStartMarker = '<script>\n // Static search data embedded at build time';
const scriptStart = content.indexOf(scriptStartMarker);
if (scriptStart === -1) {
console.log('Could not find script start marker');
process.exit(1);
}
// Find the closing </script> after that
const scriptEndSearch = content.indexOf('</script>', scriptStart);
if (scriptEndSearch === -1) {
console.log('Could not find script end');
process.exit(1);
}
// Extract the content between script tags
const scriptContent = content.substring(scriptStart + '<script>'.length, scriptEndSearch);
// Find the JSON array
const jsonStart = scriptContent.indexOf('[');
const jsonEnd = scriptContent.lastIndexOf(']');
if (jsonStart === -1 || jsonEnd === -1) {
console.log('Could not find JSON array');
process.exit(1);
}
let jsonStr = scriptContent.substring(jsonStart, jsonEnd + 1);
// Clean up the JSON - fix spacing issues
jsonStr = jsonStr.replace(/" \}/g, '"}'); // Fix "id":"value" }
jsonStr = jsonStr.replace(/\} ,/g, '},'); // Fix } ,
jsonStr = jsonStr.replace(/\}\]/g, '}]'); // Fix end
// Parse and validate
let skillsData;
try {
skillsData = JSON.parse(jsonStr);
console.log('Parsed ' + skillsData.length + ' skills successfully');
} catch (e) {
console.log('JSON parse error:', e.message);
// Show position of error
const match = e.message.match(/position (\d+)/);
if (match) {
const pos = parseInt(match[1]);
console.log('Around position ' + pos + ': ...' + jsonStr.substring(pos - 20, pos + 20) + '...');
}
process.exit(1);
}
// Build the properly formatted script
const newScript = `<script>
// Static search data embedded at build time
const skillsData = ${JSON.stringify(skillsData)};
const searchInput = document.getElementById('searchInput');
const categoriesGrid = document.getElementById('categoriesGrid');
const noResults = document.getElementById('noResults');
const searchResults = document.getElementById('searchResults');
const categoryCards = document.querySelectorAll('.category-card');
// Keyboard shortcut
document.addEventListener('keydown', (e) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault();
searchInput.focus();
}
if (e.key === 'Escape') {
searchInput.blur();
searchInput.value = '';
handleSearch('');
}
});
searchInput.addEventListener('input', (e) => handleSearch(e.target.value));
function handleSearch(query) {
const q = query.toLowerCase().trim();
if (!q) {
// Show all categories, hide skill result
categoryCards.forEach(card => card.style.display = '');
noResults.style.display = 'none';
searchResults.style.display = 'none';
return;
}
// Filter categories
let visibleCount = 0;
categoryCards.forEach(card => {
const name = card.dataset.name;
const desc = card.dataset.desc;
const matches = name.includes(q) || desc.includes(q);
card.style.display = matches ? '' : 'none';
if (matches) visibleCount++;
});
// Search skills
const matchingSkills = skillsData.filter(s =>
s.name.toLowerCase().includes(q) || s.desc.toLowerCase().includes(q)
).slice(0, 8);
if (matchingSkills.length > 0) {
searchResults.innerHTML = '<div class="search-results-title">Skills matching "' + q + '"</div>'
+ matchingSkills.map(s =>
'<a href="./' + s.catId + '/' + s.id + '/index.html" class="search-result-item">'
+ '<div class="search-result-name">' + s.name + '</div>'
+ '<div class="search-result-cat">' + s.catName + '</div>'
+ '</a>'
).join('');
searchResults.style.display = 'block';
} else {
searchResults.style.display = 'none';
}
noResults.style.display = visibleCount === 0 && matchingSkills.length === 0 ? 'block' : 'none';
}
</script>`;
// Replace the script section
const newContent = content.substring(0, scriptStart) + newScript + content.substring(scriptEndSearch + '</script>'.length);
fs.writeFileSync(filePath, newContent, 'utf8');
console.log('Successfully replaced the search script');