@rankcli/agent-runtime 0.0.14 → 0.0.16

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 (199) hide show
  1. package/dist/index.d.mts +77 -393
  2. package/dist/index.d.ts +77 -393
  3. package/dist/index.js +417 -6443
  4. package/dist/index.mjs +296 -6313
  5. package/package.json +6 -1
  6. package/scripts/build-deno.ts +0 -134
  7. package/src/analyzers/core-web-vitals-analyzer.test.ts +0 -236
  8. package/src/analyzers/core-web-vitals-analyzer.ts +0 -557
  9. package/src/analyzers/geo-analyzer.test.ts +0 -310
  10. package/src/analyzers/geo-analyzer.ts +0 -814
  11. package/src/analyzers/image-optimization-analyzer.test.ts +0 -145
  12. package/src/analyzers/image-optimization-analyzer.ts +0 -348
  13. package/src/analyzers/index.ts +0 -233
  14. package/src/analyzers/internal-linking-analyzer.test.ts +0 -141
  15. package/src/analyzers/internal-linking-analyzer.ts +0 -419
  16. package/src/analyzers/mobile-seo-analyzer.test.ts +0 -140
  17. package/src/analyzers/mobile-seo-analyzer.ts +0 -455
  18. package/src/analyzers/security-headers-analyzer.test.ts +0 -115
  19. package/src/analyzers/security-headers-analyzer.ts +0 -318
  20. package/src/analyzers/structured-data-analyzer.test.ts +0 -210
  21. package/src/analyzers/structured-data-analyzer.ts +0 -590
  22. package/src/audit/ai/analyzer.ts +0 -347
  23. package/src/audit/ai/index.ts +0 -29
  24. package/src/audit/ai/prompts/content-analysis.ts +0 -271
  25. package/src/audit/ai/types.ts +0 -179
  26. package/src/audit/checks/additional-checks.ts +0 -439
  27. package/src/audit/checks/ai-citation-worthiness.ts +0 -399
  28. package/src/audit/checks/ai-content-structure.ts +0 -325
  29. package/src/audit/checks/ai-readiness.ts +0 -339
  30. package/src/audit/checks/anchor-text.ts +0 -179
  31. package/src/audit/checks/answer-conciseness.ts +0 -322
  32. package/src/audit/checks/asset-minification.ts +0 -270
  33. package/src/audit/checks/bing-optimization.ts +0 -206
  34. package/src/audit/checks/brand-mention-optimization.ts +0 -349
  35. package/src/audit/checks/caching-headers.ts +0 -305
  36. package/src/audit/checks/canonical-advanced.ts +0 -150
  37. package/src/audit/checks/canonical-domain.ts +0 -196
  38. package/src/audit/checks/citation-quality.ts +0 -358
  39. package/src/audit/checks/client-rendering.ts +0 -542
  40. package/src/audit/checks/color-contrast.ts +0 -342
  41. package/src/audit/checks/content-freshness.ts +0 -170
  42. package/src/audit/checks/content-science.ts +0 -589
  43. package/src/audit/checks/conversion-elements.ts +0 -526
  44. package/src/audit/checks/crawlability.ts +0 -220
  45. package/src/audit/checks/directory-listing.ts +0 -172
  46. package/src/audit/checks/dom-analysis.ts +0 -191
  47. package/src/audit/checks/dom-size.ts +0 -246
  48. package/src/audit/checks/duplicate-content.ts +0 -194
  49. package/src/audit/checks/eeat-signals.ts +0 -990
  50. package/src/audit/checks/entity-seo.ts +0 -396
  51. package/src/audit/checks/featured-snippet.ts +0 -473
  52. package/src/audit/checks/freshness-signals.ts +0 -443
  53. package/src/audit/checks/funnel-intent.ts +0 -463
  54. package/src/audit/checks/hreflang.ts +0 -174
  55. package/src/audit/checks/html-compliance.ts +0 -302
  56. package/src/audit/checks/image-dimensions.ts +0 -167
  57. package/src/audit/checks/images.ts +0 -160
  58. package/src/audit/checks/indexnow.ts +0 -275
  59. package/src/audit/checks/interactive-tools.ts +0 -475
  60. package/src/audit/checks/internal-link-graph.ts +0 -436
  61. package/src/audit/checks/keyword-analysis.ts +0 -239
  62. package/src/audit/checks/keyword-cannibalization.ts +0 -385
  63. package/src/audit/checks/keyword-placement.ts +0 -471
  64. package/src/audit/checks/links.ts +0 -273
  65. package/src/audit/checks/llms-txt.ts +0 -224
  66. package/src/audit/checks/local-seo.ts +0 -296
  67. package/src/audit/checks/mobile.ts +0 -167
  68. package/src/audit/checks/modern-images.ts +0 -226
  69. package/src/audit/checks/navboost-signals.ts +0 -395
  70. package/src/audit/checks/on-page.ts +0 -209
  71. package/src/audit/checks/page-resources.ts +0 -285
  72. package/src/audit/checks/pagination.ts +0 -180
  73. package/src/audit/checks/performance.ts +0 -153
  74. package/src/audit/checks/platform-presence.ts +0 -580
  75. package/src/audit/checks/redirect-analysis.ts +0 -153
  76. package/src/audit/checks/redirect-chain.ts +0 -389
  77. package/src/audit/checks/resource-hints.ts +0 -420
  78. package/src/audit/checks/responsive-css.ts +0 -247
  79. package/src/audit/checks/responsive-images.ts +0 -396
  80. package/src/audit/checks/review-ecosystem.ts +0 -415
  81. package/src/audit/checks/robots-validation.ts +0 -373
  82. package/src/audit/checks/security-headers.ts +0 -188
  83. package/src/audit/checks/security.ts +0 -144
  84. package/src/audit/checks/serp-preview.ts +0 -251
  85. package/src/audit/checks/site-maturity.ts +0 -453
  86. package/src/audit/checks/social-meta.test.ts +0 -275
  87. package/src/audit/checks/social-meta.ts +0 -134
  88. package/src/audit/checks/soft-404.ts +0 -151
  89. package/src/audit/checks/structured-data.ts +0 -238
  90. package/src/audit/checks/tech-detection.ts +0 -496
  91. package/src/audit/checks/topical-clusters.ts +0 -435
  92. package/src/audit/checks/tracker-bloat.ts +0 -462
  93. package/src/audit/checks/tracking-verification.test.ts +0 -371
  94. package/src/audit/checks/tracking-verification.ts +0 -636
  95. package/src/audit/checks/url-safety.ts +0 -682
  96. package/src/audit/deno-entry.ts +0 -66
  97. package/src/audit/discovery/index.ts +0 -15
  98. package/src/audit/discovery/link-crawler.ts +0 -233
  99. package/src/audit/discovery/repo-routes.ts +0 -347
  100. package/src/audit/engine.ts +0 -627
  101. package/src/audit/fixes/index.ts +0 -209
  102. package/src/audit/fixes/social-meta-fixes.test.ts +0 -329
  103. package/src/audit/fixes/social-meta-fixes.ts +0 -463
  104. package/src/audit/index.ts +0 -74
  105. package/src/audit/runner.test.ts +0 -299
  106. package/src/audit/runner.ts +0 -130
  107. package/src/audit/types.ts +0 -1963
  108. package/src/content/featured-snippet.ts +0 -367
  109. package/src/content/generator.test.ts +0 -534
  110. package/src/content/generator.ts +0 -501
  111. package/src/content/headline.ts +0 -317
  112. package/src/content/index.ts +0 -62
  113. package/src/content/intent.ts +0 -258
  114. package/src/content/keyword-density.ts +0 -349
  115. package/src/content/readability.ts +0 -262
  116. package/src/executor.ts +0 -336
  117. package/src/fixer/framework-fixes.test.ts +0 -489
  118. package/src/fixer/framework-fixes.ts +0 -5287
  119. package/src/fixer/index.ts +0 -6
  120. package/src/fixer/schemas.ts +0 -971
  121. package/src/fixer.ts +0 -642
  122. package/src/frameworks/detector.test.ts +0 -248
  123. package/src/frameworks/detector.ts +0 -899
  124. package/src/frameworks/index.ts +0 -68
  125. package/src/frameworks/recipes/angular.yaml +0 -171
  126. package/src/frameworks/recipes/astro.yaml +0 -206
  127. package/src/frameworks/recipes/django.yaml +0 -180
  128. package/src/frameworks/recipes/laravel.yaml +0 -137
  129. package/src/frameworks/recipes/nextjs.yaml +0 -268
  130. package/src/frameworks/recipes/nuxt.yaml +0 -175
  131. package/src/frameworks/recipes/rails.yaml +0 -188
  132. package/src/frameworks/recipes/react.yaml +0 -202
  133. package/src/frameworks/recipes/sveltekit.yaml +0 -154
  134. package/src/frameworks/recipes/vue.yaml +0 -137
  135. package/src/frameworks/recipes/wordpress.yaml +0 -209
  136. package/src/frameworks/suggestion-engine.ts +0 -357
  137. package/src/geo/geo-content.test.ts +0 -305
  138. package/src/geo/geo-content.ts +0 -266
  139. package/src/geo/geo-history.test.ts +0 -473
  140. package/src/geo/geo-history.ts +0 -433
  141. package/src/geo/geo-tracker.test.ts +0 -359
  142. package/src/geo/geo-tracker.ts +0 -411
  143. package/src/geo/index.ts +0 -11
  144. package/src/geo/llm-citation-checker.ts +0 -188
  145. package/src/git/commit-helper.test.ts +0 -261
  146. package/src/git/commit-helper.ts +0 -329
  147. package/src/git/index.ts +0 -12
  148. package/src/git/pr-helper.test.ts +0 -284
  149. package/src/git/pr-helper.ts +0 -307
  150. package/src/index.ts +0 -76
  151. package/src/keywords/ai-keyword-engine.ts +0 -1062
  152. package/src/keywords/ai-summarizer.ts +0 -387
  153. package/src/keywords/ci-mode.ts +0 -555
  154. package/src/keywords/engine.ts +0 -359
  155. package/src/keywords/index.ts +0 -151
  156. package/src/keywords/llm-judge.ts +0 -357
  157. package/src/keywords/nlp-analysis.ts +0 -706
  158. package/src/keywords/prioritizer.ts +0 -295
  159. package/src/keywords/site-crawler.ts +0 -342
  160. package/src/keywords/sources/autocomplete.ts +0 -139
  161. package/src/keywords/sources/competitive-search.ts +0 -450
  162. package/src/keywords/sources/competitor-analysis.ts +0 -374
  163. package/src/keywords/sources/dataforseo.ts +0 -206
  164. package/src/keywords/sources/free-sources.ts +0 -294
  165. package/src/keywords/sources/gsc.ts +0 -123
  166. package/src/keywords/topic-grouping.ts +0 -327
  167. package/src/keywords/types.ts +0 -144
  168. package/src/keywords/wizard.ts +0 -457
  169. package/src/loader.ts +0 -40
  170. package/src/ranking/index.ts +0 -5
  171. package/src/ranking/serp-client.ts +0 -348
  172. package/src/ranking/tracker.ts +0 -380
  173. package/src/ranking/types.ts +0 -123
  174. package/src/reports/index.ts +0 -7
  175. package/src/reports/report-generator.test.ts +0 -293
  176. package/src/reports/report-generator.ts +0 -713
  177. package/src/scheduler/alerts.test.ts +0 -458
  178. package/src/scheduler/alerts.ts +0 -328
  179. package/src/scheduler/index.ts +0 -8
  180. package/src/scheduler/scheduled-audit.test.ts +0 -377
  181. package/src/scheduler/scheduled-audit.ts +0 -149
  182. package/src/test/integration-test.ts +0 -325
  183. package/src/tools/analyzer.ts +0 -373
  184. package/src/tools/crawl.ts +0 -293
  185. package/src/tools/files.ts +0 -301
  186. package/src/tools/h1-fixer.ts +0 -249
  187. package/src/tools/index.ts +0 -67
  188. package/src/tracking/github-action.ts +0 -326
  189. package/src/tracking/google-analytics.ts +0 -265
  190. package/src/tracking/index.ts +0 -45
  191. package/src/tracking/report-generator.ts +0 -386
  192. package/src/tracking/search-console.ts +0 -335
  193. package/src/types.ts +0 -148
  194. package/src/utils/http.ts +0 -302
  195. package/src/wasm-adapter.ts +0 -297
  196. package/src/wasm-entry.ts +0 -14
  197. package/tsconfig.json +0 -17
  198. package/tsup.wasm.config.ts +0 -26
  199. package/vitest.config.ts +0 -15
@@ -1,349 +0,0 @@
1
- // Keyword Density Analysis & LSI Keyword Detection
2
- // Analyzes keyword usage and detects semantically related terms
3
-
4
- export interface KeywordDensityAnalysis {
5
- primaryKeyword: string;
6
- // Density metrics
7
- density: number; // Percentage
8
- occurrences: number;
9
- wordCount: number;
10
- // Placement analysis
11
- inTitle: boolean;
12
- inH1: boolean;
13
- inFirstParagraph: boolean;
14
- inLastParagraph: boolean;
15
- inHeadings: number; // Count of H2-H6 with keyword
16
- // Warnings
17
- isKeywordStuffing: boolean;
18
- isUnderOptimized: boolean;
19
- // Related terms found
20
- lsiKeywords: LSIKeyword[];
21
- // Recommendations
22
- recommendations: string[];
23
- }
24
-
25
- export interface LSIKeyword {
26
- keyword: string;
27
- occurrences: number;
28
- relevance: 'high' | 'medium' | 'low';
29
- }
30
-
31
- export interface ContentElements {
32
- title?: string;
33
- h1?: string;
34
- headings: string[];
35
- body: string;
36
- firstParagraph?: string;
37
- lastParagraph?: string;
38
- }
39
-
40
- // Common LSI/semantic relations for various topics
41
- const LSI_PATTERNS: Record<string, string[]> = {
42
- // Generic patterns
43
- 'software': ['application', 'tool', 'platform', 'solution', 'program', 'app'],
44
- 'website': ['site', 'webpage', 'page', 'web application', 'portal'],
45
- 'seo': ['search engine optimization', 'ranking', 'serp', 'google', 'keywords', 'backlinks'],
46
- 'marketing': ['advertising', 'promotion', 'campaign', 'strategy', 'content', 'audience'],
47
- 'business': ['company', 'enterprise', 'organization', 'startup', 'firm'],
48
- 'tutorial': ['guide', 'how-to', 'instructions', 'steps', 'learn'],
49
- 'best': ['top', 'leading', 'recommended', 'popular', 'favorite'],
50
- };
51
-
52
- /**
53
- * Analyze keyword density in content
54
- */
55
- export function analyzeKeywordDensity(
56
- primaryKeyword: string,
57
- content: ContentElements
58
- ): KeywordDensityAnalysis {
59
- const keyword = primaryKeyword.toLowerCase();
60
- const keywordWords = keyword.split(/\s+/);
61
- const isPhrase = keywordWords.length > 1;
62
-
63
- const recommendations: string[] = [];
64
-
65
- // Count occurrences in body
66
- const bodyText = content.body.toLowerCase();
67
- const words = bodyText.split(/\s+/).filter(w => w.length > 0);
68
- const wordCount = words.length;
69
-
70
- // Count exact phrase/word occurrences
71
- let occurrences = 0;
72
- if (isPhrase) {
73
- // Count phrase occurrences
74
- const regex = new RegExp(escapeRegex(keyword), 'gi');
75
- const matches = bodyText.match(regex);
76
- occurrences = matches ? matches.length : 0;
77
- } else {
78
- // Count word occurrences
79
- occurrences = words.filter(w => w.replace(/[^a-z]/g, '') === keyword).length;
80
- }
81
-
82
- // Calculate density
83
- const density = (occurrences / Math.max(wordCount, 1)) * 100;
84
-
85
- // Check placement
86
- const inTitle = content.title ? content.title.toLowerCase().includes(keyword) : false;
87
- const inH1 = content.h1 ? content.h1.toLowerCase().includes(keyword) : false;
88
- const inFirstParagraph = content.firstParagraph
89
- ? content.firstParagraph.toLowerCase().includes(keyword)
90
- : false;
91
- const inLastParagraph = content.lastParagraph
92
- ? content.lastParagraph.toLowerCase().includes(keyword)
93
- : false;
94
- const inHeadings = content.headings.filter(h => h.toLowerCase().includes(keyword)).length;
95
-
96
- // Check for keyword stuffing (> 3% is generally considered stuffing)
97
- const isKeywordStuffing = density > 3;
98
- if (isKeywordStuffing) {
99
- recommendations.push(`Keyword density (${density.toFixed(2)}%) is too high. Reduce to 1-2% to avoid penalties`);
100
- }
101
-
102
- // Check for under-optimization
103
- const isUnderOptimized = density < 0.5 || occurrences < 2;
104
- if (isUnderOptimized && wordCount > 100) {
105
- recommendations.push(`Keyword density (${density.toFixed(2)}%) is low. Consider adding more natural mentions`);
106
- }
107
-
108
- // Placement recommendations
109
- if (!inTitle) {
110
- recommendations.push('Add primary keyword to the page title');
111
- }
112
- if (!inH1) {
113
- recommendations.push('Add primary keyword to the H1 heading');
114
- }
115
- if (!inFirstParagraph) {
116
- recommendations.push('Include keyword in the first 100 words of content');
117
- }
118
- if (inHeadings < 2 && wordCount > 500) {
119
- recommendations.push('Add keyword to at least 2 subheadings (H2/H3)');
120
- }
121
-
122
- // Find LSI keywords
123
- const lsiKeywords = findLSIKeywords(keyword, bodyText);
124
-
125
- // LSI recommendations
126
- const highRelevanceLSI = lsiKeywords.filter(l => l.relevance === 'high' && l.occurrences === 0);
127
- if (highRelevanceLSI.length > 0) {
128
- recommendations.push(`Consider adding related terms: ${highRelevanceLSI.slice(0, 3).map(l => l.keyword).join(', ')}`);
129
- }
130
-
131
- return {
132
- primaryKeyword,
133
- density: Math.round(density * 100) / 100,
134
- occurrences,
135
- wordCount,
136
- inTitle,
137
- inH1,
138
- inFirstParagraph,
139
- inLastParagraph,
140
- inHeadings,
141
- isKeywordStuffing,
142
- isUnderOptimized,
143
- lsiKeywords,
144
- recommendations,
145
- };
146
- }
147
-
148
- /**
149
- * Find LSI (Latent Semantic Indexing) keywords in content
150
- */
151
- export function findLSIKeywords(primaryKeyword: string, content: string): LSIKeyword[] {
152
- const keyword = primaryKeyword.toLowerCase();
153
- const contentLower = content.toLowerCase();
154
- const results: LSIKeyword[] = [];
155
-
156
- // Get semantically related terms based on keyword components
157
- const relatedTerms = new Set<string>();
158
-
159
- // Check against known LSI patterns
160
- for (const [pattern, related] of Object.entries(LSI_PATTERNS)) {
161
- if (keyword.includes(pattern) || pattern.includes(keyword)) {
162
- related.forEach(term => relatedTerms.add(term));
163
- }
164
- }
165
-
166
- // Add common modifiers and variations
167
- const modifiers = [
168
- 'best', 'top', 'free', 'online', 'guide', 'tutorial', 'how to',
169
- 'tips', 'examples', 'review', 'vs', 'alternative',
170
- ];
171
- modifiers.forEach(mod => {
172
- relatedTerms.add(`${mod} ${keyword}`);
173
- relatedTerms.add(`${keyword} ${mod}`);
174
- });
175
-
176
- // Add word variations
177
- const keywordWords = keyword.split(/\s+/);
178
- keywordWords.forEach(word => {
179
- // Plural/singular variations
180
- if (word.endsWith('s')) {
181
- relatedTerms.add(word.slice(0, -1));
182
- } else {
183
- relatedTerms.add(word + 's');
184
- }
185
- // -ing/-ed variations
186
- if (word.endsWith('ing')) {
187
- relatedTerms.add(word.slice(0, -3));
188
- relatedTerms.add(word.slice(0, -3) + 'ed');
189
- }
190
- });
191
-
192
- // Analyze content for related terms
193
- for (const term of relatedTerms) {
194
- if (term === keyword) continue;
195
-
196
- const regex = new RegExp(`\\b${escapeRegex(term)}\\b`, 'gi');
197
- const matches = contentLower.match(regex);
198
- const occurrences = matches ? matches.length : 0;
199
-
200
- // Determine relevance
201
- let relevance: 'high' | 'medium' | 'low' = 'low';
202
- if (term.includes(keyword) || keyword.includes(term)) {
203
- relevance = 'high';
204
- } else if (term.split(/\s+/).some(w => keyword.includes(w))) {
205
- relevance = 'medium';
206
- }
207
-
208
- results.push({ keyword: term, occurrences, relevance });
209
- }
210
-
211
- // Sort by relevance and occurrences
212
- results.sort((a, b) => {
213
- const relevanceOrder = { high: 0, medium: 1, low: 2 };
214
- if (relevanceOrder[a.relevance] !== relevanceOrder[b.relevance]) {
215
- return relevanceOrder[a.relevance] - relevanceOrder[b.relevance];
216
- }
217
- return b.occurrences - a.occurrences;
218
- });
219
-
220
- return results.slice(0, 20);
221
- }
222
-
223
- /**
224
- * Calculate TF-IDF score for keywords in content
225
- */
226
- export function calculateTFIDF(
227
- content: string,
228
- documentCount: number = 100
229
- ): Map<string, number> {
230
- const words = content.toLowerCase().split(/\s+/).filter(w => w.length > 3);
231
- const wordCount = words.length;
232
- const termFrequency = new Map<string, number>();
233
-
234
- // Count term frequency
235
- for (const word of words) {
236
- const clean = word.replace(/[^a-z]/g, '');
237
- if (clean.length > 3 && !isStopWord(clean)) {
238
- termFrequency.set(clean, (termFrequency.get(clean) || 0) + 1);
239
- }
240
- }
241
-
242
- // Calculate TF-IDF (simplified - assume inverse document frequency)
243
- const tfidf = new Map<string, number>();
244
- for (const [term, freq] of termFrequency) {
245
- const tf = freq / wordCount;
246
- // Simplified IDF - assume rare terms have higher value
247
- const idf = Math.log(documentCount / (1 + Math.min(freq, 10)));
248
- tfidf.set(term, tf * idf);
249
- }
250
-
251
- return tfidf;
252
- }
253
-
254
- function isStopWord(word: string): boolean {
255
- const stopWords = new Set([
256
- 'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for',
257
- 'of', 'with', 'by', 'from', 'as', 'is', 'was', 'are', 'were', 'been',
258
- 'be', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would',
259
- 'could', 'should', 'may', 'might', 'must', 'shall', 'can', 'need',
260
- 'this', 'that', 'these', 'those', 'what', 'which', 'who', 'when',
261
- 'where', 'why', 'how', 'all', 'each', 'every', 'both', 'few', 'more',
262
- 'most', 'other', 'some', 'such', 'only', 'same', 'than', 'too', 'very',
263
- 'just', 'also', 'now', 'here', 'there', 'then', 'once', 'your', 'their',
264
- ]);
265
- return stopWords.has(word);
266
- }
267
-
268
- function escapeRegex(str: string): string {
269
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
270
- }
271
-
272
- /**
273
- * Format keyword density report
274
- */
275
- export function formatKeywordDensityReport(analysis: KeywordDensityAnalysis): string {
276
- const lines: string[] = [];
277
-
278
- lines.push('');
279
- lines.push('═'.repeat(60));
280
- lines.push(' KEYWORD DENSITY ANALYSIS');
281
- lines.push('═'.repeat(60));
282
- lines.push('');
283
-
284
- // Primary keyword stats
285
- lines.push(`Primary Keyword: "${analysis.primaryKeyword}"`);
286
- lines.push('');
287
-
288
- // Density metrics
289
- const densityIcon = analysis.isKeywordStuffing ? '🔴' :
290
- analysis.isUnderOptimized ? '🟡' : '🟢';
291
- lines.push(`${densityIcon} Density: ${analysis.density}%`);
292
- lines.push(` Occurrences: ${analysis.occurrences} times in ${analysis.wordCount} words`);
293
-
294
- if (analysis.isKeywordStuffing) {
295
- lines.push(' ⚠️ WARNING: Possible keyword stuffing detected');
296
- }
297
- if (analysis.isUnderOptimized) {
298
- lines.push(' ⚠️ WARNING: Keyword may be under-optimized');
299
- }
300
- lines.push('');
301
-
302
- // Placement analysis
303
- lines.push('📍 KEYWORD PLACEMENT');
304
- lines.push('─'.repeat(60));
305
- lines.push(` In Title: ${analysis.inTitle ? '✅ Yes' : '❌ No'}`);
306
- lines.push(` In H1: ${analysis.inH1 ? '✅ Yes' : '❌ No'}`);
307
- lines.push(` In First Paragraph: ${analysis.inFirstParagraph ? '✅ Yes' : '❌ No'}`);
308
- lines.push(` In Subheadings: ${analysis.inHeadings > 0 ? `✅ ${analysis.inHeadings} times` : '❌ No'}`);
309
- lines.push(` In Last Paragraph: ${analysis.inLastParagraph ? '✅ Yes' : '❌ No'}`);
310
- lines.push('');
311
-
312
- // LSI Keywords
313
- const presentLSI = analysis.lsiKeywords.filter(l => l.occurrences > 0);
314
- const missingLSI = analysis.lsiKeywords.filter(l => l.occurrences === 0 && l.relevance !== 'low');
315
-
316
- if (presentLSI.length > 0) {
317
- lines.push('🔗 LSI KEYWORDS FOUND');
318
- lines.push('─'.repeat(60));
319
- for (const lsi of presentLSI.slice(0, 8)) {
320
- const relevanceIcon = lsi.relevance === 'high' ? '🟢' :
321
- lsi.relevance === 'medium' ? '🟡' : '⚪';
322
- lines.push(` ${relevanceIcon} "${lsi.keyword}" - ${lsi.occurrences} occurrences`);
323
- }
324
- lines.push('');
325
- }
326
-
327
- if (missingLSI.length > 0) {
328
- lines.push('📝 SUGGESTED LSI KEYWORDS TO ADD');
329
- lines.push('─'.repeat(60));
330
- for (const lsi of missingLSI.slice(0, 5)) {
331
- lines.push(` • ${lsi.keyword}`);
332
- }
333
- lines.push('');
334
- }
335
-
336
- // Recommendations
337
- if (analysis.recommendations.length > 0) {
338
- lines.push('💡 RECOMMENDATIONS');
339
- lines.push('─'.repeat(60));
340
- for (const rec of analysis.recommendations) {
341
- lines.push(` • ${rec}`);
342
- }
343
- lines.push('');
344
- }
345
-
346
- lines.push('═'.repeat(60));
347
-
348
- return lines.join('\n');
349
- }
@@ -1,262 +0,0 @@
1
- // Content Readability Analysis
2
- // Implements Flesch Reading Ease, Flesch-Kincaid, and other readability metrics
3
-
4
- export interface ReadabilityResult {
5
- // Flesch Reading Ease (0-100, higher = easier)
6
- fleschReadingEase: number;
7
- fleschGrade: string;
8
- // Flesch-Kincaid Grade Level (US school grade)
9
- fleschKincaidGrade: number;
10
- // Additional metrics
11
- avgSentenceLength: number;
12
- avgSyllablesPerWord: number;
13
- avgWordsPerParagraph: number;
14
- // Content stats
15
- wordCount: number;
16
- sentenceCount: number;
17
- paragraphCount: number;
18
- syllableCount: number;
19
- // Quality indicators
20
- passiveVoicePercentage: number;
21
- complexWordPercentage: number; // Words with 3+ syllables
22
- // Recommendations
23
- recommendations: string[];
24
- score: number; // 0-100 composite score
25
- }
26
-
27
- /**
28
- * Analyze content readability
29
- */
30
- export function analyzeReadability(text: string): ReadabilityResult {
31
- // Clean and normalize text
32
- const cleanText = text.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim();
33
-
34
- // Count elements
35
- const words = getWords(cleanText);
36
- const sentences = getSentences(cleanText);
37
- const paragraphs = getParagraphs(cleanText);
38
- const syllables = countTotalSyllables(words);
39
-
40
- const wordCount = words.length;
41
- const sentenceCount = Math.max(sentences.length, 1);
42
- const paragraphCount = Math.max(paragraphs.length, 1);
43
-
44
- // Calculate averages
45
- const avgSentenceLength = wordCount / sentenceCount;
46
- const avgSyllablesPerWord = syllables / Math.max(wordCount, 1);
47
- const avgWordsPerParagraph = wordCount / paragraphCount;
48
-
49
- // Flesch Reading Ease: 206.835 - 1.015 * (words/sentences) - 84.6 * (syllables/words)
50
- const fleschReadingEase = Math.max(0, Math.min(100,
51
- 206.835 - (1.015 * avgSentenceLength) - (84.6 * avgSyllablesPerWord)
52
- ));
53
-
54
- // Flesch-Kincaid Grade Level: 0.39 * (words/sentences) + 11.8 * (syllables/words) - 15.59
55
- const fleschKincaidGrade = Math.max(0,
56
- (0.39 * avgSentenceLength) + (11.8 * avgSyllablesPerWord) - 15.59
57
- );
58
-
59
- // Passive voice detection
60
- const passiveVoiceCount = countPassiveVoice(sentences);
61
- const passiveVoicePercentage = (passiveVoiceCount / sentenceCount) * 100;
62
-
63
- // Complex words (3+ syllables)
64
- const complexWords = words.filter(w => countSyllables(w) >= 3);
65
- const complexWordPercentage = (complexWords.length / Math.max(wordCount, 1)) * 100;
66
-
67
- // Generate recommendations
68
- const recommendations: string[] = [];
69
-
70
- if (avgSentenceLength > 20) {
71
- recommendations.push(`Shorten sentences. Average is ${avgSentenceLength.toFixed(1)} words (target: <20)`);
72
- }
73
-
74
- if (fleschReadingEase < 60) {
75
- recommendations.push('Simplify language. Content may be too difficult for general audiences');
76
- }
77
-
78
- if (passiveVoicePercentage > 15) {
79
- recommendations.push(`Reduce passive voice (${passiveVoicePercentage.toFixed(1)}%). Use active voice for clarity`);
80
- }
81
-
82
- if (complexWordPercentage > 25) {
83
- recommendations.push(`Reduce complex words (${complexWordPercentage.toFixed(1)}%). Use simpler alternatives`);
84
- }
85
-
86
- if (avgWordsPerParagraph > 150) {
87
- recommendations.push('Break up long paragraphs. Target 50-150 words per paragraph');
88
- }
89
-
90
- // Composite score
91
- const score = calculateReadabilityScore({
92
- fleschReadingEase,
93
- avgSentenceLength,
94
- passiveVoicePercentage,
95
- complexWordPercentage,
96
- });
97
-
98
- return {
99
- fleschReadingEase: Math.round(fleschReadingEase * 10) / 10,
100
- fleschGrade: getFleschGrade(fleschReadingEase),
101
- fleschKincaidGrade: Math.round(fleschKincaidGrade * 10) / 10,
102
- avgSentenceLength: Math.round(avgSentenceLength * 10) / 10,
103
- avgSyllablesPerWord: Math.round(avgSyllablesPerWord * 100) / 100,
104
- avgWordsPerParagraph: Math.round(avgWordsPerParagraph),
105
- wordCount,
106
- sentenceCount,
107
- paragraphCount,
108
- syllableCount: syllables,
109
- passiveVoicePercentage: Math.round(passiveVoicePercentage * 10) / 10,
110
- complexWordPercentage: Math.round(complexWordPercentage * 10) / 10,
111
- recommendations,
112
- score,
113
- };
114
- }
115
-
116
- function getWords(text: string): string[] {
117
- return text.split(/\s+/).filter(w => w.length > 0 && /[a-zA-Z]/.test(w));
118
- }
119
-
120
- function getSentences(text: string): string[] {
121
- return text.split(/[.!?]+/).filter(s => s.trim().length > 0);
122
- }
123
-
124
- function getParagraphs(text: string): string[] {
125
- return text.split(/\n\n+/).filter(p => p.trim().length > 0);
126
- }
127
-
128
- function countSyllables(word: string): number {
129
- word = word.toLowerCase().replace(/[^a-z]/g, '');
130
- if (word.length <= 3) return 1;
131
-
132
- // Count vowel groups
133
- const vowels = 'aeiouy';
134
- let count = 0;
135
- let prevWasVowel = false;
136
-
137
- for (const char of word) {
138
- const isVowel = vowels.includes(char);
139
- if (isVowel && !prevWasVowel) {
140
- count++;
141
- }
142
- prevWasVowel = isVowel;
143
- }
144
-
145
- // Adjust for silent e
146
- if (word.endsWith('e') && count > 1) {
147
- count--;
148
- }
149
-
150
- // Adjust for common endings
151
- if (word.endsWith('le') && word.length > 2 && !vowels.includes(word[word.length - 3])) {
152
- count++;
153
- }
154
-
155
- return Math.max(1, count);
156
- }
157
-
158
- function countTotalSyllables(words: string[]): number {
159
- return words.reduce((sum, word) => sum + countSyllables(word), 0);
160
- }
161
-
162
- function countPassiveVoice(sentences: string[]): number {
163
- const passivePatterns = [
164
- /\b(am|is|are|was|were|been|being)\s+\w+ed\b/i,
165
- /\b(am|is|are|was|were|been|being)\s+\w+en\b/i,
166
- /\b(get|gets|got|gotten|getting)\s+\w+ed\b/i,
167
- ];
168
-
169
- return sentences.filter(sentence =>
170
- passivePatterns.some(pattern => pattern.test(sentence))
171
- ).length;
172
- }
173
-
174
- function getFleschGrade(score: number): string {
175
- if (score >= 90) return 'Very Easy (5th grade)';
176
- if (score >= 80) return 'Easy (6th grade)';
177
- if (score >= 70) return 'Fairly Easy (7th grade)';
178
- if (score >= 60) return 'Standard (8th-9th grade)';
179
- if (score >= 50) return 'Fairly Difficult (10th-12th grade)';
180
- if (score >= 30) return 'Difficult (College)';
181
- return 'Very Difficult (College Graduate)';
182
- }
183
-
184
- function calculateReadabilityScore(metrics: {
185
- fleschReadingEase: number;
186
- avgSentenceLength: number;
187
- passiveVoicePercentage: number;
188
- complexWordPercentage: number;
189
- }): number {
190
- // Weight factors
191
- const weights = {
192
- flesch: 0.4,
193
- sentenceLength: 0.25,
194
- passiveVoice: 0.2,
195
- complexity: 0.15,
196
- };
197
-
198
- // Score components (0-100)
199
- const fleschScore = metrics.fleschReadingEase;
200
- const sentenceLengthScore = Math.max(0, 100 - Math.abs(metrics.avgSentenceLength - 15) * 3);
201
- const passiveVoiceScore = Math.max(0, 100 - metrics.passiveVoicePercentage * 5);
202
- const complexityScore = Math.max(0, 100 - metrics.complexWordPercentage * 2);
203
-
204
- const totalScore =
205
- fleschScore * weights.flesch +
206
- sentenceLengthScore * weights.sentenceLength +
207
- passiveVoiceScore * weights.passiveVoice +
208
- complexityScore * weights.complexity;
209
-
210
- return Math.round(totalScore);
211
- }
212
-
213
- /**
214
- * Format readability report
215
- */
216
- export function formatReadabilityReport(result: ReadabilityResult): string {
217
- const lines: string[] = [];
218
-
219
- lines.push('');
220
- lines.push('═'.repeat(60));
221
- lines.push(' READABILITY ANALYSIS');
222
- lines.push('═'.repeat(60));
223
- lines.push('');
224
-
225
- // Score overview
226
- const scoreIcon = result.score >= 70 ? '🟢' : result.score >= 50 ? '🟡' : '🔴';
227
- lines.push(`${scoreIcon} Readability Score: ${result.score}/100`);
228
- lines.push('');
229
-
230
- // Key metrics
231
- lines.push('📊 KEY METRICS');
232
- lines.push('─'.repeat(60));
233
- lines.push(` Flesch Reading Ease: ${result.fleschReadingEase} (${result.fleschGrade})`);
234
- lines.push(` Flesch-Kincaid Grade: ${result.fleschKincaidGrade}`);
235
- lines.push(` Avg Sentence Length: ${result.avgSentenceLength} words`);
236
- lines.push(` Passive Voice: ${result.passiveVoicePercentage}%`);
237
- lines.push(` Complex Words: ${result.complexWordPercentage}%`);
238
- lines.push('');
239
-
240
- // Content stats
241
- lines.push('📝 CONTENT STATS');
242
- lines.push('─'.repeat(60));
243
- lines.push(` Words: ${result.wordCount}`);
244
- lines.push(` Sentences: ${result.sentenceCount}`);
245
- lines.push(` Paragraphs: ${result.paragraphCount}`);
246
- lines.push(` Syllables: ${result.syllableCount}`);
247
- lines.push('');
248
-
249
- // Recommendations
250
- if (result.recommendations.length > 0) {
251
- lines.push('💡 RECOMMENDATIONS');
252
- lines.push('─'.repeat(60));
253
- for (const rec of result.recommendations) {
254
- lines.push(` • ${rec}`);
255
- }
256
- lines.push('');
257
- }
258
-
259
- lines.push('═'.repeat(60));
260
-
261
- return lines.join('\n');
262
- }