seomd-cli 1.3.0 → 2.0.0

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 (37) hide show
  1. package/PROVIDER_SPEC.md +125 -0
  2. package/README.md +54 -190
  3. package/SKILL.md +44 -0
  4. package/bin/seomd.js +78 -31
  5. package/package.json +11 -7
  6. package/src/commands/analyze.js +92 -92
  7. package/src/commands/check.js +301 -0
  8. package/src/commands/doctor.js +109 -0
  9. package/src/commands/generate.js +202 -0
  10. package/src/commands/init.js +127 -79
  11. package/src/commands/status.js +107 -79
  12. package/src/commands/sync.js +86 -72
  13. package/src/commands/validate.js +52 -25
  14. package/src/generators/directory.js +44 -143
  15. package/src/generators/reverse.js +46 -56
  16. package/src/generators/seomd.js +197 -44
  17. package/src/providers/http.js +67 -0
  18. package/src/providers/index.js +194 -0
  19. package/src/utils/agent-wiring.js +97 -0
  20. package/src/utils/api-client.js +14 -56
  21. package/src/utils/constants.js +110 -12
  22. package/src/utils/parser.js +114 -25
  23. package/src/utils/repo-scanner.js +207 -0
  24. package/src/utils/writeback.js +181 -100
  25. package/src/validators/seomd.js +255 -93
  26. package/scripts/release-contributors.js +0 -196
  27. package/scripts/release-notes.js +0 -194
  28. package/src/templates/blog/SEO.REVERSE.md +0 -176
  29. package/src/templates/blog/SEO.md +0 -348
  30. package/src/templates/ecommerce/SEO.REVERSE.md +0 -199
  31. package/src/templates/ecommerce/SEO.md +0 -354
  32. package/src/templates/local/SEO.REVERSE.md +0 -222
  33. package/src/templates/local/SEO.md +0 -360
  34. package/src/templates/marketplace/SEO.REVERSE.md +0 -222
  35. package/src/templates/marketplace/SEO.md +0 -360
  36. package/src/templates/saas/SEO.REVERSE.md +0 -268
  37. package/src/templates/saas/SEO.md +0 -372
@@ -1,129 +1,210 @@
1
1
  import fs from 'fs-extra';
2
2
  import path from 'path';
3
3
  import YAML from 'yaml';
4
- import { writeSeoMd } from './parser.js';
4
+ import { INTENT_CATEGORIES } from './constants.js';
5
5
 
6
6
  /**
7
- * Merges platform analysis blocks back into the SEO.md YAML document.
8
- *
9
- * @param {YAML.Document} doc - The YAML Document of SEO.md
10
- * @param {any} response - The API response from analyze/sync
11
- * @param {string} cwd - Current working directory
7
+ * Sanitizes a page ID so it can never escape `.seo/pages/`.
12
8
  */
13
- export async function writeAnalysisToSeoMd(doc, response, cwd) {
14
- const lastAnalyzed = response.aeo_analysis.last_analyzed;
15
- const nextAnalysis = response.aeo_analysis.next_analysis;
16
-
17
- // 1. Update AEO analysis
18
- doc.setIn(['aeo', '_analysis', 'source'], 'foxcite');
19
- doc.setIn(['aeo', '_analysis', 'overall_citation_rate'], response.aeo_analysis.overall_citation_rate);
20
- doc.setIn(['aeo', '_analysis', 'overall_gap_score'], response.aeo_analysis.overall_gap_score);
21
- doc.setIn(['aeo', '_analysis', 'engines_tracked'], response.aeo_analysis.engines_tracked);
22
- doc.setIn(['aeo', '_analysis', 'last_analyzed'], lastAnalyzed);
23
- doc.setIn(['aeo', '_analysis', 'next_analysis'], nextAnalysis);
24
-
25
- // 2. Update Intent analysis
26
- doc.setIn(['intent', '_analysis', 'source'], 'foxcite');
27
- doc.setIn(['intent', '_analysis', 'last_analyzed'], lastAnalyzed);
28
- doc.setIn(['intent', '_analysis', 'next_analysis'], nextAnalysis);
29
-
30
- const categories = ['informational', 'comparison', 'transactional', 'reputational', 'category'];
31
- for (const cat of categories) {
32
- if (response.intent_analysis && response.intent_analysis[cat]) {
33
- doc.setIn(['intent', '_analysis', cat, 'citation_rate'], response.intent_analysis[cat].citation_rate);
34
- doc.setIn(['intent', '_analysis', cat, 'top_cited_competitor'], response.intent_analysis[cat].top_cited_competitor);
35
- doc.setIn(['intent', '_analysis', cat, 'gap_score'], response.intent_analysis[cat].gap_score);
36
- doc.setIn(['intent', '_analysis', cat, 'trend'], response.intent_analysis[cat].trend || 'stable');
9
+ export function sanitizePageId(id) {
10
+ const cleaned = String(id || 'page')
11
+ .replace(/[^a-zA-Z0-9_-]/g, '-')
12
+ .replace(/-+/g, '-')
13
+ .replace(/^-+|-+$/g, '');
14
+ return cleaned || 'page';
15
+ }
16
+
17
+ function mergePagesById(existingPages = [], incomingPages = []) {
18
+ const map = new Map();
19
+ for (const p of existingPages) {
20
+ if (p && p.id) map.set(p.id, p);
21
+ }
22
+ for (const p of incomingPages) {
23
+ if (p && p.id) map.set(p.id, p);
24
+ }
25
+ return Array.from(map.values());
26
+ }
27
+
28
+ /**
29
+ * Writes (and merges) platform analysis metrics into `.seo/STATUS.yml`.
30
+ *
31
+ * @param {any} _doc - Deprecated parameter kept for backwards signature compatibility
32
+ * @param {any} response - API response from analyze/sync
33
+ * @param {string} cwd - Project root directory
34
+ * @param {{ provider?: string }} [options]
35
+ */
36
+ export async function writeAnalysisToSeoMd(_doc, response, cwd, options = {}) {
37
+ const seomdDir = path.join(cwd, '.seo');
38
+ await fs.ensureDir(seomdDir);
39
+ const statusPath = path.join(seomdDir, 'STATUS.yml');
40
+
41
+ let existing = {};
42
+ if (await fs.pathExists(statusPath)) {
43
+ try {
44
+ existing = YAML.parse(await fs.readFile(statusPath, 'utf8')) || {};
45
+ } catch {
46
+ existing = {};
47
+ }
48
+ }
49
+
50
+ const aeo = response?.aeo_analysis || {};
51
+ const incomingPages = Array.isArray(response?.page_analysis)
52
+ ? response.page_analysis.map((p) => ({
53
+ id: p.id,
54
+ url: p.url,
55
+ citation_rate: p.citation_rate ?? null,
56
+ gap_score: p.gap_score ?? null,
57
+ top_cited_competitor: p.top_cited_competitor ?? null,
58
+ }))
59
+ : [];
60
+
61
+ const existingPages = Array.isArray(existing?.pages?.pages) ? existing.pages.pages : [];
62
+ const mergedPages = mergePagesById(existingPages, incomingPages);
63
+
64
+ const mergedIntent = { ...(existing.intent || {}) };
65
+ if (response?.intent_analysis && typeof response.intent_analysis === 'object') {
66
+ for (const cat of INTENT_CATEGORIES) {
67
+ if (response.intent_analysis[cat]) {
68
+ mergedIntent[cat] = {
69
+ citation_rate: response.intent_analysis[cat].citation_rate ?? null,
70
+ top_cited_competitor: response.intent_analysis[cat].top_cited_competitor ?? null,
71
+ gap_score: response.intent_analysis[cat].gap_score ?? null,
72
+ trend: response.intent_analysis[cat].trend || 'stable',
73
+ };
74
+ }
37
75
  }
38
76
  }
39
77
 
40
- // 3. Update Pages analysis
41
- doc.setIn(['pages', '_analysis', 'source'], 'foxcite');
42
- doc.setIn(['pages', '_analysis', 'last_analyzed'], lastAnalyzed);
43
-
44
- const pageSummaries = response.page_analysis.map(p => ({
45
- id: p.id,
46
- url: p.url,
47
- citation_rate: p.citation_rate,
48
- gap_score: p.gap_score,
49
- top_cited_competitor: p.top_cited_competitor
50
- }));
51
- doc.setIn(['pages', '_analysis', 'pages'], pageSummaries);
52
- doc.setIn(['pages', '_analysis', 'missing_pages'], []);
53
- doc.setIn(['pages', '_analysis', 'build_order_recommendation'], []);
54
-
55
- // 4. Update Keywords analysis
56
- doc.setIn(['keywords', '_analysis', 'source'], 'foxcite');
57
- doc.setIn(['keywords', '_analysis', 'last_analyzed'], lastAnalyzed);
58
- doc.setIn(['keywords', '_analysis', 'next_analysis'], nextAnalysis);
59
-
60
- // Write back to SEO.md
61
- await writeSeoMd(doc, cwd);
78
+ const source = response?.provider || options.provider || existing.source || 'custom';
79
+
80
+ const statusData = {
81
+ source,
82
+ last_analyzed: aeo.last_analyzed ?? existing.last_analyzed ?? null,
83
+ next_analysis: aeo.next_analysis ?? existing.next_analysis ?? null,
84
+ aeo: {
85
+ overall_citation_rate: aeo.overall_citation_rate ?? existing.aeo?.overall_citation_rate ?? null,
86
+ overall_gap_score: aeo.overall_gap_score ?? existing.aeo?.overall_gap_score ?? null,
87
+ engines_tracked: aeo.engines_tracked ?? existing.aeo?.engines_tracked ?? [],
88
+ },
89
+ intent: mergedIntent,
90
+ pages: {
91
+ pages: mergedPages,
92
+ missing_pages: existing.pages?.missing_pages || [],
93
+ build_order_recommendation: existing.pages?.build_order_recommendation || [],
94
+ },
95
+ keywords: existing.keywords || {},
96
+ };
97
+
98
+ const header = [
99
+ '# .seo/STATUS.yml',
100
+ '#',
101
+ '# Auto-generated by your connected SEO.md provider.',
102
+ '# Do not edit manually — run `npx seomd-cli analyze` or `npx seomd-cli sync` to update.',
103
+ '',
104
+ ].join('\n');
105
+
106
+ await fs.writeFile(statusPath, `${header}\n${YAML.stringify(statusData)}`, 'utf8');
62
107
  }
63
108
 
109
+ export const writeStatusYml = writeAnalysisToSeoMd;
110
+
64
111
  /**
65
- * Generates and writes the SEO.REVERSE.md file.
66
- *
67
- * @param {string} cwd - Current working directory
68
- * @param {any} response - The API response from analyze/sync
112
+ * Generates and merges `.seo/REVERSE.md`.
113
+ *
114
+ * @param {string} cwd - Project root directory
115
+ * @param {any} response - API response from analyze/sync
116
+ * @param {string} [defaultDomain='example.com']
117
+ * @param {string} [defaultBrand='My Brand']
118
+ * @param {{ provider?: string }} [options]
69
119
  */
70
- export async function writeReverseMd(cwd, response, defaultDomain = 'example.com', defaultBrand = 'My Brand') {
71
- const reversePath = path.join(cwd, 'SEO.REVERSE.md');
72
-
73
- const reversePages = response.page_analysis.map(p => ({
74
- id: p.id,
75
- url: p.url,
76
- status: p.status || 'planned',
77
- citation_rate: p.citation_rate,
78
- gap_score: p.gap_score,
79
- top_cited_competitor: p.top_cited_competitor,
80
- why_page_won: p.why_page_won || [],
81
- citation_hooks: p.citation_hooks || [],
82
- gaps_for_my_brand: p.gaps_for_brand || [],
83
- remediation_playbook: p.remediation_playbook || [],
84
- fastest_win: p.fastest_win || { action: null, estimated_gap_score_improvement: null, effort: null },
85
- suggested_content_outline: p.suggested_content_outline || []
86
- }));
87
-
88
- const primaryCompetitor = response.intent_analysis?.comparison?.top_cited_competitor || 'None';
120
+ export async function writeReverseMd(
121
+ cwd,
122
+ response,
123
+ defaultDomain = 'example.com',
124
+ defaultBrand = 'My Brand',
125
+ options = {}
126
+ ) {
127
+ const seomdDir = path.join(cwd, '.seo');
128
+ await fs.ensureDir(seomdDir);
129
+ const reversePath = path.join(seomdDir, 'REVERSE.md');
130
+
131
+ let existing = {};
132
+ if (await fs.pathExists(reversePath)) {
133
+ try {
134
+ existing = YAML.parse(await fs.readFile(reversePath, 'utf8')) || {};
135
+ } catch {
136
+ existing = {};
137
+ }
138
+ }
139
+
140
+ const incomingPages = Array.isArray(response?.page_analysis)
141
+ ? response.page_analysis.map((p) => ({
142
+ id: p.id,
143
+ url: p.url,
144
+ status: p.status || 'planned',
145
+ citation_rate: p.citation_rate ?? null,
146
+ gap_score: p.gap_score ?? null,
147
+ top_cited_competitor: p.top_cited_competitor ?? null,
148
+ why_page_won: p.why_page_won || [],
149
+ citation_hooks: p.citation_hooks || [],
150
+ gaps_for_brand: p.gaps_for_brand || p.gaps_for_my_brand || [],
151
+ remediation_playbook: p.remediation_playbook || [],
152
+ fastest_win: p.fastest_win || { action: null, estimated_gap_score_improvement: null, effort: null },
153
+ suggested_content_outline: p.suggested_content_outline || [],
154
+ }))
155
+ : [];
156
+
157
+ const existingPages = Array.isArray(existing.pages) ? existing.pages : [];
158
+ const mergedPages = mergePagesById(existingPages, incomingPages);
159
+
160
+ const aeo = response?.aeo_analysis || {};
161
+ const primaryCompetitor =
162
+ response?.intent_analysis?.comparison?.top_cited_competitor ||
163
+ existing.primary_competitor ||
164
+ null;
165
+
166
+ const source = response?.provider || options.provider || existing.source || 'custom';
89
167
 
90
168
  const reverseDoc = {
91
- domain: response.domain || defaultDomain,
92
- brand: response.brand_name || defaultBrand,
169
+ domain: response?.domain || existing.domain || defaultDomain,
170
+ brand: response?.brand_name || existing.brand || defaultBrand,
93
171
  primary_competitor: primaryCompetitor,
94
- last_analyzed: response.aeo_analysis.last_analyzed,
95
- next_analysis: response.aeo_analysis.next_analysis,
96
- source: 'foxcite',
97
- pages: reversePages
172
+ last_analyzed: aeo.last_analyzed ?? existing.last_analyzed ?? null,
173
+ next_analysis: aeo.next_analysis ?? existing.next_analysis ?? null,
174
+ source,
175
+ pages: mergedPages,
176
+ ...(Array.isArray(existing.competitors) ? { competitors: existing.competitors } : {}),
98
177
  };
99
178
 
100
- const header = `# SEO.REVERSE.md
101
- #
102
- # This file is generated by the foxcite platform.
103
- # Do not edit manually — run \`npx seomd analyze\` to update.
104
- # Run \`npx seomd sync\` to pull latest intelligence.
105
-
106
- `;
179
+ const header = [
180
+ '# .seo/REVERSE.md',
181
+ '#',
182
+ '# Auto-generated by your connected SEO.md provider.',
183
+ '# Do not edit manually — run `npx seomd-cli analyze` or `npx seomd-cli sync` to update.',
184
+ '',
185
+ ].join('\n');
107
186
 
108
- const yamlContent = YAML.stringify(reverseDoc);
109
- await fs.writeFile(reversePath, header + yamlContent, 'utf8');
187
+ await fs.writeFile(reversePath, `${header}\n${YAML.stringify(reverseDoc)}`, 'utf8');
110
188
  }
111
189
 
112
190
  /**
113
- * Writes individual page playbooks into .seo/pages/{id}.md files.
114
- *
115
- * @param {string} cwd - Current working directory
116
- * @param {any} response - The API response from analyze/sync
191
+ * Writes individual page playbooks into `.seo/pages/{sanitized-id}.md`.
192
+ *
193
+ * @param {string} cwd - Project root directory
194
+ * @param {any} response - API response from analyze/sync
117
195
  */
118
196
  export async function writePageAnalysis(cwd, response) {
119
- const seomdDir = path.join(cwd, '.seo');
120
- const pagesDir = path.join(seomdDir, 'pages');
121
-
197
+ const pagesDir = path.resolve(cwd, '.seo', 'pages');
122
198
  await fs.ensureDir(pagesDir);
123
199
 
124
- for (const page of response.page_analysis) {
125
- if (page.markdown_content) {
126
- const filePath = path.join(pagesDir, `${page.id}.md`);
200
+ const pages = Array.isArray(response?.page_analysis) ? response.page_analysis : [];
201
+ for (const page of pages) {
202
+ if (page && page.markdown_content) {
203
+ const safeId = sanitizePageId(page.id);
204
+ const filePath = path.resolve(pagesDir, `${safeId}.md`);
205
+ if (!filePath.startsWith(pagesDir + path.sep)) {
206
+ continue;
207
+ }
127
208
  await fs.writeFile(filePath, page.markdown_content, 'utf8');
128
209
  }
129
210
  }