dotcms-custom-mcp 0.0.0-stage → 1.3.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.
- package/README.md +257 -2
- package/dotcms.js +322 -0
- package/hooks/confirm-write.mjs +41 -0
- package/index.js +954 -0
- package/package.json +39 -4
- package/plans.js +111 -0
- package/relations.js +171 -0
- package/seo.js +365 -0
package/seo.js
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// SEO analysis of a dotCMS page or a content item. Pure functions: the server fetches the data,
|
|
2
|
+
// these decide what is wrong with it.
|
|
3
|
+
//
|
|
4
|
+
// Two sources, depending on what is analysed:
|
|
5
|
+
// - a regular (drag-and-drop) page: its SEO values live on the page itself
|
|
6
|
+
// - a detail page (URL-mapped content) or a content item: the values come from the content's
|
|
7
|
+
// fields, and its content type says which SEO fields exist at all
|
|
8
|
+
// When the public site URL is known, the rendered HTML is checked too (title tag, meta tags,
|
|
9
|
+
// headings, images), since that is what search engines actually see.
|
|
10
|
+
|
|
11
|
+
export const LIMITS = { titleMin: 30, titleMax: 60, descMin: 70, descMax: 160, urlMax: 75 };
|
|
12
|
+
|
|
13
|
+
const TITLE_FIELDS = ["seoTitle", "metaTitle", "title", "headline", "name"];
|
|
14
|
+
const DESC_FIELDS = ["seoDescription", "metaDescription", "description", "teaser", "summary", "abstract", "excerpt"];
|
|
15
|
+
const IMAGE_FIELDS = ["ogImage", "image", "heroImage", "mainImage", "featuredImage", "teaserImage", "thumbnail", "photo"];
|
|
16
|
+
|
|
17
|
+
const finding = (severity, id, message, where) => ({ severity, id, message, where });
|
|
18
|
+
const SEVERITY_ORDER = { error: 0, warning: 1, info: 2 };
|
|
19
|
+
|
|
20
|
+
const decode = (s) =>
|
|
21
|
+
s
|
|
22
|
+
.replace(/ /g, " ")
|
|
23
|
+
.replace(/"/g, '"')
|
|
24
|
+
.replace(/�?39;|'/g, "'")
|
|
25
|
+
.replace(/</g, "<")
|
|
26
|
+
.replace(/>/g, ">")
|
|
27
|
+
.replace(/&/g, "&");
|
|
28
|
+
|
|
29
|
+
/** Plain text of a field value (WYSIWYG fields hold HTML). */
|
|
30
|
+
const text = (v) => (typeof v === "string" ? decode(v.replace(/<[^>]*>/g, " ")).replace(/\s+/g, " ").trim() : "");
|
|
31
|
+
|
|
32
|
+
/** A site path from a path or a full URL; only the path is kept. Throws when it is not a plain path. */
|
|
33
|
+
export function normalizePagePath(input) {
|
|
34
|
+
const raw = String(input ?? "").trim();
|
|
35
|
+
if (!raw) throw new Error("Pass the page path, e.g. '/about-us'.");
|
|
36
|
+
// The URL parser would quietly resolve "/a/../b" to "/b": refuse dot segments in what was typed.
|
|
37
|
+
const typed = raw.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]*/i, "").split(/[?#]/)[0];
|
|
38
|
+
const dotSegment = typed.split("/").some((seg) => {
|
|
39
|
+
try {
|
|
40
|
+
const decoded = decodeURIComponent(seg);
|
|
41
|
+
return decoded === "." || decoded === "..";
|
|
42
|
+
} catch {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
let pathname;
|
|
47
|
+
try {
|
|
48
|
+
pathname = new URL(raw, "http://placeholder.invalid").pathname;
|
|
49
|
+
} catch {
|
|
50
|
+
throw new Error(`Invalid path '${raw}'.`);
|
|
51
|
+
}
|
|
52
|
+
if (dotSegment || !/^\/[A-Za-z0-9\-._~/%@+]*$/.test(pathname) || /%(?![0-9A-Fa-f]{2})/.test(pathname)) {
|
|
53
|
+
throw new Error(`Invalid path '${raw}': use a site path such as '/blog/my-post'.`);
|
|
54
|
+
}
|
|
55
|
+
return pathname.length > 1 ? pathname.replace(/\/+$/, "") : pathname;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** First non-empty text value among the named fields (case-insensitive). */
|
|
59
|
+
export function pickText(obj, names) {
|
|
60
|
+
if (!obj || typeof obj !== "object") return null;
|
|
61
|
+
const byLower = new Map(Object.keys(obj).map((k) => [k.toLowerCase(), k]));
|
|
62
|
+
for (const name of names) {
|
|
63
|
+
const key = byLower.get(name.toLowerCase());
|
|
64
|
+
const value = key ? text(obj[key]) : "";
|
|
65
|
+
if (value) return { field: key, value };
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const IMAGE_NAME = /image|photo|picture|thumbnail|banner|hero/i;
|
|
71
|
+
const hasValue = (v) => (typeof v === "string" ? v.trim() !== "" && v !== "TITLE_IMAGE_NOT_FOUND" : !!v && typeof v === "object");
|
|
72
|
+
|
|
73
|
+
/** First image-like field that has a value: a well-known name first, then any field named like an image. */
|
|
74
|
+
function pickPresent(obj, names) {
|
|
75
|
+
if (!obj || typeof obj !== "object") return null;
|
|
76
|
+
const keys = Object.keys(obj);
|
|
77
|
+
const byLower = new Map(keys.map((k) => [k.toLowerCase(), k]));
|
|
78
|
+
for (const name of names) {
|
|
79
|
+
const key = byLower.get(name.toLowerCase());
|
|
80
|
+
if (key && hasValue(obj[key])) return key;
|
|
81
|
+
}
|
|
82
|
+
return keys.find((k) => IMAGE_NAME.test(k) && !/^(has|is)[A-Z]/.test(k) && hasValue(obj[k])) || null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const NAME_LIKE = /title|name|headline/i;
|
|
86
|
+
const DESC_LIKE = /desc|summary|teaser|abstract|excerpt|overview/i;
|
|
87
|
+
|
|
88
|
+
/** The variable of a title or name field of a content type (dotCMS itself always generates a "title" value). */
|
|
89
|
+
function typeTitleField(ct) {
|
|
90
|
+
return typeField(ct, TITLE_FIELDS) || (ct?.fields || []).find((f) => f.variable && NAME_LIKE.test(f.variable))?.variable || null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The variable of a description-like field of a content type. */
|
|
94
|
+
function typeDescriptionField(ct) {
|
|
95
|
+
return typeField(ct, DESC_FIELDS) || (ct?.fields || []).find((f) => f.variable && DESC_LIKE.test(f.variable))?.variable || null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The variable of an image field of a content type: a well-known name, an image or binary field, or one named like an image. */
|
|
99
|
+
function typeImageField(ct) {
|
|
100
|
+
return (
|
|
101
|
+
typeField(ct, IMAGE_FIELDS) ||
|
|
102
|
+
(ct?.fields || []).find((f) => f.variable && (/(Image|Binary)Field$/.test(f.clazz || "") || IMAGE_NAME.test(f.variable)))?.variable ||
|
|
103
|
+
null
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The variable of the first field of a content type that has one of these names. */
|
|
108
|
+
export function typeField(ct, names) {
|
|
109
|
+
const byLower = new Map((ct?.fields || []).filter((f) => f.variable).map((f) => [f.variable.toLowerCase(), f.variable]));
|
|
110
|
+
for (const name of names) {
|
|
111
|
+
const v = byLower.get(name.toLowerCase());
|
|
112
|
+
if (v) return v;
|
|
113
|
+
}
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function lengthFindings(out, { id, label, value, min, max, where, severity = "warning" }) {
|
|
118
|
+
const n = [...value].length;
|
|
119
|
+
if (n < min) out.push(finding(severity, `${id}-short`, `${label} is ${n} characters; aim for ${min}–${max}.`, where));
|
|
120
|
+
else if (n > max) out.push(finding(severity, `${id}-long`, `${label} is ${n} characters; search results cut it off after about ${max}.`, where));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** URL hygiene: readable, lowercase, hyphenated, not too long or deep. */
|
|
124
|
+
export function checkUrl(urlPath) {
|
|
125
|
+
const out = [];
|
|
126
|
+
const where = "url";
|
|
127
|
+
if (urlPath.length > LIMITS.urlMax) out.push(finding("warning", "url-long", `The URL is ${urlPath.length} characters; keep it under ${LIMITS.urlMax}.`, where));
|
|
128
|
+
if (/[A-Z]/.test(urlPath)) out.push(finding("warning", "url-uppercase", "The URL contains uppercase letters; search engines treat /Page and /page as different URLs.", where));
|
|
129
|
+
if (urlPath.includes("_")) out.push(finding("warning", "url-underscore", "The URL uses underscores; hyphens are read as word separators, underscores are not.", where));
|
|
130
|
+
if (/%20|\s/.test(urlPath)) out.push(finding("warning", "url-space", "The URL contains spaces.", where));
|
|
131
|
+
if (/\.(html?|php|aspx?|jsp)$/i.test(urlPath)) out.push(finding("info", "url-extension", "The URL ends in a file extension; extension-less URLs are cleaner.", where));
|
|
132
|
+
if (urlPath.split("/").filter(Boolean).length > 4) out.push(finding("info", "url-deep", "The URL is more than 4 levels deep.", where));
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Checks of the values stored in dotCMS.
|
|
138
|
+
* kind: "page" | "detail-page" | "content"
|
|
139
|
+
* page / content: the page contentlet, or the (mapped) content item
|
|
140
|
+
* contentType: the content type entity of the content, when there is one
|
|
141
|
+
* htmlSeen: { title, description } — the rendered HTML has them, so field checks for them are skipped
|
|
142
|
+
*/
|
|
143
|
+
export function analyzeFields({ kind, page, content, contentType, htmlSeen = {} }) {
|
|
144
|
+
const findings = [];
|
|
145
|
+
const source = kind === "page" ? page : content;
|
|
146
|
+
const where = kind === "page" ? "page fields" : `${contentType?.variable || "content"} fields`;
|
|
147
|
+
|
|
148
|
+
const title = pickText(source, TITLE_FIELDS);
|
|
149
|
+
const desc = pickText(source, DESC_FIELDS);
|
|
150
|
+
const image = kind === "page" ? null : pickPresent(source, IMAGE_FIELDS);
|
|
151
|
+
|
|
152
|
+
if (!htmlSeen.title) {
|
|
153
|
+
if (!title) findings.push(finding("error", "title-missing", "No title value found.", where));
|
|
154
|
+
// A content title is usually only part of the page <title> (the template adds more), so it is a note, not a warning.
|
|
155
|
+
else lengthFindings(findings, { id: "title", label: `Title (${title.field})`, value: title.value, min: LIMITS.titleMin, max: LIMITS.titleMax, where, severity: kind === "page" ? "warning" : "info" });
|
|
156
|
+
}
|
|
157
|
+
if (!htmlSeen.description) {
|
|
158
|
+
if (!desc) {
|
|
159
|
+
const hint = kind === "page" ? " Add one to the page, or make sure the template sets <meta name=\"description\">." : "";
|
|
160
|
+
findings.push(finding("warning", "description-missing", `No description value found.${hint}`, where));
|
|
161
|
+
} else {
|
|
162
|
+
lengthFindings(findings, { id: "description", label: `Description (${desc.field})`, value: desc.value, min: LIMITS.descMin, max: LIMITS.descMax, where });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (title && desc && title.value.toLowerCase() === desc.value.toLowerCase()) {
|
|
166
|
+
findings.push(finding("warning", "title-equals-description", "The title and the description are identical.", where));
|
|
167
|
+
}
|
|
168
|
+
if (kind !== "page" && !image) {
|
|
169
|
+
findings.push(finding("info", "image-missing", "No image value found; shares of this page will have no preview picture.", where));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (kind !== "page" && contentType) {
|
|
173
|
+
const v = contentType.variable;
|
|
174
|
+
const typeWhere = `content type ${v}`;
|
|
175
|
+
if (!typeTitleField(contentType)) findings.push(finding("warning", "type-no-title", `Content type ${v} has no field that looks like a title or name, so pages can only use the title dotCMS generates.`, typeWhere));
|
|
176
|
+
if (!typeDescriptionField(contentType)) findings.push(finding("warning", "type-no-description", `Content type ${v} has no description field, so its pages cannot carry a unique meta description.`, typeWhere));
|
|
177
|
+
if (!typeImageField(contentType)) findings.push(finding("info", "type-no-image", `Content type ${v} has no image field for social previews.`, typeWhere));
|
|
178
|
+
const pattern = contentType.urlMapPattern;
|
|
179
|
+
if (!pattern) {
|
|
180
|
+
if (kind === "content") findings.push(finding("warning", "type-not-mapped", `Content type ${v} has no URL map pattern, so its content has no page of its own for search engines.`, typeWhere));
|
|
181
|
+
} else if (/\{(identifier|inode)\}/i.test(pattern)) {
|
|
182
|
+
findings.push(finding("warning", "pattern-id", `The URL map pattern ${pattern} builds URLs from an id; a descriptive field such as a url title reads better and ranks better.`, typeWhere));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
findings,
|
|
188
|
+
data: {
|
|
189
|
+
title: title?.value,
|
|
190
|
+
titleField: title?.field,
|
|
191
|
+
description: desc?.value,
|
|
192
|
+
descriptionField: desc?.field,
|
|
193
|
+
imageField: image || undefined,
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const attr = (tag, name) => {
|
|
199
|
+
const m = new RegExp(`\\s${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, "i").exec(tag);
|
|
200
|
+
return m ? (m[1] ?? m[2] ?? m[3] ?? "") : null;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/** Checks of the rendered page, the way a crawler reads it. */
|
|
204
|
+
export function analyzeHtml(html) {
|
|
205
|
+
const findings = [];
|
|
206
|
+
const where = "rendered HTML";
|
|
207
|
+
|
|
208
|
+
const titleMatch = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(html);
|
|
209
|
+
const title = titleMatch ? text(titleMatch[1]) : "";
|
|
210
|
+
const metas = [...html.matchAll(/<meta\b[^>]*>/gi)].map((m) => m[0]);
|
|
211
|
+
const meta = (key, by = "name") => {
|
|
212
|
+
for (const tag of metas) if ((attr(tag, by) || "").toLowerCase() === key) return attr(tag, "content") ?? "";
|
|
213
|
+
return null;
|
|
214
|
+
};
|
|
215
|
+
const description = text(meta("description") ?? "");
|
|
216
|
+
const robots = (meta("robots") ?? "").toLowerCase();
|
|
217
|
+
const canonicalTag = [...html.matchAll(/<link\b[^>]*>/gi)].map((m) => m[0]).find((t) => /\bcanonical\b/i.test(attr(t, "rel") || ""));
|
|
218
|
+
const canonical = canonicalTag ? attr(canonicalTag, "href") : null;
|
|
219
|
+
const h1s = [...html.matchAll(/<h1\b[^>]*>([\s\S]*?)<\/h1>/gi)].map((m) => text(m[1]));
|
|
220
|
+
const imgs = [...html.matchAll(/<img\b[^>]*>/gi)].map((m) => m[0]);
|
|
221
|
+
const noAlt = imgs.filter((t) => attr(t, "alt") === null).length;
|
|
222
|
+
const htmlTag = /<html\b[^>]*>/i.exec(html)?.[0] || "";
|
|
223
|
+
|
|
224
|
+
if (!title) findings.push(finding("error", "html-title-missing", titleMatch ? "The <title> tag is empty." : "The page has no <title> tag.", where));
|
|
225
|
+
else lengthFindings(findings, { id: "html-title", label: "The <title>", value: title, min: LIMITS.titleMin, max: LIMITS.titleMax, where });
|
|
226
|
+
|
|
227
|
+
if (!description) {
|
|
228
|
+
findings.push(
|
|
229
|
+
finding("warning", "html-description-missing", meta("description") === null ? 'The page has no <meta name="description">.' : "The meta description is empty.", where),
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
else lengthFindings(findings, { id: "html-description", label: "The meta description", value: description, min: LIMITS.descMin, max: LIMITS.descMax, where });
|
|
233
|
+
|
|
234
|
+
if (/\bnoindex\b/.test(robots)) findings.push(finding("error", "html-noindex", "The page tells search engines not to index it (robots noindex).", where));
|
|
235
|
+
if (/\bnofollow\b/.test(robots)) findings.push(finding("warning", "html-nofollow", "The page tells search engines not to follow its links (robots nofollow).", where));
|
|
236
|
+
if (!canonical) findings.push(finding("warning", "html-canonical-missing", "The page has no canonical link, so duplicate URLs may compete with each other.", where));
|
|
237
|
+
|
|
238
|
+
if (!h1s.length) findings.push(finding("error", "html-h1-missing", "The page has no <h1>.", where));
|
|
239
|
+
else if (h1s.length > 1) findings.push(finding("warning", "html-h1-multiple", `The page has ${h1s.length} <h1> headings; use one.`, where));
|
|
240
|
+
|
|
241
|
+
const og = ["og:title", "og:description", "og:image"].filter((k) => meta(k, "property") === null);
|
|
242
|
+
if (og.length) findings.push(finding("info", "html-og-missing", `Missing Open Graph tags: ${og.join(", ")}. Shared links will look plain.`, where));
|
|
243
|
+
|
|
244
|
+
if (noAlt) findings.push(finding("warning", "html-img-alt", `${noAlt} of ${imgs.length} images have no alt attribute.`, where));
|
|
245
|
+
if (!/\slang\s*=/.test(htmlTag)) findings.push(finding("warning", "html-lang-missing", "The <html> tag has no lang attribute.", where));
|
|
246
|
+
if (meta("viewport") === null) findings.push(finding("warning", "html-viewport-missing", "The page has no viewport meta tag, so it is not mobile-friendly.", where));
|
|
247
|
+
if (!/application\/ld\+json/i.test(html)) findings.push(finding("info", "html-structured-data", "No structured data (JSON-LD) found.", where));
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
findings,
|
|
251
|
+
data: {
|
|
252
|
+
title: title || undefined,
|
|
253
|
+
description: description || undefined,
|
|
254
|
+
canonical: canonical || undefined,
|
|
255
|
+
robots: robots || undefined,
|
|
256
|
+
h1: h1s.slice(0, 3),
|
|
257
|
+
images: imgs.length,
|
|
258
|
+
imagesWithoutAlt: noAlt,
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const clip = (s, n = 50) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
|
|
264
|
+
|
|
265
|
+
/** Values dotCMS holds that the rendered page does not show: the template is not outputting them. */
|
|
266
|
+
export function crossCheck(fieldData, htmlData) {
|
|
267
|
+
const out = [];
|
|
268
|
+
if (!htmlData) return out;
|
|
269
|
+
if (fieldData?.title && !htmlData.title) {
|
|
270
|
+
out.push(finding("warning", "template-title-not-rendered", `dotCMS has a title ("${clip(fieldData.title)}") but the rendered <title> is empty; check that the template outputs it.`, "template"));
|
|
271
|
+
}
|
|
272
|
+
if (fieldData?.description && !htmlData.description) {
|
|
273
|
+
out.push(finding("warning", "template-description-not-rendered", `dotCMS has a description ("${clip(fieldData.description)}") but the rendered meta description is empty; check that the template outputs it.`, "template"));
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function summarize(findings) {
|
|
279
|
+
const count = (s) => findings.filter((f) => f.severity === s).length;
|
|
280
|
+
return { errors: count("error"), warnings: count("warning"), notes: count("info") };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const ICON = { error: "✗", warning: "⚠", info: "ℹ" };
|
|
284
|
+
|
|
285
|
+
/** The report as plain text, most serious first. */
|
|
286
|
+
export function renderSeoReport({ heading, sources, findings }) {
|
|
287
|
+
const c = summarize(findings);
|
|
288
|
+
const lines = [heading, ...sources.map((s) => ` · ${s}`), "", `${c.errors} error(s) · ${c.warnings} warning(s) · ${c.notes} note(s)`, ""];
|
|
289
|
+
const sorted = [...findings].sort((a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity]);
|
|
290
|
+
for (const f of sorted) lines.push(`${ICON[f.severity]} ${f.message} [${f.where}]`);
|
|
291
|
+
if (!findings.length) lines.push("No issues found in the checks that ran.");
|
|
292
|
+
return lines.join("\n");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ---------------------------------------------------------------------------
|
|
296
|
+
// Auditing many pages
|
|
297
|
+
// ---------------------------------------------------------------------------
|
|
298
|
+
|
|
299
|
+
/** One row of an audit: a page and what is wrong with it. */
|
|
300
|
+
export function auditRow({ path, title, findings, htmlChecked }) {
|
|
301
|
+
const counts = summarize(findings);
|
|
302
|
+
const sorted = [...findings].sort((a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity]);
|
|
303
|
+
return {
|
|
304
|
+
path,
|
|
305
|
+
title: title || undefined,
|
|
306
|
+
...counts,
|
|
307
|
+
htmlChecked: !!htmlChecked,
|
|
308
|
+
issues: sorted.map(({ severity, id, message }) => ({ severity, id, message })),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Pages with the most serious problems first. */
|
|
313
|
+
export const sortRows = (rows) =>
|
|
314
|
+
[...rows].sort((a, b) => b.errors - a.errors || b.warnings - a.warnings || b.notes - a.notes || a.path.localeCompare(b.path));
|
|
315
|
+
|
|
316
|
+
/** How many pages each kind of problem touches, most serious and most common first. */
|
|
317
|
+
export function commonIssues(rows) {
|
|
318
|
+
const byId = new Map();
|
|
319
|
+
for (const row of rows) {
|
|
320
|
+
for (const issue of row.issues) {
|
|
321
|
+
const entry = byId.get(issue.id) || { id: issue.id, severity: issue.severity, pages: 0, example: issue.message };
|
|
322
|
+
entry.pages++;
|
|
323
|
+
byId.set(issue.id, entry);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return [...byId.values()].sort(
|
|
327
|
+
(a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity] || b.pages - a.pages || a.id.localeCompare(b.id),
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** The audit as plain text: one line per page, then the most common problems. */
|
|
332
|
+
export function renderAuditReport({ heading, sources, rows, total, nextPage, onlyIssues }) {
|
|
333
|
+
const withErrors = rows.filter((r) => r.errors).length;
|
|
334
|
+
const warningsOnly = rows.filter((r) => !r.errors && r.warnings).length;
|
|
335
|
+
const clean = rows.length - withErrors - warningsOnly;
|
|
336
|
+
const short = (s, n = 90) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
|
|
337
|
+
|
|
338
|
+
const lines = [
|
|
339
|
+
heading,
|
|
340
|
+
...sources.map((s) => ` · ${s}`),
|
|
341
|
+
"",
|
|
342
|
+
`${rows.length} page(s) checked: ${withErrors} with errors · ${warningsOnly} with warnings only · ${clean} without problems` +
|
|
343
|
+
(total > rows.length ? ` (the search matched ${total})` : ""),
|
|
344
|
+
"",
|
|
345
|
+
];
|
|
346
|
+
const listed = sortRows(onlyIssues ? rows.filter((r) => r.errors || r.warnings) : rows);
|
|
347
|
+
for (const r of listed) {
|
|
348
|
+
const mark = r.errors ? `✗${r.errors}${r.warnings ? ` ⚠${r.warnings}` : ""}` : r.warnings ? `⚠${r.warnings}` : "✓";
|
|
349
|
+
const top = r.issues
|
|
350
|
+
.filter((i) => i.severity !== "info")
|
|
351
|
+
.slice(0, 2)
|
|
352
|
+
.map((i) => short(i.message))
|
|
353
|
+
.join(" · ");
|
|
354
|
+
lines.push(`${mark.padEnd(7)} ${r.path}${top ? ` — ${top}` : ""}`);
|
|
355
|
+
}
|
|
356
|
+
if (!listed.length) lines.push(onlyIssues ? "No page has errors or warnings." : "No pages found.");
|
|
357
|
+
|
|
358
|
+
const common = commonIssues(rows).filter((i) => i.severity !== "info").slice(0, 8);
|
|
359
|
+
if (common.length) {
|
|
360
|
+
lines.push("", "Most common problems:");
|
|
361
|
+
for (const i of common) lines.push(` ${String(i.pages).padStart(3)} page(s) · ${i.id} (${i.severity})`);
|
|
362
|
+
}
|
|
363
|
+
if (nextPage) lines.push("", `More pages exist: ask for page ${nextPage}.`);
|
|
364
|
+
return lines.join("\n");
|
|
365
|
+
}
|