sanity-plugin-seo-copilot 0.2.5 → 0.2.10
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.
- package/dist/index.js +1310 -625
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1332 -647
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -2
- package/src/components/ChatPanel.tsx +5 -3
- package/src/components/FeedbackButton.tsx +6 -1
- package/src/components/KeywordPanel.tsx +179 -6
- package/src/components/OnboardingPopup.tsx +108 -0
- package/src/components/SEOCopilotPanel.tsx +160 -9
- package/src/components/ShimmerLoader.tsx +4 -0
- package/src/components/TabBar.tsx +3 -0
- package/src/components/styles.ts +8 -0
- package/src/components/tabs/AEOTab.tsx +4 -2
- package/src/components/tabs/LinkingTab.tsx +85 -8
- package/src/components/tabs/MetaTab.tsx +32 -119
- package/src/components/tabs/SummaryTab.tsx +19 -4
- package/src/components/tabs/TechnicalTab.tsx +118 -8
- package/src/hooks/useDocumentContent.ts +48 -47
- package/src/hooks/useKeywordAnalysis.ts +81 -48
- package/src/hooks/useSEOChat.ts +49 -9
- package/src/inspector.ts +0 -1
- package/src/lib/api.ts +30 -10
- package/src/lib/documentExtract.ts +268 -257
- package/src/lib/scoring.ts +42 -15
- package/src/lib/summary.ts +121 -41
- package/src/lib/utils.ts +35 -0
|
@@ -1,257 +1,268 @@
|
|
|
1
|
-
// Pure document-parsing helpers, split out of useDocumentContent so they can be
|
|
2
|
-
// unit-tested without a React / Sanity Studio runtime. No imports from "react"
|
|
3
|
-
// or "sanity" here - everything operates on plain Sanity document values.
|
|
4
|
-
|
|
5
|
-
import type { DocumentFields, HeadingItem } from "./types";
|
|
6
|
-
|
|
7
|
-
interface PortableTextSpan {
|
|
8
|
-
_type: string;
|
|
9
|
-
text?: string;
|
|
10
|
-
marks?: string[];
|
|
11
|
-
children?: PortableTextSpan[];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface PortableTextMarkDef {
|
|
15
|
-
_key: string;
|
|
16
|
-
_type: string;
|
|
17
|
-
href?: string;
|
|
18
|
-
url?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface PortableTextBlock {
|
|
22
|
-
_type: string;
|
|
23
|
-
style?: string;
|
|
24
|
-
children?: PortableTextSpan[];
|
|
25
|
-
markDefs?: PortableTextMarkDef[];
|
|
26
|
-
text?: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function extractText(blocks: unknown): string {
|
|
30
|
-
if (!blocks || !Array.isArray(blocks)) return "";
|
|
31
|
-
const parts: string[] = [];
|
|
32
|
-
for (const block of blocks as PortableTextBlock[]) {
|
|
33
|
-
if (block._type === "block" && block.children) {
|
|
34
|
-
const blockText = block.children.map((child) => child.text || "").join("");
|
|
35
|
-
if (blockText) parts.push(blockText);
|
|
36
|
-
} else if (typeof block === "string") {
|
|
37
|
-
parts.push(block);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return parts.join("\n");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function extractHeadings(blocks: unknown): string[] {
|
|
44
|
-
if (!blocks || !Array.isArray(blocks)) return [];
|
|
45
|
-
const headings: string[] = [];
|
|
46
|
-
for (const block of blocks as PortableTextBlock[]) {
|
|
47
|
-
if (block._type === "block" && block.style && /^h[1-6]$/.test(block.style) && block.children) {
|
|
48
|
-
const text = block.children.map((child) => child.text || "").join("");
|
|
49
|
-
if (text) headings.push(text);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return headings;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Headings with their numeric level (1-6), so the Technical tab can audit hierarchy.
|
|
56
|
-
export function extractHeadingsDetailed(blocks: unknown): HeadingItem[] {
|
|
57
|
-
if (!blocks || !Array.isArray(blocks)) return [];
|
|
58
|
-
const out: HeadingItem[] = [];
|
|
59
|
-
for (const block of blocks as PortableTextBlock[]) {
|
|
60
|
-
if (block._type === "block" && block.style && /^h[1-6]$/.test(block.style) && block.children) {
|
|
61
|
-
const text = block.children.map((child) => child.text || "").join("").trim();
|
|
62
|
-
const level = Number(block.style.slice(1));
|
|
63
|
-
// Include empty headings too - the Technical tab flags them.
|
|
64
|
-
out.push({ text, level });
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return out;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Every URL the editor has linked from this article body. Sanity stores them
|
|
71
|
-
// in Portable Text markDefs (with _type: "link" and href/url). We also catch
|
|
72
|
-
// inline plain-text URLs as a fallback.
|
|
73
|
-
export function extractBodyLinks(blocks: unknown): string[] {
|
|
74
|
-
const urls = new Set<string>();
|
|
75
|
-
if (Array.isArray(blocks)) {
|
|
76
|
-
for (const block of blocks as PortableTextBlock[]) {
|
|
77
|
-
if (block._type === "block" && Array.isArray(block.markDefs)) {
|
|
78
|
-
for (const md of block.markDefs) {
|
|
79
|
-
const href = md.href || md.url;
|
|
80
|
-
if (typeof href === "string" && /^https?:\/\//i.test(href)) {
|
|
81
|
-
urls.add(href.trim());
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
// Inline URLs that aren't in a link annotation - rare but possible.
|
|
88
|
-
if (Array.isArray(blocks)) {
|
|
89
|
-
for (const block of blocks as PortableTextBlock[]) {
|
|
90
|
-
if (block._type === "block" && Array.isArray(block.children)) {
|
|
91
|
-
const blockText = block.children.map((c) => c.text || "").join(" ");
|
|
92
|
-
const matches = blockText.match(/https?:\/\/[^\s"'<>)\]]+/g);
|
|
93
|
-
if (matches) matches.forEach((u) => urls.add(u.replace(/[.,;:!?]+$/, "")));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return Array.from(urls);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Slug detection across the common schema shapes. Looks for, in order:
|
|
101
|
-
// 1) A Sanity `slug` type object on any of several field names. MJH stores the
|
|
102
|
-
// slug at `url.current` (a Sanity slug type named "url"); the standard
|
|
103
|
-
// convention is `slug.current`. Check both, plus a few common variants.
|
|
104
|
-
// 2) Plain-string fields under common slug field names (fallback for schemas
|
|
105
|
-
// that store the slug as a flat string).
|
|
106
|
-
// We only accept a string if it looks slug-shaped (lowercase/hyphens, no spaces,
|
|
107
|
-
// no protocol) so we don't mistake a fully-qualified URL for a slug.
|
|
108
|
-
export function detectSlug(val: Record<string, unknown> | undefined): string | undefined {
|
|
109
|
-
if (!val) return undefined;
|
|
110
|
-
// Sanity slug-type objects: { _type: "slug", current: "..." }
|
|
111
|
-
const slugTypeFields = ["slug", "url", "urlSlug", "permalink", "pathname"];
|
|
112
|
-
for (const f of slugTypeFields) {
|
|
113
|
-
const obj = val[f] as { current?: unknown } | undefined;
|
|
114
|
-
if (obj && typeof obj === "object" && typeof obj.current === "string" && obj.current.trim()) {
|
|
115
|
-
return obj.current.trim();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
// Plain-string fallbacks
|
|
119
|
-
const stringFields = ["urlSlug", "url", "URL", "path", "permalink", "pathname"];
|
|
120
|
-
const slugShape = /^[a-z0-9._~-]+(?:\/[a-z0-9._~-]+)*\/?$/i;
|
|
121
|
-
for (const f of stringFields) {
|
|
122
|
-
const v = val[f];
|
|
123
|
-
if (typeof v === "string") {
|
|
124
|
-
const trimmed = v.trim();
|
|
125
|
-
if (trimmed && !/^https?:\/\//i.test(trimmed) && slugShape.test(trimmed)) {
|
|
126
|
-
return trimmed;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return undefined;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Author detection across a broad set of schema shapes. Tries every reasonable
|
|
134
|
-
// field name, handles inline strings, inline objects, and Sanity references
|
|
135
|
-
// (which we can't resolve here, so we mark them so the audit doesn't shout
|
|
136
|
-
// "Missing").
|
|
137
|
-
export function detectAuthor(val: Record<string, unknown> | undefined): {
|
|
138
|
-
authorName?: string;
|
|
139
|
-
authorCredentials?: string;
|
|
140
|
-
authorBioUrl?: string;
|
|
141
|
-
authorIsReference: boolean;
|
|
142
|
-
} {
|
|
143
|
-
if (!val) return { authorIsReference: false };
|
|
144
|
-
const candidateFields = [
|
|
145
|
-
"author", "authors", "byline", "articleAuthor", "primaryAuthor",
|
|
146
|
-
"writer", "writers", "contributor", "contributors",
|
|
147
|
-
"authorMapping", // MJH schema - array of author references
|
|
148
|
-
"Author", "Authors", // PascalCase variants
|
|
149
|
-
];
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
//
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
1
|
+
// Pure document-parsing helpers, split out of useDocumentContent so they can be
|
|
2
|
+
// unit-tested without a React / Sanity Studio runtime. No imports from "react"
|
|
3
|
+
// or "sanity" here - everything operates on plain Sanity document values.
|
|
4
|
+
|
|
5
|
+
import type { DocumentFields, HeadingItem } from "./types";
|
|
6
|
+
|
|
7
|
+
interface PortableTextSpan {
|
|
8
|
+
_type: string;
|
|
9
|
+
text?: string;
|
|
10
|
+
marks?: string[];
|
|
11
|
+
children?: PortableTextSpan[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface PortableTextMarkDef {
|
|
15
|
+
_key: string;
|
|
16
|
+
_type: string;
|
|
17
|
+
href?: string;
|
|
18
|
+
url?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface PortableTextBlock {
|
|
22
|
+
_type: string;
|
|
23
|
+
style?: string;
|
|
24
|
+
children?: PortableTextSpan[];
|
|
25
|
+
markDefs?: PortableTextMarkDef[];
|
|
26
|
+
text?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function extractText(blocks: unknown): string {
|
|
30
|
+
if (!blocks || !Array.isArray(blocks)) return "";
|
|
31
|
+
const parts: string[] = [];
|
|
32
|
+
for (const block of blocks as PortableTextBlock[]) {
|
|
33
|
+
if (block._type === "block" && block.children) {
|
|
34
|
+
const blockText = block.children.map((child) => child.text || "").join("");
|
|
35
|
+
if (blockText) parts.push(blockText);
|
|
36
|
+
} else if (typeof block === "string") {
|
|
37
|
+
parts.push(block);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return parts.join("\n");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function extractHeadings(blocks: unknown): string[] {
|
|
44
|
+
if (!blocks || !Array.isArray(blocks)) return [];
|
|
45
|
+
const headings: string[] = [];
|
|
46
|
+
for (const block of blocks as PortableTextBlock[]) {
|
|
47
|
+
if (block._type === "block" && block.style && /^h[1-6]$/.test(block.style) && block.children) {
|
|
48
|
+
const text = block.children.map((child) => child.text || "").join("");
|
|
49
|
+
if (text) headings.push(text);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return headings;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Headings with their numeric level (1-6), so the Technical tab can audit hierarchy.
|
|
56
|
+
export function extractHeadingsDetailed(blocks: unknown): HeadingItem[] {
|
|
57
|
+
if (!blocks || !Array.isArray(blocks)) return [];
|
|
58
|
+
const out: HeadingItem[] = [];
|
|
59
|
+
for (const block of blocks as PortableTextBlock[]) {
|
|
60
|
+
if (block._type === "block" && block.style && /^h[1-6]$/.test(block.style) && block.children) {
|
|
61
|
+
const text = block.children.map((child) => child.text || "").join("").trim();
|
|
62
|
+
const level = Number(block.style.slice(1));
|
|
63
|
+
// Include empty headings too - the Technical tab flags them.
|
|
64
|
+
out.push({ text, level });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Every URL the editor has linked from this article body. Sanity stores them
|
|
71
|
+
// in Portable Text markDefs (with _type: "link" and href/url). We also catch
|
|
72
|
+
// inline plain-text URLs as a fallback.
|
|
73
|
+
export function extractBodyLinks(blocks: unknown): string[] {
|
|
74
|
+
const urls = new Set<string>();
|
|
75
|
+
if (Array.isArray(blocks)) {
|
|
76
|
+
for (const block of blocks as PortableTextBlock[]) {
|
|
77
|
+
if (block._type === "block" && Array.isArray(block.markDefs)) {
|
|
78
|
+
for (const md of block.markDefs) {
|
|
79
|
+
const href = md.href || md.url;
|
|
80
|
+
if (typeof href === "string" && /^https?:\/\//i.test(href)) {
|
|
81
|
+
urls.add(href.trim());
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Inline URLs that aren't in a link annotation - rare but possible.
|
|
88
|
+
if (Array.isArray(blocks)) {
|
|
89
|
+
for (const block of blocks as PortableTextBlock[]) {
|
|
90
|
+
if (block._type === "block" && Array.isArray(block.children)) {
|
|
91
|
+
const blockText = block.children.map((c) => c.text || "").join(" ");
|
|
92
|
+
const matches = blockText.match(/https?:\/\/[^\s"'<>)\]]+/g);
|
|
93
|
+
if (matches) matches.forEach((u) => urls.add(u.replace(/[.,;:!?]+$/, "")));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return Array.from(urls);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Slug detection across the common schema shapes. Looks for, in order:
|
|
101
|
+
// 1) A Sanity `slug` type object on any of several field names. MJH stores the
|
|
102
|
+
// slug at `url.current` (a Sanity slug type named "url"); the standard
|
|
103
|
+
// convention is `slug.current`. Check both, plus a few common variants.
|
|
104
|
+
// 2) Plain-string fields under common slug field names (fallback for schemas
|
|
105
|
+
// that store the slug as a flat string).
|
|
106
|
+
// We only accept a string if it looks slug-shaped (lowercase/hyphens, no spaces,
|
|
107
|
+
// no protocol) so we don't mistake a fully-qualified URL for a slug.
|
|
108
|
+
export function detectSlug(val: Record<string, unknown> | undefined): string | undefined {
|
|
109
|
+
if (!val) return undefined;
|
|
110
|
+
// Sanity slug-type objects: { _type: "slug", current: "..." }
|
|
111
|
+
const slugTypeFields = ["slug", "url", "urlSlug", "permalink", "pathname"];
|
|
112
|
+
for (const f of slugTypeFields) {
|
|
113
|
+
const obj = val[f] as { current?: unknown } | undefined;
|
|
114
|
+
if (obj && typeof obj === "object" && typeof obj.current === "string" && obj.current.trim()) {
|
|
115
|
+
return obj.current.trim();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Plain-string fallbacks
|
|
119
|
+
const stringFields = ["urlSlug", "url", "URL", "path", "permalink", "pathname"];
|
|
120
|
+
const slugShape = /^[a-z0-9._~-]+(?:\/[a-z0-9._~-]+)*\/?$/i;
|
|
121
|
+
for (const f of stringFields) {
|
|
122
|
+
const v = val[f];
|
|
123
|
+
if (typeof v === "string") {
|
|
124
|
+
const trimmed = v.trim();
|
|
125
|
+
if (trimmed && !/^https?:\/\//i.test(trimmed) && slugShape.test(trimmed)) {
|
|
126
|
+
return trimmed;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Author detection across a broad set of schema shapes. Tries every reasonable
|
|
134
|
+
// field name, handles inline strings, inline objects, and Sanity references
|
|
135
|
+
// (which we can't resolve here, so we mark them so the audit doesn't shout
|
|
136
|
+
// "Missing").
|
|
137
|
+
export function detectAuthor(val: Record<string, unknown> | undefined): {
|
|
138
|
+
authorName?: string;
|
|
139
|
+
authorCredentials?: string;
|
|
140
|
+
authorBioUrl?: string;
|
|
141
|
+
authorIsReference: boolean;
|
|
142
|
+
} {
|
|
143
|
+
if (!val) return { authorIsReference: false };
|
|
144
|
+
const candidateFields = [
|
|
145
|
+
"author", "authors", "byline", "articleAuthor", "primaryAuthor",
|
|
146
|
+
"writer", "writers", "contributor", "contributors",
|
|
147
|
+
"authorMapping", // MJH schema - array of author references
|
|
148
|
+
"Author", "Authors", // PascalCase variants
|
|
149
|
+
];
|
|
150
|
+
// Also scan any field whose NAME mentions author/byline (covers MJH's exact
|
|
151
|
+
// field name whatever it turns out to be), but never the fact-check / reviewer
|
|
152
|
+
// fields - those aren't the visible byline. This is why a referenced author was
|
|
153
|
+
// showing as "Missing": the real field name wasn't in the fixed list above.
|
|
154
|
+
const dynamic = Object.keys(val).filter(
|
|
155
|
+
// Author/byline-ish names, but never fact-check/reviewer fields, nor image/url/
|
|
156
|
+
// id sidecars (e.g. authorPhoto, authorImageUrl, authorId) that could otherwise
|
|
157
|
+
// be mistaken for a reference-typed author.
|
|
158
|
+
(k) => /author|byline/i.test(k) && !/fact|check|review|photo|image|avatar|picture|url|link|count|id$/i.test(k)
|
|
159
|
+
);
|
|
160
|
+
const fields = Array.from(new Set([...candidateFields, ...dynamic]));
|
|
161
|
+
for (const f of fields) {
|
|
162
|
+
const raw = val[f];
|
|
163
|
+
if (!raw) continue;
|
|
164
|
+
const first = Array.isArray(raw) ? raw[0] : raw;
|
|
165
|
+
if (!first) continue;
|
|
166
|
+
if (typeof first === "string") {
|
|
167
|
+
const trimmed = first.trim();
|
|
168
|
+
if (trimmed) return { authorName: trimmed, authorIsReference: false };
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (typeof first === "object" && first !== null) {
|
|
172
|
+
const o = first as Record<string, unknown>;
|
|
173
|
+
const name = ([o.name, o.fullName, o.displayName] as unknown[]).find((v): v is string => typeof v === 'string' && v.trim().length > 0);
|
|
174
|
+
const creds = ([o.credentials, o.title, o.suffix] as unknown[]).find((v): v is string => typeof v === 'string' && v.trim().length > 0);
|
|
175
|
+
const slug = (o.slug as { current?: string } | undefined)?.current;
|
|
176
|
+
const bio = (o.bioUrl as string | undefined) || (slug ? `/author/${slug}` : undefined);
|
|
177
|
+
if (name) {
|
|
178
|
+
return { authorName: name, authorCredentials: creds, authorBioUrl: bio, authorIsReference: false };
|
|
179
|
+
}
|
|
180
|
+
// Reference shape - has _ref, or is typed as a reference.
|
|
181
|
+
if (typeof o._ref === "string" || o._type === "reference") {
|
|
182
|
+
return { authorIsReference: true };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return { authorIsReference: false };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Extracts image metadata from top-level image fields AND inline body
|
|
190
|
+
// figure/image blocks (where MJH stores article images):
|
|
191
|
+
// - names: alt text + asset filename (for the alt + filename audits)
|
|
192
|
+
// - titles: the `title` field on the image object or asset (the hover tooltip)
|
|
193
|
+
// - count: number of real images found (so the title audit can say "X of N")
|
|
194
|
+
export function extractImageMeta(value: Record<string, unknown>): { names: string[]; titles: string[]; count: number } {
|
|
195
|
+
const names: string[] = [];
|
|
196
|
+
const titles: string[] = [];
|
|
197
|
+
let count = 0;
|
|
198
|
+
const imageFields = ["mainImage", "image", "heroImage", "thumbnail", "featuredImage"];
|
|
199
|
+
for (const field of imageFields) {
|
|
200
|
+
const img = value[field] as Record<string, unknown> | undefined;
|
|
201
|
+
if (!img || typeof img !== "object") continue;
|
|
202
|
+
const asset = img.asset && typeof img.asset === "object" ? (img.asset as Record<string, unknown>) : undefined;
|
|
203
|
+
// Only count entries that are actually images (have an asset or an image _type).
|
|
204
|
+
if (!asset && img._type !== "image") continue;
|
|
205
|
+
count++;
|
|
206
|
+
if (typeof img.alt === "string" && img.alt) names.push(img.alt);
|
|
207
|
+
if (asset && typeof asset.originalFilename === "string" && asset.originalFilename) names.push(asset.originalFilename);
|
|
208
|
+
// The hover-tooltip title can live on the image object or on the asset.
|
|
209
|
+
if (typeof img.title === "string" && img.title) titles.push(img.title);
|
|
210
|
+
else if (asset && typeof asset.title === "string" && asset.title) titles.push(asset.title);
|
|
211
|
+
}
|
|
212
|
+
// Inline body images: MJH (and most editorial schemas) embed article images as
|
|
213
|
+
// `figure`/`image` blocks INSIDE the Portable Text body, not as a top-level
|
|
214
|
+
// field. Without this walk the entire image audit (count, alt, title) is blind
|
|
215
|
+
// to the images that are actually in the article.
|
|
216
|
+
const body = value.body;
|
|
217
|
+
if (Array.isArray(body)) {
|
|
218
|
+
for (const blk of body) {
|
|
219
|
+
if (!blk || typeof blk !== "object") continue;
|
|
220
|
+
const b = blk as Record<string, unknown>;
|
|
221
|
+
if (b._type !== "figure" && b._type !== "image") continue;
|
|
222
|
+
const asset = b.asset && typeof b.asset === "object" ? (b.asset as Record<string, unknown>) : undefined;
|
|
223
|
+
if (!asset && b._type !== "image") continue;
|
|
224
|
+
count++;
|
|
225
|
+
if (typeof b.alt === "string" && b.alt) names.push(b.alt);
|
|
226
|
+
// Asset is usually an unresolved reference here, so originalFilename is
|
|
227
|
+
// rarely present - push it only when the studio expanded the asset.
|
|
228
|
+
if (asset && typeof asset.originalFilename === "string" && asset.originalFilename) names.push(asset.originalFilename);
|
|
229
|
+
if (typeof b.title === "string" && b.title) titles.push(b.title);
|
|
230
|
+
else if (asset && typeof asset.title === "string" && asset.title) titles.push(asset.title);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return { names, titles, count };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Assembles the full DocumentFields object from a raw Sanity document value.
|
|
237
|
+
// Pure + side-effect-free so it can be exercised against real fetched documents.
|
|
238
|
+
export function buildDocumentFields(value: Record<string, unknown> | undefined): DocumentFields {
|
|
239
|
+
const val = value;
|
|
240
|
+
const title = typeof val?.title === 'string' ? val.title : undefined;
|
|
241
|
+
const body = val?.body;
|
|
242
|
+
const { authorName, authorCredentials, authorBioUrl, authorIsReference } = detectAuthor(val);
|
|
243
|
+
const imageMeta = val ? extractImageMeta(val) : { names: [], titles: [], count: 0 };
|
|
244
|
+
return {
|
|
245
|
+
title: title || undefined,
|
|
246
|
+
// MJH uses `summary`; standard Sanity SEO plugins use `metaDescription`
|
|
247
|
+
metaDescription: ([val?.metaDescription, val?.summary, val?.description, val?.seoDescription] as unknown[]).find((v): v is string => typeof v === 'string' && v.trim().length > 0),
|
|
248
|
+
slug: detectSlug(val),
|
|
249
|
+
imageNames: imageMeta.names,
|
|
250
|
+
imageTitles: imageMeta.titles,
|
|
251
|
+
imageCount: imageMeta.count,
|
|
252
|
+
headings: extractHeadings(body),
|
|
253
|
+
headingsDetailed: extractHeadingsDetailed(body),
|
|
254
|
+
bodyLinks: extractBodyLinks(body),
|
|
255
|
+
contentCategory: typeof val?.contentCategory === 'string' ? val.contentCategory : typeof val?.category === 'string' ? val.category : undefined,
|
|
256
|
+
contentPlacement: Array.isArray(val?.contentPlacement) ? (val.contentPlacement as string[]) : undefined,
|
|
257
|
+
authorName,
|
|
258
|
+
authorCredentials,
|
|
259
|
+
authorBioUrl,
|
|
260
|
+
authorIsReference,
|
|
261
|
+
// `published` is MJH's date field; `publishedAt`/`publishDate` cover other schemas
|
|
262
|
+
publishedAt: ([val?.publishedAt, val?.publishDate, val?.published, val?._createdAt] as unknown[]).find((v): v is string => typeof v === 'string' && v.trim().length > 0),
|
|
263
|
+
// `updatedOn` is MJH's "Updated On" field. `updatedAt` is the standard Sanity convention.
|
|
264
|
+
// `_updatedAt` is the Sanity-managed timestamp as a final fallback.
|
|
265
|
+
updatedAt: ([val?.updatedOn, val?.updatedAt, val?._updatedAt] as unknown[]).find((v): v is string => typeof v === 'string' && v.trim().length > 0),
|
|
266
|
+
lastReviewedAt: ([val?.lastReviewedAt, val?.lastReviewed, val?.medicallyReviewedAt] as unknown[]).find((v): v is string => typeof v === 'string' && v.trim().length > 0),
|
|
267
|
+
};
|
|
268
|
+
}
|