sanity-plugin-seo-copilot 0.2.5 → 0.2.7

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.
@@ -21,6 +21,11 @@ interface BuildSummaryInput {
21
21
  // When non-empty, broken/errored entries surface here as their own issue.
22
22
  linkCheckResults?: LinkCheckResult[];
23
23
  text: string;
24
+ // The editor's SELECTED primary keyword (effectivePrimary). Falls back to the
25
+ // analysis' top candidate when not supplied.
26
+ primaryKeyword?: string;
27
+ // Optional secondary target keyword - items flag where it's missing.
28
+ secondaryKeyword?: string;
24
29
  }
25
30
 
26
31
  function tokensContain(haystack: string, needle?: string): boolean {
@@ -31,14 +36,32 @@ function tokensContain(haystack: string, needle?: string): boolean {
31
36
  return tokens.every((t) => lower.includes(t));
32
37
  }
33
38
 
39
+ // True when the primary keyword leads the URL slug (within the first three words).
40
+ function keywordLeadsSlug(slug: string, primaryKeyword: string): boolean {
41
+ const slugWords = slug.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim().split(/\s+/).filter(Boolean);
42
+ const tokens = primaryKeyword.toLowerCase().split(/\s+/).filter((t) => t.length > 1);
43
+ if (tokens.length === 0 || slugWords.length === 0) return true;
44
+ let earliest = Infinity;
45
+ for (const t of tokens) {
46
+ const idx = slugWords.findIndex((w) => w.includes(t));
47
+ if (idx >= 0) earliest = Math.min(earliest, idx);
48
+ }
49
+ return earliest <= 2;
50
+ }
51
+
34
52
  // Conference previews, news briefs, recaps, and listicles are legitimately short.
35
53
  // Detect them so we don't nag "push past 1,500 words" on content that should be
36
54
  // short. Uses the contentCategory field when present, else title-pattern signals.
37
- function isShortFormContent(documentFields?: DocumentFields, title?: string): boolean {
38
- // Token-match the category (not substring) so "newsletter" doesn't match "news"
39
- // and "updated-guidelines" doesn't match "update".
55
+ export function isShortFormContent(documentFields?: DocumentFields, title?: string): boolean {
56
+ const cat = (documentFields?.contentCategory || "").toLowerCase().trim();
57
+ // Media formats (video / podcast / episode / webinar): the body text is a summary
58
+ // of the media, not the article. Match the category AS A WHOLE so a written piece
59
+ // in a compound category like "clinical video interviews" is NOT exempted.
60
+ if (/^(videos?|podcasts?|episodes?|webinars?|video series|peer exchange)$/.test(cat)) return true;
61
+ // Written short formats - token match (not substring) so "newsletter" doesn't match
62
+ // "news" and "updated-guidelines" doesn't match "update".
40
63
  const shortFormCats = new Set(["news", "brief", "preview", "recap", "update", "announcement", "roundup", "digest", "interview"]);
41
- const catTokens = (documentFields?.contentCategory || "").toLowerCase().split(/[^a-z]+/).filter(Boolean);
64
+ const catTokens = cat.split(/[^a-z]+/).filter(Boolean);
42
65
  if (catTokens.some((tok) => shortFormCats.has(tok))) return true;
43
66
  const t = (title || "").toLowerCase();
44
67
  if (/\b(preview|recap|roundup|digest)\b/.test(t)) return true;
@@ -51,7 +74,8 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
51
74
  // contentScore is in BuildSummaryInput for forward-compat but is now shown
52
75
  // directly on the Summary tab as a card with breakdown - no echo item needed here.
53
76
  const { analysis, deepAnalysis, documentFields, linkingSuggestions, linkCheckResults, text } = input;
54
- const primaryKeyword = analysis?.primaryKeyword?.term;
77
+ const primaryKeyword = input.primaryKeyword || analysis?.primaryKeyword?.term;
78
+ const secondaryKeyword = input.secondaryKeyword || "";
55
79
 
56
80
  // ---------- META: title ----------
57
81
  // Length warnings removed - the article title IS the page title and editors
@@ -152,39 +176,41 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
152
176
  }
153
177
  }
154
178
 
155
- // ---------- META: slug ----------
179
+ // ---------- URL slug (lives on the Other Recs tab) ----------
180
+ // No character-count nag and no "too long" - just scan for the primary keyword,
181
+ // ideally leading the URL. Keep the guidance vague (short + descriptive).
156
182
  const slug = documentFields?.slug;
157
183
  if (!slug) {
158
184
  items.push({
159
185
  id: "meta-slug-missing",
160
186
  category: "meta",
161
- severity: "error",
187
+ severity: "warning",
162
188
  label: "URL slug missing",
163
189
  description: "No URL slug set.",
164
- howToFix: "Add a clean, hyphenated slug 3-5 words long that contains the primary keyword.",
165
- jumpTo: { tab: "meta", anchorId: "anchor-url-slug" },
190
+ howToFix: "Add a short, descriptive slug that leads with your primary keyword.",
191
+ jumpTo: { tab: "technical", anchorId: "anchor-url-slug" },
166
192
  });
167
193
  } else {
168
- if (slug.length > 60) {
194
+ const slugAsWords = slug.replace(/-/g, " ");
195
+ if (primaryKeyword && !tokensContain(slugAsWords, primaryKeyword)) {
169
196
  items.push({
170
- id: "meta-slug-long",
197
+ id: "meta-slug-no-keyword",
171
198
  category: "meta",
172
- severity: "warning",
173
- label: "URL slug is long",
174
- description: `Slug is ${slug.length} characters. 3-5 words is the 2026 sweet spot.`,
175
- howToFix: "Shorten the slug to keep the URL clean and scannable.",
176
- jumpTo: { tab: "meta", anchorId: "anchor-url-slug" },
199
+ severity: "opportunity",
200
+ label: "Primary keyword not in URL",
201
+ description: `The URL doesn't include "${primaryKeyword}".`,
202
+ howToFix: "Work the primary keyword into the URL, ideally at the start. Keep the URL short and descriptive.",
203
+ jumpTo: { tab: "technical", anchorId: "anchor-url-slug" },
177
204
  });
178
- }
179
- if (primaryKeyword && !tokensContain(slug.replace(/-/g, " "), primaryKeyword)) {
205
+ } else if (primaryKeyword && !keywordLeadsSlug(slug, primaryKeyword)) {
180
206
  items.push({
181
- id: "meta-slug-no-keyword",
207
+ id: "meta-slug-keyword-late",
182
208
  category: "meta",
183
209
  severity: "opportunity",
184
- label: "Primary keyword not in URL",
185
- description: `Slug doesn't contain "${primaryKeyword}".`,
186
- howToFix: "Rework the slug to include the primary keyword (hyphenated). URLs with keywords still get a modest ranking lift.",
187
- jumpTo: { tab: "meta", anchorId: "anchor-url-slug" },
210
+ label: "Primary keyword late in URL",
211
+ description: `"${primaryKeyword}" is in the URL but not near the start.`,
212
+ howToFix: "Lead the URL with the primary keyword so it stays visible if the address gets shortened.",
213
+ jumpTo: { tab: "technical", anchorId: "anchor-url-slug" },
188
214
  });
189
215
  }
190
216
  }
@@ -193,9 +219,12 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
193
219
  // 450 is the body-text floor (per editor guidance). 1,500+ is the competitive
194
220
  // target for HCP clinical articles.
195
221
  const wordCount = text ? text.trim().split(/\s+/).filter(Boolean).length : 0;
222
+ // Videos, podcasts, news, briefs, etc. are legitimately short (the body is a
223
+ // summary, not the deliverable), so they're exempt from the word-count floor.
224
+ const isShortForm = isShortFormContent(documentFields, title);
196
225
  if (wordCount === 0) {
197
226
  // No item - empty doc state is handled by the empty-state UI elsewhere
198
- } else if (wordCount < 450) {
227
+ } else if (wordCount < 450 && !isShortForm) {
199
228
  items.push({
200
229
  id: "content-too-short",
201
230
  category: "content",
@@ -205,7 +234,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
205
234
  howToFix: "Add more body text. A link or image on its own doesn't count - the article needs real written substance.",
206
235
  jumpTo: { tab: "technical", anchorId: "anchor-word-count" },
207
236
  });
208
- } else if (wordCount < 1500 && !isShortFormContent(documentFields, title)) {
237
+ } else if (wordCount < 1500 && !isShortForm) {
209
238
  items.push({
210
239
  id: "content-short-for-clinical",
211
240
  category: "content",
@@ -374,7 +403,11 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
374
403
  // Image titles (the hover tooltip) - the director treats this as an SEO signal.
375
404
  const imageTitleCount = (documentFields?.imageTitles || []).length;
376
405
  const totalImageCount = documentFields?.imageCount ?? 0;
377
- const missingTitles = totalImageCount > 0 ? totalImageCount - imageTitleCount : 0;
406
+ // Only flag missing titles when SOME image already has one (i.e. the schema
407
+ // actually exposes a title field). MJH combines alt + title into a single
408
+ // "Image ALT/Title Text" field, so imageTitleCount is always 0 there - flagging
409
+ // every image as "missing a title" was a false alarm. Mirrors TechnicalTab's skip.
410
+ const missingTitles = totalImageCount > 0 && imageTitleCount > 0 ? totalImageCount - imageTitleCount : 0;
378
411
  if (missingTitles > 0) {
379
412
  items.push({
380
413
  id: "images-titles-missing",
@@ -407,7 +440,10 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
407
440
  // analysis has fetched real SEMrush volumes (realtime leaves volume null), so
408
441
  // gate on deepAnalysis to avoid false warnings mid-typing.
409
442
  const primaryVolume = analysis?.primaryKeyword?.volume;
410
- if (deepAnalysis && (primaryVolume === 0 || primaryVolume === null)) {
443
+ // Only an explicit measured zero means "nobody searches this". A null volume
444
+ // means SEMrush simply had no data for the term (common for niche clinical
445
+ // phrases) - warning on null produced false positives, so we don't.
446
+ if (deepAnalysis && primaryVolume === 0) {
411
447
  items.push({
412
448
  id: "keywords-primary-zero-volume",
413
449
  category: "keywords",
@@ -420,6 +456,59 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
420
456
  }
421
457
  }
422
458
 
459
+ // ---------- SECONDARY KEYWORD COVERAGE ----------
460
+ // When the editor has chosen an optional secondary keyword, flag the spots it's
461
+ // missing from so the "primary in, secondary out" gaps are visible at a glance.
462
+ if (secondaryKeyword) {
463
+ if (text && !tokensContain(text, secondaryKeyword)) {
464
+ items.push({
465
+ id: "secondary-not-in-body",
466
+ category: "keywords",
467
+ severity: "opportunity",
468
+ label: "Secondary keyword not in body",
469
+ description: `Your secondary keyword "${secondaryKeyword}" doesn't appear in the article body.`,
470
+ howToFix: "Work the secondary keyword into a body paragraph or two where it reads naturally.",
471
+ jumpTo: { tab: "keywords" },
472
+ });
473
+ }
474
+ const titleVal = documentFields?.title;
475
+ if (titleVal && !tokensContain(titleVal, secondaryKeyword)) {
476
+ items.push({
477
+ id: "secondary-not-in-title",
478
+ category: "meta",
479
+ severity: "opportunity",
480
+ label: "Secondary keyword not in title",
481
+ description: `The title leads with the primary keyword but doesn't include "${secondaryKeyword}".`,
482
+ howToFix: "If it fits naturally, include the secondary keyword in the title too - but never at the cost of the primary leading.",
483
+ jumpTo: { tab: "meta", anchorId: "anchor-meta-title" },
484
+ });
485
+ }
486
+ const metaVal = documentFields?.metaDescription;
487
+ if (metaVal && !tokensContain(metaVal, secondaryKeyword)) {
488
+ items.push({
489
+ id: "secondary-not-in-meta",
490
+ category: "meta",
491
+ severity: "opportunity",
492
+ label: "Secondary keyword not in meta description",
493
+ description: `The meta description doesn't include "${secondaryKeyword}".`,
494
+ howToFix: "Add the secondary keyword to the meta description if it reads naturally.",
495
+ jumpTo: { tab: "meta", anchorId: "anchor-meta-description" },
496
+ });
497
+ }
498
+ const slugVal = documentFields?.slug;
499
+ if (slugVal && !tokensContain(slugVal.replace(/-/g, " "), secondaryKeyword)) {
500
+ items.push({
501
+ id: "secondary-not-in-url",
502
+ category: "meta",
503
+ severity: "opportunity",
504
+ label: "Secondary keyword not in URL",
505
+ description: `The URL doesn't include "${secondaryKeyword}".`,
506
+ howToFix: "Optionally include the secondary keyword in the URL after the primary - only if the slug stays short and clean.",
507
+ jumpTo: { tab: "technical", anchorId: "anchor-url-slug" },
508
+ });
509
+ }
510
+ }
511
+
423
512
  // ---------- CONTENT SCORE ----------
424
513
  // The score itself is shown as a card at the top of the Summary tab with a
425
514
  // visible per-component breakdown, so we DON'T echo it here as a separate