sanity-plugin-seo-copilot 0.2.7 → 0.2.11

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.
@@ -28,26 +28,7 @@ interface BuildSummaryInput {
28
28
  secondaryKeyword?: string;
29
29
  }
30
30
 
31
- function tokensContain(haystack: string, needle?: string): boolean {
32
- if (!needle) return true;
33
- const tokens = needle.toLowerCase().split(/\s+/).filter((t) => t.length > 1);
34
- if (tokens.length === 0) return true;
35
- const lower = haystack.toLowerCase();
36
- return tokens.every((t) => lower.includes(t));
37
- }
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
- }
31
+ import { containsKeywordTokens, keywordLeadsSlug } from "./utils";
51
32
 
52
33
  // Conference previews, news briefs, recaps, and listicles are legitimately short.
53
34
  // Detect them so we don't nag "push past 1,500 words" on content that should be
@@ -76,6 +57,16 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
76
57
  const { analysis, deepAnalysis, documentFields, linkingSuggestions, linkCheckResults, text } = input;
77
58
  const primaryKeyword = input.primaryKeyword || analysis?.primaryKeyword?.term;
78
59
  const secondaryKeyword = input.secondaryKeyword || "";
60
+ // Body-only text: useDocumentContent prepends the document title so the AI
61
+ // analysis includes it, but word-count and keyword-in-body checks must not
62
+ // count the title as body content. Strip the title prefix when present.
63
+ const bodyText = (() => {
64
+ if (!documentFields?.title) return text;
65
+ if (!text.startsWith(documentFields.title)) return text;
66
+ const rest = text.slice(documentFields.title.length);
67
+ if (rest === "" || /^\s/.test(rest)) return rest.trimStart();
68
+ return text;
69
+ })();
79
70
 
80
71
  // ---------- META: title ----------
81
72
  // Length warnings removed - the article title IS the page title and editors
@@ -94,7 +85,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
94
85
  jumpTo: { tab: "meta", anchorId: "anchor-meta-title" },
95
86
  });
96
87
  } else if (primaryKeyword) {
97
- if (!tokensContain(title, primaryKeyword)) {
88
+ if (!containsKeywordTokens(title, primaryKeyword)) {
98
89
  items.push({
99
90
  id: "meta-title-no-keyword",
100
91
  category: "meta",
@@ -163,7 +154,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
163
154
  jumpTo: { tab: "meta", anchorId: "anchor-meta-description" },
164
155
  });
165
156
  }
166
- if (primaryKeyword && !tokensContain(meta, primaryKeyword)) {
157
+ if (primaryKeyword && !containsKeywordTokens(meta, primaryKeyword)) {
167
158
  items.push({
168
159
  id: "meta-desc-no-keyword",
169
160
  category: "meta",
@@ -192,7 +183,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
192
183
  });
193
184
  } else {
194
185
  const slugAsWords = slug.replace(/-/g, " ");
195
- if (primaryKeyword && !tokensContain(slugAsWords, primaryKeyword)) {
186
+ if (primaryKeyword && !containsKeywordTokens(slugAsWords, primaryKeyword)) {
196
187
  items.push({
197
188
  id: "meta-slug-no-keyword",
198
189
  category: "meta",
@@ -218,7 +209,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
218
209
  // ---------- CONTENT: word count ----------
219
210
  // 450 is the body-text floor (per editor guidance). 1,500+ is the competitive
220
211
  // target for HCP clinical articles.
221
- const wordCount = text ? text.trim().split(/\s+/).filter(Boolean).length : 0;
212
+ const wordCount = bodyText ? bodyText.trim().split(/\s+/).filter(Boolean).length : 0;
222
213
  // Videos, podcasts, news, briefs, etc. are legitimately short (the body is a
223
214
  // summary, not the deliverable), so they're exempt from the word-count floor.
224
215
  const isShortForm = isShortFormContent(documentFields, title);
@@ -256,7 +247,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
256
247
  const h2Count = headingsDetailed
257
248
  ? headingsDetailed.filter((h) => h.level === 2 || h.level === 3).length
258
249
  : headings.length;
259
- if (headings.length === 0 && wordCount > 200 && wordCount < 1000) {
250
+ if (h2Count === 0 && wordCount > 200 && wordCount < 1000) {
260
251
  items.push({
261
252
  id: "headings-missing",
262
253
  category: "headings",
@@ -369,7 +360,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
369
360
  if (GENERIC_FN_RE.test(base)) return true;
370
361
  if (/_/.test(base) || /\s/.test(base)) return true;
371
362
  const words = base.split("-").filter(Boolean);
372
- return words.length < 2 || words.length > 7;
363
+ return words.length < 2 || words.length > 5;
373
364
  });
374
365
  if (badFilenames.length > 0) {
375
366
  items.push({
@@ -387,7 +378,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
387
378
  if (!trimmed) return false;
388
379
  const len = trimmed.length;
389
380
  const wc = trimmed.split(/\s+/).filter(Boolean).length;
390
- return len > 125 || wc < 2;
381
+ return len >= 125 || wc < 2;
391
382
  });
392
383
  if (badAlts.length > 0) {
393
384
  items.push({
@@ -425,7 +416,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
425
416
  // Keywords haven't loaded - skip
426
417
  } else {
427
418
  // If primary is missing from body, flag it
428
- if (text && !tokensContain(text, primaryKeyword)) {
419
+ if (bodyText && !containsKeywordTokens(bodyText, primaryKeyword)) {
429
420
  items.push({
430
421
  id: "keywords-primary-not-in-body",
431
422
  category: "keywords",
@@ -460,7 +451,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
460
451
  // When the editor has chosen an optional secondary keyword, flag the spots it's
461
452
  // missing from so the "primary in, secondary out" gaps are visible at a glance.
462
453
  if (secondaryKeyword) {
463
- if (text && !tokensContain(text, secondaryKeyword)) {
454
+ if (bodyText && !containsKeywordTokens(bodyText, secondaryKeyword)) {
464
455
  items.push({
465
456
  id: "secondary-not-in-body",
466
457
  category: "keywords",
@@ -472,19 +463,19 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
472
463
  });
473
464
  }
474
465
  const titleVal = documentFields?.title;
475
- if (titleVal && !tokensContain(titleVal, secondaryKeyword)) {
466
+ if (titleVal && !containsKeywordTokens(titleVal, secondaryKeyword)) {
476
467
  items.push({
477
468
  id: "secondary-not-in-title",
478
469
  category: "meta",
479
470
  severity: "opportunity",
480
471
  label: "Secondary keyword not in title",
481
- description: `The title leads with the primary keyword but doesn't include "${secondaryKeyword}".`,
472
+ description: `The title doesn't include your secondary keyword "${secondaryKeyword}".`,
482
473
  howToFix: "If it fits naturally, include the secondary keyword in the title too - but never at the cost of the primary leading.",
483
474
  jumpTo: { tab: "meta", anchorId: "anchor-meta-title" },
484
475
  });
485
476
  }
486
477
  const metaVal = documentFields?.metaDescription;
487
- if (metaVal && !tokensContain(metaVal, secondaryKeyword)) {
478
+ if (metaVal && !containsKeywordTokens(metaVal, secondaryKeyword)) {
488
479
  items.push({
489
480
  id: "secondary-not-in-meta",
490
481
  category: "meta",
@@ -496,7 +487,7 @@ export function buildSummaryItems(input: BuildSummaryInput): SummaryItem[] {
496
487
  });
497
488
  }
498
489
  const slugVal = documentFields?.slug;
499
- if (slugVal && !tokensContain(slugVal.replace(/-/g, " "), secondaryKeyword)) {
490
+ if (slugVal && !containsKeywordTokens(slugVal.replace(/-/g, " "), secondaryKeyword)) {
500
491
  items.push({
501
492
  id: "secondary-not-in-url",
502
493
  category: "meta",
@@ -0,0 +1,35 @@
1
+ // Token-based keyword match: "selinexor in myelofibrosis" still counts as containing
2
+ // "selinexor myelofibrosis" because word order and connecting words don't matter.
3
+ export function containsKeywordTokens(haystack: string, needle?: string): boolean {
4
+ if (!needle) return true;
5
+ const tokens = needle.toLowerCase().split(/\s+/).filter((t) => t.length > 1);
6
+ if (tokens.length === 0) return true;
7
+ const lower = haystack.toLowerCase();
8
+ return tokens.every((t) => lower.includes(t));
9
+ }
10
+
11
+ // True when the primary keyword sits near the start of the slug (within the first
12
+ // three slug words). Editors get the most SEO/CTR value when the keyword leads.
13
+ export function keywordLeadsSlug(slug: string, primaryKeyword?: string): boolean {
14
+ if (!primaryKeyword) return true;
15
+ const slugWords = slug.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim().split(/\s+/).filter(Boolean);
16
+ const tokens = primaryKeyword.toLowerCase().split(/\s+/).filter((t) => t.length > 1);
17
+ if (tokens.length === 0 || slugWords.length === 0) return true;
18
+ let earliest = Infinity;
19
+ for (const t of tokens) {
20
+ const idx = slugWords.findIndex((w) => w.includes(t));
21
+ if (idx >= 0) earliest = Math.min(earliest, idx);
22
+ }
23
+ return earliest <= 2;
24
+ }
25
+
26
+ // MJH publications that use the /view/{slug} article route (verified via CDN probe).
27
+ // We only construct live URLs for known publications to avoid linking to 404s on
28
+ // brands that use a different route pattern.
29
+ export const KNOWN_MJH_PUBLICATIONS = new Set([
30
+ "onclive",
31
+ "hcplive",
32
+ "ajmc",
33
+ "pharmacytimes",
34
+ "cancernetwork",
35
+ ]);