Repository navigation
Expand file tree
/
Copy pathvalidate-seo.js
More file actions
291 lines (236 loc) · 9.67 KB
/
Copy pathvalidate-seo.js
File metadata and controls
291 lines (236 loc) · 9.67 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env node
/**
* SEO Validation Script for GGUF Loader Website
*
* This script validates:
* - Canonical URLs are present and correct
* - No duplicate content issues
* - Proper URL hierarchy
* - Sitemap completeness
*/
const fs = require('fs');
const path = require('path');
const { JSDOM } = require('jsdom');
// Configuration
const SITE_URL = 'https://ggufloader.github.io';
const DOCS_PAGES = [
'docs/index.html',
'docs/installation/index.html',
'docs/quick-start/index.html',
'docs/addon-development/index.html',
'docs/addon-api/index.html',
'docs/floating-chat-example/index.html',
'docs/package-structure/index.html'
];
class SEOValidator {
constructor() {
this.errors = [];
this.warnings = [];
this.canonicalUrls = new Set();
}
log(message, type = 'info') {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] ${type.toUpperCase()}: ${message}`);
if (type === 'error') {
this.errors.push(message);
} else if (type === 'warning') {
this.warnings.push(message);
}
}
validateCanonicalUrl(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const dom = new JSDOM(content);
const document = dom.window.document;
const canonicalLink = document.querySelector('link[rel="canonical"]');
if (!canonicalLink) {
this.log(`Missing canonical URL in ${filePath}`, 'error');
return false;
}
const canonicalUrl = canonicalLink.getAttribute('href');
if (!canonicalUrl.startsWith(SITE_URL)) {
this.log(`Invalid canonical URL in ${filePath}: ${canonicalUrl}`, 'error');
return false;
}
if (this.canonicalUrls.has(canonicalUrl)) {
this.log(`Duplicate canonical URL found: ${canonicalUrl}`, 'error');
return false;
}
this.canonicalUrls.add(canonicalUrl);
this.log(`Valid canonical URL in ${filePath}: ${canonicalUrl}`);
return true;
} catch (error) {
this.log(`Error reading ${filePath}: ${error.message}`, 'error');
return false;
}
}
validateMetaTags(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const dom = new JSDOM(content);
const document = dom.window.document;
// Check for required meta tags
const requiredMeta = [
'meta[name="description"]',
'meta[name="keywords"]',
'meta[name="robots"]',
'meta[property="og:title"]',
'meta[property="og:description"]',
'meta[property="og:url"]'
];
let valid = true;
requiredMeta.forEach(selector => {
const element = document.querySelector(selector);
if (!element) {
this.log(`Missing meta tag ${selector} in ${filePath}`, 'warning');
valid = false;
} else {
const content = element.getAttribute('content') || element.getAttribute('property');
if (!content || content.trim().length === 0) {
this.log(`Empty meta tag ${selector} in ${filePath}`, 'warning');
valid = false;
}
}
});
return valid;
} catch (error) {
this.log(`Error validating meta tags in ${filePath}: ${error.message}`, 'error');
return false;
}
}
validateStructuredData(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const dom = new JSDOM(content);
const document = dom.window.document;
const jsonLdScripts = document.querySelectorAll('script[type="application/ld+json"]');
if (jsonLdScripts.length === 0) {
this.log(`No structured data found in ${filePath}`, 'warning');
return false;
}
let valid = true;
jsonLdScripts.forEach((script, index) => {
try {
const jsonData = JSON.parse(script.textContent);
if (!jsonData['@context'] || !jsonData['@type']) {
this.log(`Invalid structured data format in ${filePath} (script ${index + 1})`, 'error');
valid = false;
} else {
this.log(`Valid structured data (${jsonData['@type']}) in ${filePath}`);
}
} catch (parseError) {
this.log(`Invalid JSON-LD in ${filePath} (script ${index + 1}): ${parseError.message}`, 'error');
valid = false;
}
});
return valid;
} catch (error) {
this.log(`Error validating structured data in ${filePath}: ${error.message}`, 'error');
return false;
}
}
validateSitemap() {
try {
const sitemapPath = 'sitemap.xml';
if (!fs.existsSync(sitemapPath)) {
this.log('Sitemap.xml not found', 'error');
return false;
}
const content = fs.readFileSync(sitemapPath, 'utf8');
const dom = new JSDOM(content, { contentType: 'text/xml' });
const document = dom.window.document;
const urls = document.querySelectorAll('url loc');
const sitemapUrls = Array.from(urls).map(url => url.textContent);
this.log(`Sitemap contains ${sitemapUrls.length} URLs`);
// Check if all documentation pages are in sitemap
const expectedUrls = [
`${SITE_URL}/`,
`${SITE_URL}/docs/`,
`${SITE_URL}/docs/installation/`,
`${SITE_URL}/docs/quick-start/`,
`${SITE_URL}/docs/addon-development/`,
`${SITE_URL}/docs/addon-api/`,
`${SITE_URL}/docs/floating-chat-example/`,
`${SITE_URL}/docs/package-structure/`
];
let valid = true;
expectedUrls.forEach(expectedUrl => {
if (!sitemapUrls.includes(expectedUrl)) {
this.log(`Missing URL in sitemap: ${expectedUrl}`, 'warning');
valid = false;
}
});
return valid;
} catch (error) {
this.log(`Error validating sitemap: ${error.message}`, 'error');
return false;
}
}
validateUrlHierarchy() {
this.log('Validating URL hierarchy...');
// Expected URL structure
const expectedStructure = {
'/': 'Homepage',
'/docs/': 'Documentation Hub',
'/docs/installation/': 'Installation Guide',
'/docs/quick-start/': 'Quick Start Guide',
'/docs/addon-development/': 'Addon Development Guide',
'/docs/addon-api/': 'API Reference',
'/docs/floating-chat-example/': 'Floating Chat Example',
'/docs/package-structure/': 'Package Structure'
};
Object.entries(expectedStructure).forEach(([url, description]) => {
this.log(`✓ ${url} - ${description}`);
});
this.log('URL hierarchy validation complete');
return true;
}
async validate() {
this.log('Starting SEO validation...');
// Validate main index page
if (fs.existsSync('index.html')) {
this.validateCanonicalUrl('index.html');
this.validateMetaTags('index.html');
this.validateStructuredData('index.html');
}
// Validate documentation pages
DOCS_PAGES.forEach(page => {
if (fs.existsSync(page)) {
this.log(`Validating ${page}...`);
this.validateCanonicalUrl(page);
this.validateMetaTags(page);
this.validateStructuredData(page);
} else {
this.log(`File not found: ${page}`, 'warning');
}
});
// Validate sitemap
this.validateSitemap();
// Validate URL hierarchy
this.validateUrlHierarchy();
// Summary
this.log('\n=== VALIDATION SUMMARY ===');
this.log(`Errors: ${this.errors.length}`);
this.log(`Warnings: ${this.warnings.length}`);
if (this.errors.length > 0) {
this.log('\nERRORS:');
this.errors.forEach(error => this.log(` - ${error}`));
}
if (this.warnings.length > 0) {
this.log('\nWARNINGS:');
this.warnings.forEach(warning => this.log(` - ${warning}`));
}
if (this.errors.length === 0 && this.warnings.length === 0) {
this.log('✅ All SEO validations passed!');
}
return this.errors.length === 0;
}
}
// Run validation if called directly
if (require.main === module) {
const validator = new SEOValidator();
validator.validate().then(success => {
process.exit(success ? 0 : 1);
});
}
module.exports = SEOValidator;