@rankcli/agent-runtime 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/README.md +242 -0
  2. package/dist/analyzer-2CSWIQGD.mjs +6 -0
  3. package/dist/chunk-YNZYHEYM.mjs +774 -0
  4. package/dist/index.d.mts +4012 -0
  5. package/dist/index.d.ts +4012 -0
  6. package/dist/index.js +29672 -0
  7. package/dist/index.mjs +28602 -0
  8. package/package.json +53 -0
  9. package/scripts/build-deno.ts +134 -0
  10. package/src/audit/ai/analyzer.ts +347 -0
  11. package/src/audit/ai/index.ts +29 -0
  12. package/src/audit/ai/prompts/content-analysis.ts +271 -0
  13. package/src/audit/ai/types.ts +179 -0
  14. package/src/audit/checks/additional-checks.ts +439 -0
  15. package/src/audit/checks/ai-citation-worthiness.ts +399 -0
  16. package/src/audit/checks/ai-content-structure.ts +325 -0
  17. package/src/audit/checks/ai-readiness.ts +339 -0
  18. package/src/audit/checks/anchor-text.ts +179 -0
  19. package/src/audit/checks/answer-conciseness.ts +322 -0
  20. package/src/audit/checks/asset-minification.ts +270 -0
  21. package/src/audit/checks/bing-optimization.ts +206 -0
  22. package/src/audit/checks/brand-mention-optimization.ts +349 -0
  23. package/src/audit/checks/caching-headers.ts +305 -0
  24. package/src/audit/checks/canonical-advanced.ts +150 -0
  25. package/src/audit/checks/canonical-domain.ts +196 -0
  26. package/src/audit/checks/citation-quality.ts +358 -0
  27. package/src/audit/checks/client-rendering.ts +542 -0
  28. package/src/audit/checks/color-contrast.ts +342 -0
  29. package/src/audit/checks/content-freshness.ts +170 -0
  30. package/src/audit/checks/content-science.ts +589 -0
  31. package/src/audit/checks/conversion-elements.ts +526 -0
  32. package/src/audit/checks/crawlability.ts +220 -0
  33. package/src/audit/checks/directory-listing.ts +172 -0
  34. package/src/audit/checks/dom-analysis.ts +191 -0
  35. package/src/audit/checks/dom-size.ts +246 -0
  36. package/src/audit/checks/duplicate-content.ts +194 -0
  37. package/src/audit/checks/eeat-signals.ts +990 -0
  38. package/src/audit/checks/entity-seo.ts +396 -0
  39. package/src/audit/checks/featured-snippet.ts +473 -0
  40. package/src/audit/checks/freshness-signals.ts +443 -0
  41. package/src/audit/checks/funnel-intent.ts +463 -0
  42. package/src/audit/checks/hreflang.ts +174 -0
  43. package/src/audit/checks/html-compliance.ts +302 -0
  44. package/src/audit/checks/image-dimensions.ts +167 -0
  45. package/src/audit/checks/images.ts +160 -0
  46. package/src/audit/checks/indexnow.ts +275 -0
  47. package/src/audit/checks/interactive-tools.ts +475 -0
  48. package/src/audit/checks/internal-link-graph.ts +436 -0
  49. package/src/audit/checks/keyword-analysis.ts +239 -0
  50. package/src/audit/checks/keyword-cannibalization.ts +385 -0
  51. package/src/audit/checks/keyword-placement.ts +471 -0
  52. package/src/audit/checks/links.ts +203 -0
  53. package/src/audit/checks/llms-txt.ts +224 -0
  54. package/src/audit/checks/local-seo.ts +296 -0
  55. package/src/audit/checks/mobile.ts +167 -0
  56. package/src/audit/checks/modern-images.ts +226 -0
  57. package/src/audit/checks/navboost-signals.ts +395 -0
  58. package/src/audit/checks/on-page.ts +209 -0
  59. package/src/audit/checks/page-resources.ts +285 -0
  60. package/src/audit/checks/pagination.ts +180 -0
  61. package/src/audit/checks/performance.ts +153 -0
  62. package/src/audit/checks/platform-presence.ts +580 -0
  63. package/src/audit/checks/redirect-analysis.ts +153 -0
  64. package/src/audit/checks/redirect-chain.ts +389 -0
  65. package/src/audit/checks/resource-hints.ts +420 -0
  66. package/src/audit/checks/responsive-css.ts +247 -0
  67. package/src/audit/checks/responsive-images.ts +396 -0
  68. package/src/audit/checks/review-ecosystem.ts +415 -0
  69. package/src/audit/checks/robots-validation.ts +373 -0
  70. package/src/audit/checks/security-headers.ts +172 -0
  71. package/src/audit/checks/security.ts +144 -0
  72. package/src/audit/checks/serp-preview.ts +251 -0
  73. package/src/audit/checks/site-maturity.ts +444 -0
  74. package/src/audit/checks/social-meta.test.ts +275 -0
  75. package/src/audit/checks/social-meta.ts +134 -0
  76. package/src/audit/checks/soft-404.ts +151 -0
  77. package/src/audit/checks/structured-data.ts +238 -0
  78. package/src/audit/checks/tech-detection.ts +496 -0
  79. package/src/audit/checks/topical-clusters.ts +435 -0
  80. package/src/audit/checks/tracker-bloat.ts +462 -0
  81. package/src/audit/checks/tracking-verification.test.ts +371 -0
  82. package/src/audit/checks/tracking-verification.ts +636 -0
  83. package/src/audit/checks/url-safety.ts +682 -0
  84. package/src/audit/deno-entry.ts +66 -0
  85. package/src/audit/discovery/index.ts +15 -0
  86. package/src/audit/discovery/link-crawler.ts +232 -0
  87. package/src/audit/discovery/repo-routes.ts +347 -0
  88. package/src/audit/engine.ts +620 -0
  89. package/src/audit/fixes/index.ts +209 -0
  90. package/src/audit/fixes/social-meta-fixes.test.ts +329 -0
  91. package/src/audit/fixes/social-meta-fixes.ts +463 -0
  92. package/src/audit/index.ts +74 -0
  93. package/src/audit/runner.test.ts +299 -0
  94. package/src/audit/runner.ts +130 -0
  95. package/src/audit/types.ts +1953 -0
  96. package/src/content/featured-snippet.ts +367 -0
  97. package/src/content/generator.test.ts +534 -0
  98. package/src/content/generator.ts +501 -0
  99. package/src/content/headline.ts +317 -0
  100. package/src/content/index.ts +62 -0
  101. package/src/content/intent.ts +258 -0
  102. package/src/content/keyword-density.ts +349 -0
  103. package/src/content/readability.ts +262 -0
  104. package/src/executor.ts +336 -0
  105. package/src/fixer.ts +416 -0
  106. package/src/frameworks/detector.test.ts +248 -0
  107. package/src/frameworks/detector.ts +371 -0
  108. package/src/frameworks/index.ts +68 -0
  109. package/src/frameworks/recipes/angular.yaml +171 -0
  110. package/src/frameworks/recipes/astro.yaml +206 -0
  111. package/src/frameworks/recipes/django.yaml +180 -0
  112. package/src/frameworks/recipes/laravel.yaml +137 -0
  113. package/src/frameworks/recipes/nextjs.yaml +268 -0
  114. package/src/frameworks/recipes/nuxt.yaml +175 -0
  115. package/src/frameworks/recipes/rails.yaml +188 -0
  116. package/src/frameworks/recipes/react.yaml +202 -0
  117. package/src/frameworks/recipes/sveltekit.yaml +154 -0
  118. package/src/frameworks/recipes/vue.yaml +137 -0
  119. package/src/frameworks/recipes/wordpress.yaml +209 -0
  120. package/src/frameworks/suggestion-engine.ts +320 -0
  121. package/src/geo/geo-content.test.ts +305 -0
  122. package/src/geo/geo-content.ts +266 -0
  123. package/src/geo/geo-history.test.ts +473 -0
  124. package/src/geo/geo-history.ts +433 -0
  125. package/src/geo/geo-tracker.test.ts +359 -0
  126. package/src/geo/geo-tracker.ts +411 -0
  127. package/src/geo/index.ts +10 -0
  128. package/src/git/commit-helper.test.ts +261 -0
  129. package/src/git/commit-helper.ts +329 -0
  130. package/src/git/index.ts +12 -0
  131. package/src/git/pr-helper.test.ts +284 -0
  132. package/src/git/pr-helper.ts +307 -0
  133. package/src/index.ts +66 -0
  134. package/src/keywords/ai-keyword-engine.ts +1062 -0
  135. package/src/keywords/ai-summarizer.ts +387 -0
  136. package/src/keywords/ci-mode.ts +555 -0
  137. package/src/keywords/engine.ts +359 -0
  138. package/src/keywords/index.ts +151 -0
  139. package/src/keywords/llm-judge.ts +357 -0
  140. package/src/keywords/nlp-analysis.ts +706 -0
  141. package/src/keywords/prioritizer.ts +295 -0
  142. package/src/keywords/site-crawler.ts +342 -0
  143. package/src/keywords/sources/autocomplete.ts +139 -0
  144. package/src/keywords/sources/competitive-search.ts +450 -0
  145. package/src/keywords/sources/competitor-analysis.ts +374 -0
  146. package/src/keywords/sources/dataforseo.ts +206 -0
  147. package/src/keywords/sources/free-sources.ts +294 -0
  148. package/src/keywords/sources/gsc.ts +123 -0
  149. package/src/keywords/topic-grouping.ts +327 -0
  150. package/src/keywords/types.ts +144 -0
  151. package/src/keywords/wizard.ts +457 -0
  152. package/src/loader.ts +40 -0
  153. package/src/reports/index.ts +7 -0
  154. package/src/reports/report-generator.test.ts +293 -0
  155. package/src/reports/report-generator.ts +713 -0
  156. package/src/scheduler/alerts.test.ts +458 -0
  157. package/src/scheduler/alerts.ts +328 -0
  158. package/src/scheduler/index.ts +8 -0
  159. package/src/scheduler/scheduled-audit.test.ts +377 -0
  160. package/src/scheduler/scheduled-audit.ts +149 -0
  161. package/src/test/integration-test.ts +325 -0
  162. package/src/tools/analyzer.ts +373 -0
  163. package/src/tools/crawl.ts +293 -0
  164. package/src/tools/files.ts +301 -0
  165. package/src/tools/h1-fixer.ts +249 -0
  166. package/src/tools/index.ts +67 -0
  167. package/src/tracking/github-action.ts +326 -0
  168. package/src/tracking/google-analytics.ts +265 -0
  169. package/src/tracking/index.ts +45 -0
  170. package/src/tracking/report-generator.ts +386 -0
  171. package/src/tracking/search-console.ts +335 -0
  172. package/src/types.ts +134 -0
  173. package/src/utils/http.ts +302 -0
  174. package/src/wasm-adapter.ts +297 -0
  175. package/src/wasm-entry.ts +14 -0
  176. package/tsconfig.json +17 -0
  177. package/tsup.wasm.config.ts +26 -0
  178. package/vitest.config.ts +15 -0
@@ -0,0 +1,325 @@
1
+ #!/usr/bin/env npx tsx
2
+ /**
3
+ * Integration Test for SEO Audit Tool
4
+ *
5
+ * Tests the full workflow:
6
+ * 1. Detect framework
7
+ * 2. Run SEO audit
8
+ * 3. Generate fixes
9
+ * 4. Create conventional commits with co-author
10
+ *
11
+ * Usage:
12
+ * npx tsx src/test/integration-test.ts [repo-path]
13
+ *
14
+ * Test repos in /workspace/test_repos/:
15
+ * - html-portfolio (Pure HTML/CSS)
16
+ * - astro-blog (Astro)
17
+ * - hugo-xmin (Hugo)
18
+ * - github-pages-site (Jekyll/GitHub Pages)
19
+ */
20
+
21
+ import * as fs from 'fs';
22
+ import * as path from 'path';
23
+ import { fileURLToPath } from 'url';
24
+ import {
25
+ formatSEOCommitMessage,
26
+ createSEOCommit,
27
+ generateCommitSummary,
28
+ detectGitHubPages,
29
+ isGitRepo,
30
+ type SEOFixCommit,
31
+ type CommitConfig,
32
+ } from '../git/commit-helper.js';
33
+
34
+ const __filename = fileURLToPath(import.meta.url);
35
+ const __dirname = path.dirname(__filename);
36
+
37
+ // Framework detection
38
+ interface FrameworkInfo {
39
+ name: string;
40
+ type: 'static' | 'ssr' | 'spa';
41
+ configFiles: string[];
42
+ fixPattern: string;
43
+ }
44
+
45
+ const FRAMEWORKS: Record<string, FrameworkInfo> = {
46
+ astro: {
47
+ name: 'Astro',
48
+ type: 'static',
49
+ configFiles: ['astro.config.mjs', 'astro.config.ts', 'astro.config.js'],
50
+ fixPattern: 'src/components/BaseHead.astro',
51
+ },
52
+ sveltekit: {
53
+ name: 'SvelteKit',
54
+ type: 'ssr',
55
+ configFiles: ['svelte.config.js', 'svelte.config.ts'],
56
+ fixPattern: 'src/routes/+layout.svelte',
57
+ },
58
+ nextjs: {
59
+ name: 'Next.js',
60
+ type: 'ssr',
61
+ configFiles: ['next.config.js', 'next.config.ts', 'next.config.mjs'],
62
+ fixPattern: 'app/layout.tsx',
63
+ },
64
+ hugo: {
65
+ name: 'Hugo',
66
+ type: 'static',
67
+ configFiles: ['config.toml', 'config.yaml', 'hugo.toml', 'hugo.yaml'],
68
+ fixPattern: 'layouts/partials/head.html',
69
+ },
70
+ jekyll: {
71
+ name: 'Jekyll',
72
+ type: 'static',
73
+ configFiles: ['_config.yml', '_config.yaml'],
74
+ fixPattern: '_includes/head.html',
75
+ },
76
+ html: {
77
+ name: 'Static HTML',
78
+ type: 'static',
79
+ configFiles: [],
80
+ fixPattern: 'index.html',
81
+ },
82
+ };
83
+
84
+ function detectFramework(repoPath: string): FrameworkInfo {
85
+ for (const [key, framework] of Object.entries(FRAMEWORKS)) {
86
+ if (key === 'html') continue; // Fallback
87
+
88
+ for (const configFile of framework.configFiles) {
89
+ if (fs.existsSync(path.join(repoPath, configFile))) {
90
+ return framework;
91
+ }
92
+ }
93
+ }
94
+
95
+ return FRAMEWORKS.html;
96
+ }
97
+
98
+ // Mock SEO audit results for testing
99
+ interface MockAuditResult {
100
+ category: string;
101
+ issues: Array<{
102
+ code: string;
103
+ title: string;
104
+ severity: 'error' | 'warning' | 'notice';
105
+ affectedFiles: string[];
106
+ }>;
107
+ }
108
+
109
+ function getMockAuditResults(framework: FrameworkInfo): MockAuditResult[] {
110
+ // These simulate what a real audit would find
111
+ return [
112
+ {
113
+ category: 'on-page',
114
+ issues: [
115
+ {
116
+ code: 'TITLE_MISSING',
117
+ title: 'Missing title tag',
118
+ severity: 'error',
119
+ affectedFiles: [framework.fixPattern],
120
+ },
121
+ {
122
+ code: 'META_DESC_MISSING',
123
+ title: 'Missing meta description',
124
+ severity: 'error',
125
+ affectedFiles: [framework.fixPattern],
126
+ },
127
+ ],
128
+ },
129
+ {
130
+ category: 'social-meta',
131
+ issues: [
132
+ {
133
+ code: 'OG_TITLE_MISSING',
134
+ title: 'Missing Open Graph title',
135
+ severity: 'warning',
136
+ affectedFiles: [framework.fixPattern],
137
+ },
138
+ {
139
+ code: 'TWITTER_CARD_MISSING',
140
+ title: 'Missing Twitter Card meta tags',
141
+ severity: 'warning',
142
+ affectedFiles: [framework.fixPattern],
143
+ },
144
+ ],
145
+ },
146
+ {
147
+ category: 'crawlability',
148
+ issues: [
149
+ {
150
+ code: 'ROBOTS_TXT_MISSING',
151
+ title: 'Missing robots.txt',
152
+ severity: 'warning',
153
+ affectedFiles: ['robots.txt'],
154
+ },
155
+ {
156
+ code: 'SITEMAP_MISSING',
157
+ title: 'Missing sitemap.xml',
158
+ severity: 'warning',
159
+ affectedFiles: ['sitemap.xml'],
160
+ },
161
+ ],
162
+ },
163
+ {
164
+ category: 'structured-data',
165
+ issues: [
166
+ {
167
+ code: 'SCHEMA_MISSING',
168
+ title: 'No structured data found',
169
+ severity: 'warning',
170
+ affectedFiles: [framework.fixPattern],
171
+ },
172
+ ],
173
+ },
174
+ ];
175
+ }
176
+
177
+ // Generate mock fixes for testing
178
+ function generateMockFixes(auditResults: MockAuditResult[]): SEOFixCommit[] {
179
+ return auditResults.map(result => ({
180
+ category: result.category,
181
+ issues: result.issues.map(i => i.code),
182
+ filesChanged: [...new Set(result.issues.flatMap(i => i.affectedFiles))],
183
+ description: `add ${result.category.replace('-', ' ')} improvements`,
184
+ }));
185
+ }
186
+
187
+ // Main test function
188
+ async function runIntegrationTest(repoPath: string, options: { dryRun?: boolean } = {}) {
189
+ console.log('═'.repeat(60));
190
+ console.log('SEO AUDIT TOOL - INTEGRATION TEST');
191
+ console.log('═'.repeat(60));
192
+ console.log('');
193
+
194
+ // Validate repo path
195
+ if (!fs.existsSync(repoPath)) {
196
+ console.error(`❌ Repository not found: ${repoPath}`);
197
+ process.exit(1);
198
+ }
199
+
200
+ console.log(`📁 Repository: ${repoPath}`);
201
+
202
+ // Check if it's a git repo
203
+ const gitStatus = isGitRepo(repoPath);
204
+ console.log(`📋 Git repo: ${gitStatus ? 'Yes' : 'No'}`);
205
+
206
+ // Detect GitHub Pages
207
+ const ghPages = detectGitHubPages(repoPath);
208
+ if (ghPages.isGitHubPages) {
209
+ console.log(`🌐 GitHub Pages: Yes (${ghPages.type})`);
210
+ if (ghPages.baseUrl) {
211
+ console.log(` Base URL: ${ghPages.baseUrl}`);
212
+ }
213
+ } else {
214
+ console.log('🌐 GitHub Pages: No');
215
+ }
216
+
217
+ // Detect framework
218
+ const framework = detectFramework(repoPath);
219
+ console.log(`🔧 Framework: ${framework.name} (${framework.type})`);
220
+ console.log(`📄 Fix target: ${framework.fixPattern}`);
221
+ console.log('');
222
+
223
+ // Run mock audit
224
+ console.log('─'.repeat(60));
225
+ console.log('RUNNING SEO AUDIT...');
226
+ console.log('─'.repeat(60));
227
+
228
+ const auditResults = getMockAuditResults(framework);
229
+ let totalIssues = 0;
230
+
231
+ for (const result of auditResults) {
232
+ console.log(`\n📊 ${result.category.toUpperCase()}`);
233
+ for (const issue of result.issues) {
234
+ const icon = issue.severity === 'error' ? '❌' : issue.severity === 'warning' ? '⚠️' : 'ℹ️';
235
+ console.log(` ${icon} ${issue.code}: ${issue.title}`);
236
+ totalIssues++;
237
+ }
238
+ }
239
+
240
+ console.log(`\n📈 Total issues found: ${totalIssues}`);
241
+ console.log('');
242
+
243
+ // Generate fixes
244
+ console.log('─'.repeat(60));
245
+ console.log('GENERATING FIXES...');
246
+ console.log('─'.repeat(60));
247
+
248
+ const fixes = generateMockFixes(auditResults);
249
+
250
+ for (const fix of fixes) {
251
+ console.log(`\n🔨 ${fix.category}`);
252
+ console.log(` Description: ${fix.description}`);
253
+ console.log(` Files: ${fix.filesChanged.join(', ')}`);
254
+ console.log(` Issues: ${fix.issues.join(', ')}`);
255
+ }
256
+ console.log('');
257
+
258
+ // Create commits
259
+ console.log('─'.repeat(60));
260
+ console.log('CREATING COMMITS...');
261
+ console.log('─'.repeat(60));
262
+ console.log('');
263
+
264
+ const commitConfig: CommitConfig = {
265
+ systemName: process.env.SEO_SYSTEM_NAME || 'SEO Autopilot',
266
+ systemEmail: process.env.SEO_SYSTEM_EMAIL || 'bot@seo-autopilot.dev',
267
+ dryRun: options.dryRun ?? true, // Default to dry run for safety
268
+ };
269
+
270
+ console.log(`🤖 Co-author: ${commitConfig.systemName} <${commitConfig.systemEmail}>`);
271
+ console.log(`🏃 Mode: ${commitConfig.dryRun ? 'DRY RUN' : 'LIVE'}`);
272
+ console.log('');
273
+
274
+ for (const fix of fixes) {
275
+ const message = formatSEOCommitMessage(fix, commitConfig);
276
+ console.log('┌' + '─'.repeat(58) + '┐');
277
+ console.log('│ COMMIT MESSAGE' + ' '.repeat(43) + '│');
278
+ console.log('├' + '─'.repeat(58) + '┤');
279
+ for (const line of message.split('\n')) {
280
+ const paddedLine = line.padEnd(56).slice(0, 56);
281
+ console.log(`│ ${paddedLine} │`);
282
+ }
283
+ console.log('└' + '─'.repeat(58) + '┘');
284
+ console.log('');
285
+ }
286
+
287
+ // Summary
288
+ console.log('─'.repeat(60));
289
+ console.log('SUMMARY');
290
+ console.log('─'.repeat(60));
291
+ console.log('');
292
+ console.log(`✅ Framework detected: ${framework.name}`);
293
+ console.log(`✅ GitHub Pages: ${ghPages.isGitHubPages ? 'Yes' : 'No'}`);
294
+ console.log(`✅ Issues found: ${totalIssues}`);
295
+ console.log(`✅ Commits prepared: ${fixes.length}`);
296
+ console.log('');
297
+
298
+ if (commitConfig.dryRun) {
299
+ console.log('💡 Run with --live to create actual commits');
300
+ }
301
+
302
+ return {
303
+ framework,
304
+ ghPages,
305
+ auditResults,
306
+ fixes,
307
+ totalIssues,
308
+ };
309
+ }
310
+
311
+ // CLI entry point
312
+ const args = process.argv.slice(2);
313
+ const repoPath = args[0] || path.join(__dirname, '../../../../test_repos/html-portfolio');
314
+ const isLive = args.includes('--live');
315
+
316
+ runIntegrationTest(repoPath, { dryRun: !isLive })
317
+ .then(() => {
318
+ console.log('═'.repeat(60));
319
+ console.log('TEST COMPLETE');
320
+ console.log('═'.repeat(60));
321
+ })
322
+ .catch((error) => {
323
+ console.error('Test failed:', error);
324
+ process.exit(1);
325
+ });
@@ -0,0 +1,373 @@
1
+ import { crawlUrl, extractMeta, analyzeHeadings, extractImages, extractLinks, extractSchema, checkRobots, checkSitemap } from './crawl.js';
2
+ import type { SEOIssue, SEOAnalysisResult, MetaData, HeadingStructure, ImageInfo, ToolResult } from '../types.js';
3
+
4
+ export async function analyzeUrl(params: { url: string }): Promise<ToolResult> {
5
+ const { url } = params;
6
+ const issues: SEOIssue[] = [];
7
+
8
+ try {
9
+ // Crawl the page
10
+ const crawlResult = await crawlUrl({ url });
11
+ if (!crawlResult.success || !crawlResult.data) {
12
+ return { success: false, error: `Failed to crawl URL: ${crawlResult.error}` };
13
+ }
14
+
15
+ const { html, statusCode, loadTime } = crawlResult.data as { html: string; statusCode: number; loadTime: number };
16
+
17
+ if (statusCode !== 200) {
18
+ issues.push({
19
+ severity: 'critical',
20
+ category: 'technical',
21
+ code: 'HTTP_ERROR',
22
+ message: `Page returned HTTP ${statusCode}`,
23
+ impact: 'Search engines cannot index pages that return error codes',
24
+ });
25
+ }
26
+
27
+ // Extract meta tags
28
+ const metaResult = await extractMeta({ html, url });
29
+ if (metaResult.success && metaResult.data) {
30
+ const meta = metaResult.data as MetaData;
31
+ analyzeMeta(meta, url, issues);
32
+ }
33
+
34
+ // Analyze headings
35
+ const headingsResult = await analyzeHeadings({ html });
36
+ if (headingsResult.success && headingsResult.data) {
37
+ const headings = headingsResult.data as HeadingStructure[];
38
+ analyzeHeadingStructure(headings, issues);
39
+ }
40
+
41
+ // Extract and analyze images
42
+ const imagesResult = await extractImages({ html });
43
+ if (imagesResult.success && imagesResult.data) {
44
+ const images = imagesResult.data as ImageInfo[];
45
+ analyzeImages(images, issues);
46
+ }
47
+
48
+ // Extract schema
49
+ const schemaResult = await extractSchema({ html });
50
+ if (schemaResult.success) {
51
+ const schemas = schemaResult.data as { type: string; data: unknown }[];
52
+ if (!schemas || schemas.length === 0) {
53
+ issues.push({
54
+ severity: 'warning',
55
+ category: 'schema',
56
+ code: 'MISSING_SCHEMA',
57
+ message: 'No JSON-LD structured data found',
58
+ impact: 'Missing rich snippets in search results. Add Schema.org markup for your content type.',
59
+ });
60
+ }
61
+ }
62
+
63
+ // Check robots.txt
64
+ const robotsResult = await checkRobots({ url });
65
+ if (robotsResult.success) {
66
+ const robots = robotsResult.data as { exists: boolean };
67
+ if (!robots.exists) {
68
+ issues.push({
69
+ severity: 'warning',
70
+ category: 'technical',
71
+ code: 'MISSING_ROBOTS',
72
+ message: 'No robots.txt file found',
73
+ impact: 'Provide crawling instructions to search engines with robots.txt',
74
+ });
75
+ }
76
+ }
77
+
78
+ // Check sitemap
79
+ const sitemapResult = await checkSitemap({ url });
80
+ if (sitemapResult.success) {
81
+ const sitemap = sitemapResult.data as { exists: boolean };
82
+ if (!sitemap.exists) {
83
+ issues.push({
84
+ severity: 'warning',
85
+ category: 'technical',
86
+ code: 'MISSING_SITEMAP',
87
+ message: 'No sitemap.xml found',
88
+ impact: 'Sitemap helps search engines discover and index all your pages',
89
+ });
90
+ }
91
+ }
92
+
93
+ // Performance check
94
+ if (loadTime > 3000) {
95
+ issues.push({
96
+ severity: loadTime > 5000 ? 'critical' : 'warning',
97
+ category: 'performance',
98
+ code: 'SLOW_LOAD_TIME',
99
+ message: `Page load time is ${(loadTime / 1000).toFixed(1)}s`,
100
+ impact: 'Slow pages hurt user experience and search rankings. Target <2.5s LCP.',
101
+ });
102
+ }
103
+
104
+ // Calculate score
105
+ const score = calculateScore(issues);
106
+
107
+ const result: SEOAnalysisResult = {
108
+ url,
109
+ score,
110
+ issues,
111
+ recommendations: generateRecommendations(issues),
112
+ };
113
+
114
+ return { success: true, data: result };
115
+ } catch (error) {
116
+ return {
117
+ success: false,
118
+ error: error instanceof Error ? error.message : 'Analysis failed'
119
+ };
120
+ }
121
+ }
122
+
123
+ function analyzeMeta(meta: MetaData, url: string, issues: SEOIssue[]): void {
124
+ // Title
125
+ if (!meta.title) {
126
+ issues.push({
127
+ severity: 'critical',
128
+ category: 'meta',
129
+ code: 'MISSING_TITLE',
130
+ message: 'Missing title tag',
131
+ impact: 'Title is the most important on-page SEO element. Every page needs a unique title.',
132
+ fix: {
133
+ file: 'index.html',
134
+ before: null,
135
+ after: '<title>Your Page Title - Brand Name</title>',
136
+ }
137
+ });
138
+ } else if (meta.title.length < 30) {
139
+ issues.push({
140
+ severity: 'warning',
141
+ category: 'meta',
142
+ code: 'TITLE_TOO_SHORT',
143
+ message: `Title too short (${meta.title.length} chars)`,
144
+ impact: 'Longer titles provide more context. Aim for 50-60 characters.',
145
+ element: meta.title,
146
+ });
147
+ } else if (meta.title.length > 60) {
148
+ issues.push({
149
+ severity: 'warning',
150
+ category: 'meta',
151
+ code: 'TITLE_TOO_LONG',
152
+ message: `Title too long (${meta.title.length} chars)`,
153
+ impact: 'Titles over 60 characters get truncated in search results.',
154
+ element: meta.title.substring(0, 60) + '...',
155
+ });
156
+ }
157
+
158
+ // Meta description
159
+ if (!meta.description) {
160
+ issues.push({
161
+ severity: 'critical',
162
+ category: 'meta',
163
+ code: 'MISSING_META_DESC',
164
+ message: 'Missing meta description',
165
+ impact: 'Meta description appears in search results and affects click-through rate.',
166
+ fix: {
167
+ file: 'index.html',
168
+ before: null,
169
+ after: '<meta name="description" content="A compelling description of your page in 150-160 characters." />',
170
+ }
171
+ });
172
+ } else if (meta.description.length < 120) {
173
+ issues.push({
174
+ severity: 'warning',
175
+ category: 'meta',
176
+ code: 'META_DESC_TOO_SHORT',
177
+ message: `Meta description too short (${meta.description.length} chars)`,
178
+ impact: 'Aim for 150-160 characters for optimal display in search results.',
179
+ });
180
+ } else if (meta.description.length > 160) {
181
+ issues.push({
182
+ severity: 'info',
183
+ category: 'meta',
184
+ code: 'META_DESC_TOO_LONG',
185
+ message: `Meta description may be truncated (${meta.description.length} chars)`,
186
+ impact: 'Descriptions over 160 characters may be cut off in search results.',
187
+ });
188
+ }
189
+
190
+ // Canonical URL
191
+ if (!meta.canonical) {
192
+ issues.push({
193
+ severity: 'warning',
194
+ category: 'technical',
195
+ code: 'MISSING_CANONICAL',
196
+ message: 'No canonical URL specified',
197
+ impact: 'Canonical tags prevent duplicate content issues.',
198
+ fix: {
199
+ file: 'index.html',
200
+ before: null,
201
+ after: `<link rel="canonical" href="${url}" />`,
202
+ }
203
+ });
204
+ }
205
+
206
+ // Viewport
207
+ if (!meta.viewport) {
208
+ issues.push({
209
+ severity: 'critical',
210
+ category: 'technical',
211
+ code: 'MISSING_VIEWPORT',
212
+ message: 'Missing viewport meta tag',
213
+ impact: 'Required for mobile-friendly pages. Google uses mobile-first indexing.',
214
+ fix: {
215
+ file: 'index.html',
216
+ before: null,
217
+ after: '<meta name="viewport" content="width=device-width, initial-scale=1" />',
218
+ }
219
+ });
220
+ }
221
+
222
+ // Open Graph
223
+ const missingOG: string[] = [];
224
+ if (!meta.openGraph.title) missingOG.push('og:title');
225
+ if (!meta.openGraph.description) missingOG.push('og:description');
226
+ if (!meta.openGraph.image) missingOG.push('og:image');
227
+
228
+ if (missingOG.length > 0) {
229
+ issues.push({
230
+ severity: 'warning',
231
+ category: 'social',
232
+ code: 'MISSING_OG_TAGS',
233
+ message: `Missing Open Graph tags: ${missingOG.join(', ')}`,
234
+ impact: 'Open Graph tags control how your page appears when shared on social media.',
235
+ });
236
+ }
237
+
238
+ // Twitter Card
239
+ if (!meta.twitter.card) {
240
+ issues.push({
241
+ severity: 'info',
242
+ category: 'social',
243
+ code: 'MISSING_TWITTER_CARD',
244
+ message: 'Missing Twitter Card meta tags',
245
+ impact: 'Twitter/X cards improve visibility when your content is shared.',
246
+ });
247
+ }
248
+ }
249
+
250
+ function analyzeHeadingStructure(headings: HeadingStructure[], issues: SEOIssue[]): void {
251
+ const h1s = headings.filter(h => h.level === 1);
252
+
253
+ if (h1s.length === 0) {
254
+ issues.push({
255
+ severity: 'critical',
256
+ category: 'content',
257
+ code: 'MISSING_H1',
258
+ message: 'No H1 heading found',
259
+ impact: 'Every page should have exactly one H1 that describes the main content.',
260
+ });
261
+ } else if (h1s.length > 1) {
262
+ issues.push({
263
+ severity: 'warning',
264
+ category: 'content',
265
+ code: 'MULTIPLE_H1',
266
+ message: `Multiple H1 headings found (${h1s.length})`,
267
+ impact: 'Best practice is one H1 per page. Use H2-H6 for subsections.',
268
+ element: h1s.map(h => h.text).join(', '),
269
+ });
270
+ }
271
+
272
+ // Check for skipped heading levels
273
+ let prevLevel = 0;
274
+ for (const h of headings) {
275
+ if (h.level > prevLevel + 1 && prevLevel > 0) {
276
+ issues.push({
277
+ severity: 'info',
278
+ category: 'content',
279
+ code: 'SKIPPED_HEADING_LEVEL',
280
+ message: `Skipped heading level: H${prevLevel} to H${h.level}`,
281
+ impact: 'Maintain proper heading hierarchy for accessibility and SEO.',
282
+ });
283
+ break; // Only report once
284
+ }
285
+ prevLevel = h.level;
286
+ }
287
+ }
288
+
289
+ function analyzeImages(images: ImageInfo[], issues: SEOIssue[]): void {
290
+ const missingAlt = images.filter(img => !img.alt);
291
+
292
+ if (missingAlt.length > 0) {
293
+ issues.push({
294
+ severity: 'warning',
295
+ category: 'accessibility',
296
+ code: 'IMAGES_MISSING_ALT',
297
+ message: `${missingAlt.length} image(s) missing alt text`,
298
+ impact: 'Alt text is essential for accessibility and helps search engines understand images.',
299
+ element: missingAlt.slice(0, 3).map(i => i.src).join(', '),
300
+ });
301
+ }
302
+
303
+ const missingDimensions = images.filter(img => !img.width || !img.height);
304
+ if (missingDimensions.length > 0) {
305
+ issues.push({
306
+ severity: 'info',
307
+ category: 'performance',
308
+ code: 'IMAGES_MISSING_DIMENSIONS',
309
+ message: `${missingDimensions.length} image(s) missing width/height`,
310
+ impact: 'Specifying dimensions prevents layout shift (CLS).',
311
+ });
312
+ }
313
+ }
314
+
315
+ function calculateScore(issues: SEOIssue[]): number {
316
+ const weights = {
317
+ critical: 15,
318
+ warning: 5,
319
+ info: 1,
320
+ };
321
+
322
+ let totalDeduction = 0;
323
+ for (const issue of issues) {
324
+ totalDeduction += weights[issue.severity];
325
+ }
326
+
327
+ return Math.max(0, Math.min(100, 100 - totalDeduction));
328
+ }
329
+
330
+ function generateRecommendations(issues: SEOIssue[]) {
331
+ const recommendations = [];
332
+
333
+ const criticalCount = issues.filter(i => i.severity === 'critical').length;
334
+ const warningCount = issues.filter(i => i.severity === 'warning').length;
335
+
336
+ if (criticalCount > 0) {
337
+ recommendations.push({
338
+ priority: 'high' as const,
339
+ category: 'immediate',
340
+ message: `Fix ${criticalCount} critical issue(s) first`,
341
+ impact: 'Critical issues directly impact search visibility',
342
+ });
343
+ }
344
+
345
+ if (issues.some(i => i.code === 'MISSING_SCHEMA')) {
346
+ recommendations.push({
347
+ priority: 'high' as const,
348
+ category: 'schema',
349
+ message: 'Add JSON-LD structured data',
350
+ impact: 'Enable rich snippets in search results',
351
+ });
352
+ }
353
+
354
+ if (issues.some(i => i.code.includes('OG') || i.code.includes('TWITTER'))) {
355
+ recommendations.push({
356
+ priority: 'medium' as const,
357
+ category: 'social',
358
+ message: 'Complete social media meta tags',
359
+ impact: 'Improve appearance when shared on social platforms',
360
+ });
361
+ }
362
+
363
+ if (warningCount > 3) {
364
+ recommendations.push({
365
+ priority: 'medium' as const,
366
+ category: 'optimization',
367
+ message: `Address ${warningCount} warnings to improve score`,
368
+ impact: 'Each fix improves overall SEO health',
369
+ });
370
+ }
371
+
372
+ return recommendations;
373
+ }