seo-supervisor 1.2.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.
- package/.claude-plugin/icon.png +0 -0
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +16 -3
- package/CHANGELOG.md +430 -0
- package/NOTICE +7 -0
- package/README.md +230 -25
- package/agents/seo-auditor.md +16 -0
- package/agents/seo-reviewer.md +16 -0
- package/bin/agents.mjs +194 -29
- package/bin/extras.mjs +422 -0
- package/bin/prompt.mjs +34 -14
- package/bin/seo-supervisor.mjs +386 -53
- package/commands/seo-audit.md +29 -3
- package/commands/seo-brief.md +28 -0
- package/commands/seo-connect.md +27 -0
- package/commands/seo-fix.md +24 -3
- package/commands/seo-grow.md +26 -0
- package/commands/seo-launch.md +17 -3
- package/commands/seo-review.md +13 -1
- package/commands/seo-rewrite.md +25 -0
- package/commands/seo-setup.md +17 -1
- package/commands/seo-watch.md +28 -0
- package/hooks/hooks.json +117 -0
- package/package.json +20 -7
- package/skills/seo-supervisor/NOTICE +7 -0
- package/skills/seo-supervisor/SKILL.md +302 -21
- package/skills/seo-supervisor/audience.md +61 -0
- package/skills/seo-supervisor/examples/drafts/security.md +41 -0
- package/skills/seo-supervisor/examples/hooks/README.md +28 -0
- package/skills/seo-supervisor/examples/hooks/claude-code-settings.json +15 -0
- package/skills/seo-supervisor/examples/hooks/codex-hooks.json +17 -0
- package/skills/seo-supervisor/examples/hooks/copilot-hooks.json +15 -0
- package/skills/seo-supervisor/examples/hooks/cursor-hooks.json +11 -0
- package/skills/seo-supervisor/examples/hooks/github-actions.yml +21 -0
- package/skills/seo-supervisor/examples/hooks/pre-commit +17 -0
- package/skills/seo-supervisor/examples/hooks/reading-ease.yml +24 -0
- package/skills/seo-supervisor/examples/sample-audit.json +94 -2
- package/skills/seo-supervisor/examples/sample-report.html +111 -61
- package/skills/seo-supervisor/examples/sample-report.md +147 -44
- package/skills/seo-supervisor/examples/scheduled-runs.md +89 -0
- package/skills/seo-supervisor/examples/snippets.md +648 -1
- package/skills/seo-supervisor/grow.md +342 -0
- package/skills/seo-supervisor/og-images.md +85 -0
- package/skills/seo-supervisor/page-types.md +124 -0
- package/skills/seo-supervisor/planning.md +19 -7
- package/skills/seo-supervisor/profile-template.md +41 -0
- package/skills/seo-supervisor/report-format.md +177 -10
- package/skills/seo-supervisor/rewrite.md +59 -0
- package/skills/seo-supervisor/rules/ai-search.md +65 -12
- package/skills/seo-supervisor/rules/content.md +33 -9
- package/skills/seo-supervisor/rules/crawl-index.md +33 -6
- package/skills/seo-supervisor/rules/internal-linking.md +49 -6
- package/skills/seo-supervisor/rules/launch-monitoring.md +17 -8
- package/skills/seo-supervisor/rules/media.md +13 -1
- package/skills/seo-supervisor/rules/metadata.md +41 -9
- package/skills/seo-supervisor/rules/performance.md +50 -5
- package/skills/seo-supervisor/rules/rendering.md +39 -2
- package/skills/seo-supervisor/rules/search-intent.md +32 -0
- package/skills/seo-supervisor/rules/sitemaps-robots.md +24 -12
- package/skills/seo-supervisor/rules/structured-data.md +37 -14
- package/skills/seo-supervisor/rules/trust-security.md +25 -2
- package/skills/seo-supervisor/rules/urls.md +10 -2
- package/skills/seo-supervisor/scripts/ai-answers.mjs +314 -0
- package/skills/seo-supervisor/scripts/backlinks.mjs +412 -0
- package/skills/seo-supervisor/scripts/change-check.mjs +281 -0
- package/skills/seo-supervisor/scripts/checks/agents.mjs +488 -0
- package/skills/seo-supervisor/scripts/checks/assets.mjs +404 -0
- package/skills/seo-supervisor/scripts/checks/bots.mjs +146 -0
- package/skills/seo-supervisor/scripts/checks/business-type.mjs +193 -0
- package/skills/seo-supervisor/scripts/checks/content.mjs +391 -0
- package/skills/seo-supervisor/scripts/checks/coverage.mjs +184 -0
- package/skills/seo-supervisor/scripts/checks/crawl.mjs +95 -0
- package/skills/seo-supervisor/scripts/checks/hreflang.mjs +396 -0
- package/skills/seo-supervisor/scripts/checks/hygiene.mjs +227 -0
- package/skills/seo-supervisor/scripts/checks/lighthouse.mjs +271 -0
- package/skills/seo-supervisor/scripts/checks/link-strength.mjs +59 -0
- package/skills/seo-supervisor/scripts/checks/links.mjs +360 -0
- package/skills/seo-supervisor/scripts/checks/local.mjs +101 -0
- package/skills/seo-supervisor/scripts/checks/media.mjs +194 -0
- package/skills/seo-supervisor/scripts/checks/outbound.mjs +53 -0
- package/skills/seo-supervisor/scripts/checks/page.mjs +223 -0
- package/skills/seo-supervisor/scripts/checks/probes.mjs +130 -0
- package/skills/seo-supervisor/scripts/checks/render.mjs +552 -0
- package/skills/seo-supervisor/scripts/checks/robots.mjs +144 -0
- package/skills/seo-supervisor/scripts/checks/schema.mjs +206 -0
- package/skills/seo-supervisor/scripts/checks/site-agents.mjs +79 -0
- package/skills/seo-supervisor/scripts/checks/site-content.mjs +135 -0
- package/skills/seo-supervisor/scripts/checks/site-hygiene.mjs +202 -0
- package/skills/seo-supervisor/scripts/checks/site-pages.mjs +474 -0
- package/skills/seo-supervisor/scripts/checks/site-security.mjs +102 -0
- package/skills/seo-supervisor/scripts/checks/sitemap.mjs +473 -0
- package/skills/seo-supervisor/scripts/checks/speed.mjs +267 -0
- package/skills/seo-supervisor/scripts/checks/technical.mjs +368 -0
- package/skills/seo-supervisor/scripts/checks/tls.mjs +297 -0
- package/skills/seo-supervisor/scripts/checks/trust.mjs +421 -0
- package/skills/seo-supervisor/scripts/checks/visual.mjs +1132 -0
- package/skills/seo-supervisor/scripts/checks/writing.mjs +192 -0
- package/skills/seo-supervisor/scripts/commoncrawl.mjs +452 -0
- package/skills/seo-supervisor/scripts/connect-keys.mjs +422 -0
- package/skills/seo-supervisor/scripts/content-scan.mjs +515 -158
- package/skills/seo-supervisor/scripts/crawl/catalog.mjs +1248 -0
- package/skills/seo-supervisor/scripts/crawl/fetch.mjs +362 -0
- package/skills/seo-supervisor/scripts/crawl/page.mjs +741 -0
- package/skills/seo-supervisor/scripts/crawl/queue.mjs +190 -0
- package/skills/seo-supervisor/scripts/dashboard.mjs +140 -0
- package/skills/seo-supervisor/scripts/data/ai-bots.json +38 -0
- package/skills/seo-supervisor/scripts/data/build-schema-vocab.mjs +57 -0
- package/skills/seo-supervisor/scripts/data/business-types.json +338 -0
- package/skills/seo-supervisor/scripts/data/google-rich-results.json +79 -0
- package/skills/seo-supervisor/scripts/data/schema-vocab.json +1 -0
- package/skills/seo-supervisor/scripts/deploy-check.mjs +462 -0
- package/skills/seo-supervisor/scripts/disavow.mjs +176 -0
- package/skills/seo-supervisor/scripts/doctor.mjs +174 -0
- package/skills/seo-supervisor/scripts/geo-grid.mjs +371 -0
- package/skills/seo-supervisor/scripts/google/search-console.mjs +151 -0
- package/skills/seo-supervisor/scripts/google/session.mjs +167 -0
- package/skills/seo-supervisor/scripts/google-updates.mjs +189 -0
- package/skills/seo-supervisor/scripts/google.mjs +1341 -0
- package/skills/seo-supervisor/scripts/gsc-import.mjs +298 -0
- package/skills/seo-supervisor/scripts/guard/adapters/claude-code.mjs +149 -0
- package/skills/seo-supervisor/scripts/guard/adapters/cline.mjs +28 -0
- package/skills/seo-supervisor/scripts/guard/adapters/codex.mjs +26 -0
- package/skills/seo-supervisor/scripts/guard/adapters/copilot.mjs +36 -0
- package/skills/seo-supervisor/scripts/guard/adapters/cursor.mjs +33 -0
- package/skills/seo-supervisor/scripts/guard/adapters/devin.mjs +25 -0
- package/skills/seo-supervisor/scripts/guard/adapters/edits.mjs +124 -0
- package/skills/seo-supervisor/scripts/guard/mode.mjs +37 -0
- package/skills/seo-supervisor/scripts/guard/rules.mjs +931 -0
- package/skills/seo-supervisor/scripts/guard.mjs +74 -0
- package/skills/seo-supervisor/scripts/images.mjs +408 -0
- package/skills/seo-supervisor/scripts/indexnow.mjs +300 -0
- package/skills/seo-supervisor/scripts/issues.mjs +217 -0
- package/skills/seo-supervisor/scripts/keys/authority.mjs +135 -0
- package/skills/seo-supervisor/scripts/keys/bing.mjs +239 -0
- package/skills/seo-supervisor/scripts/keys/clarity.mjs +137 -0
- package/skills/seo-supervisor/scripts/keys/dataforseo.mjs +57 -0
- package/skills/seo-supervisor/scripts/keys/googleplaces.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/http.mjs +212 -0
- package/skills/seo-supervisor/scripts/keys/matomo.mjs +138 -0
- package/skills/seo-supervisor/scripts/keys/openai.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/perplexity.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/posthog.mjs +165 -0
- package/skills/seo-supervisor/scripts/keys/semrush.mjs +25 -0
- package/skills/seo-supervisor/scripts/keys/services.mjs +41 -0
- package/skills/seo-supervisor/scripts/keys/store.mjs +140 -0
- package/skills/seo-supervisor/scripts/keys/visits.mjs +58 -0
- package/skills/seo-supervisor/scripts/keys/youtube.mjs +84 -0
- package/skills/seo-supervisor/scripts/keyword-data.mjs +316 -0
- package/skills/seo-supervisor/scripts/keyword-ideas.mjs +503 -0
- package/skills/seo-supervisor/scripts/lib/args.mjs +56 -0
- package/skills/seo-supervisor/scripts/lib/business.mjs +27 -0
- package/skills/seo-supervisor/scripts/lib/cdp.mjs +242 -0
- package/skills/seo-supervisor/scripts/lib/cli.mjs +56 -0
- package/skills/seo-supervisor/scripts/lib/csv.mjs +38 -0
- package/skills/seo-supervisor/scripts/lib/findings.mjs +100 -0
- package/skills/seo-supervisor/scripts/lib/google-client.mjs +9 -0
- package/skills/seo-supervisor/scripts/lib/html.mjs +146 -0
- package/skills/seo-supervisor/scripts/lib/http.mjs +28 -0
- package/skills/seo-supervisor/scripts/lib/image-size.mjs +78 -0
- package/skills/seo-supervisor/scripts/lib/is-main.mjs +13 -0
- package/skills/seo-supervisor/scripts/lib/net.mjs +116 -0
- package/skills/seo-supervisor/scripts/lib/proxy.mjs +47 -0
- package/skills/seo-supervisor/scripts/lib/recheck.mjs +106 -0
- package/skills/seo-supervisor/scripts/lib/redact.mjs +78 -0
- package/skills/seo-supervisor/scripts/lib/robots.mjs +73 -0
- package/skills/seo-supervisor/scripts/lib/score.mjs +266 -0
- package/skills/seo-supervisor/scripts/lib/settings-dir.mjs +81 -0
- package/skills/seo-supervisor/scripts/links-import.mjs +603 -0
- package/skills/seo-supervisor/scripts/log-scan.mjs +597 -0
- package/skills/seo-supervisor/scripts/og-card.mjs +273 -0
- package/skills/seo-supervisor/scripts/osm.mjs +359 -0
- package/skills/seo-supervisor/scripts/rdap.mjs +164 -0
- package/skills/seo-supervisor/scripts/render-report.mjs +324 -690
- package/skills/seo-supervisor/scripts/report/client.js +37 -0
- package/skills/seo-supervisor/scripts/report/diff.mjs +264 -0
- package/skills/seo-supervisor/scripts/report/findings.mjs +165 -0
- package/skills/seo-supervisor/scripts/report/format.mjs +111 -0
- package/skills/seo-supervisor/scripts/report/markdown.mjs +250 -0
- package/skills/seo-supervisor/scripts/report/model.mjs +171 -0
- package/skills/seo-supervisor/scripts/report/overview-lines.mjs +158 -0
- package/skills/seo-supervisor/scripts/report/overview.mjs +224 -0
- package/skills/seo-supervisor/scripts/report/page.mjs +127 -0
- package/skills/seo-supervisor/scripts/report/panel.mjs +172 -0
- package/skills/seo-supervisor/scripts/report/panels/ai.mjs +211 -0
- package/skills/seo-supervisor/scripts/report/panels/connections.mjs +139 -0
- package/skills/seo-supervisor/scripts/report/panels/content.mjs +204 -0
- package/skills/seo-supervisor/scripts/report/panels/data-search.mjs +379 -0
- package/skills/seo-supervisor/scripts/report/panels/data-site.mjs +44 -0
- package/skills/seo-supervisor/scripts/report/panels/links.mjs +380 -0
- package/skills/seo-supervisor/scripts/report/panels/local.mjs +221 -0
- package/skills/seo-supervisor/scripts/report/panels/site.mjs +359 -0
- package/skills/seo-supervisor/scripts/report/panels/speed-media.mjs +267 -0
- package/skills/seo-supervisor/scripts/report/sources.mjs +163 -0
- package/skills/seo-supervisor/scripts/report/styles.css +223 -0
- package/skills/seo-supervisor/scripts/report/traffic.mjs +264 -0
- package/skills/seo-supervisor/scripts/report-plan.mjs +112 -33
- package/skills/seo-supervisor/scripts/schema-check.mjs +717 -0
- package/skills/seo-supervisor/scripts/seo-crawl.mjs +1086 -638
- package/skills/seo-supervisor/scripts/seo-diff.mjs +584 -0
- package/skills/seo-supervisor/scripts/update-check.mjs +82 -0
- package/skills/seo-supervisor/site-types/content.md +34 -1
- package/skills/seo-supervisor/site-types/ecommerce.md +38 -1
- package/skills/seo-supervisor/site-types/local-business.md +96 -6
- package/skills/seo-supervisor/site-types/services.md +46 -0
- package/skills/seo-supervisor/site-types/tools.md +3 -3
- package/skills/seo-supervisor/strategy.md +232 -0
- package/skills/seo-supervisor/troubleshooting.md +56 -0
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// Checks JSON-LD structured data offline, against the schema.org vocabulary and Google's rich-result requirements.
|
|
4
|
+
import { readFileSync, writeFileSync, mkdirSync, readdirSync, statSync, existsSync } from "node:fs";
|
|
5
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
6
|
+
import { isMain } from "./lib/is-main.mjs";
|
|
7
|
+
import { parseArgs } from "./lib/args.mjs";
|
|
8
|
+
import { Fail, runMain, warnProxy } from "./lib/cli.mjs";
|
|
9
|
+
import { bodyUpTo } from "./lib/http.mjs";
|
|
10
|
+
import { loopback } from "./lib/net.mjs";
|
|
11
|
+
import { ancestors, isA, isLocalBusinessType } from "./lib/business.mjs";
|
|
12
|
+
import {
|
|
13
|
+
EVENT_DOC,
|
|
14
|
+
MERCHANT_DOC,
|
|
15
|
+
REVIEW_DOC,
|
|
16
|
+
SUPERSEDED,
|
|
17
|
+
merchantProblems,
|
|
18
|
+
onlineOnly,
|
|
19
|
+
otherReviewed,
|
|
20
|
+
placeholderIn,
|
|
21
|
+
sellsWithOffer,
|
|
22
|
+
} from "./checks/schema.mjs";
|
|
23
|
+
// The shared checks live in checks/schema.mjs and are re-exported here for callers of this module.
|
|
24
|
+
export {
|
|
25
|
+
ISO_4217,
|
|
26
|
+
SUPERSEDED,
|
|
27
|
+
merchantProblems,
|
|
28
|
+
onlineOnly,
|
|
29
|
+
otherReviewed,
|
|
30
|
+
placeholderIn,
|
|
31
|
+
reviewEventProblems,
|
|
32
|
+
sellsWithOffer,
|
|
33
|
+
} from "./checks/schema.mjs";
|
|
34
|
+
|
|
35
|
+
const VOCAB = JSON.parse(readFileSync(new URL("./data/schema-vocab.json", import.meta.url), "utf8"));
|
|
36
|
+
// Null-prototype maps, so "toString" or "__proto__" in a page is just an unknown name.
|
|
37
|
+
const bare = o => Object.assign(Object.create(null), o);
|
|
38
|
+
const TYPES = bare(VOCAB.types),
|
|
39
|
+
PROPS = bare(VOCAB.props);
|
|
40
|
+
|
|
41
|
+
// The file's `about` explains each rule field. A missing required property is an error, a missing recommended one a
|
|
42
|
+
// warning; a nested list means "one of these".
|
|
43
|
+
// https://developers.google.com/search/docs/appearance/structured-data/search-gallery
|
|
44
|
+
const GOOGLE = JSON.parse(readFileSync(new URL("./data/google-rich-results.json", import.meta.url), "utf8"));
|
|
45
|
+
|
|
46
|
+
const RULES = [
|
|
47
|
+
...GOOGLE.supported,
|
|
48
|
+
// Merchant listings need an image, checked on top-level Products only: one nested as isSimilarTo or itemOffered isn't
|
|
49
|
+
// a listing, and Google's variants doc doesn't say whether a variant may take the group's image. Brand, GTIN, colour
|
|
50
|
+
// and the like apply only to some products, so the crawler's report counts them instead.
|
|
51
|
+
{
|
|
52
|
+
type: "Product",
|
|
53
|
+
except: "ProductGroup",
|
|
54
|
+
top: true,
|
|
55
|
+
feature: "Google merchant listings",
|
|
56
|
+
doc: MERCHANT_DOC,
|
|
57
|
+
required: ["image"],
|
|
58
|
+
test: sellsWithOffer,
|
|
59
|
+
},
|
|
60
|
+
// Google ended FAQ rich results on 7 May 2026; Bing and AI engines still read FAQPage, so only its structure is
|
|
61
|
+
// checked.
|
|
62
|
+
{
|
|
63
|
+
type: "FAQPage",
|
|
64
|
+
feature: "a well-formed FAQPage (Google shows no FAQ rich results since May 2026)",
|
|
65
|
+
doc: "https://schema.org/FAQPage",
|
|
66
|
+
required: ["mainEntity"],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
type: "Question",
|
|
70
|
+
under: "FAQPage",
|
|
71
|
+
feature: "a well-formed FAQPage (Google shows no FAQ rich results since May 2026)",
|
|
72
|
+
doc: "https://schema.org/FAQPage",
|
|
73
|
+
required: ["name", "acceptedAnswer"],
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
type: "Answer",
|
|
77
|
+
under: "Question",
|
|
78
|
+
feature: "a well-formed FAQPage or Q&A",
|
|
79
|
+
doc: "https://schema.org/Answer",
|
|
80
|
+
required: ["text"],
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
const RETIRED = GOOGLE.retired.filter(r => !r.keep),
|
|
84
|
+
KEPT = GOOGLE.retired.filter(r => r.keep && r.note);
|
|
85
|
+
const docOf = type => GOOGLE.supported.find(r => r.type === type).doc;
|
|
86
|
+
|
|
87
|
+
const monthOf = d =>
|
|
88
|
+
new Date(d + "T00:00:00Z").toLocaleDateString("en-GB", { month: "long", year: "numeric", timeZone: "UTC" });
|
|
89
|
+
|
|
90
|
+
// The lookup maps are built on first use: the crawler imports this module for a few helpers that need none of them.
|
|
91
|
+
let byDomain, typeLc, propLc;
|
|
92
|
+
const domainProps = () => {
|
|
93
|
+
if (!byDomain) {
|
|
94
|
+
byDomain = Object.create(null);
|
|
95
|
+
for (const [p, [domains]] of Object.entries(PROPS)) for (const d of domains) (byDomain[d] ||= []).push(p);
|
|
96
|
+
}
|
|
97
|
+
return byDomain;
|
|
98
|
+
};
|
|
99
|
+
const allowedCache = new Map();
|
|
100
|
+
const allowed = t =>
|
|
101
|
+
allowedCache.get(t) || allowedCache.set(t, new Set([...ancestors(t)].flatMap(a => domainProps()[a] || []))).get(t);
|
|
102
|
+
const lowerMap = o => bare(Object.fromEntries(Object.keys(o).map(k => [k.toLowerCase(), k])));
|
|
103
|
+
const TYPE_LC = () => (typeLc ||= lowerMap(TYPES)),
|
|
104
|
+
PROP_LC = () => (propLc ||= lowerMap(PROPS));
|
|
105
|
+
const hint = (lc, t) =>
|
|
106
|
+
lc()[t.toLowerCase()] && lc()[t.toLowerCase()] !== t ? `; did you mean "${lc()[t.toLowerCase()]}"?` : "";
|
|
107
|
+
const term = s => s.replace(/^(?:https?:\/\/schema\.org\/|schema:)/, "");
|
|
108
|
+
const has = (o, k) => o[k] != null && o[k] !== "" && !(Array.isArray(o[k]) && !o[k].length);
|
|
109
|
+
const list = x => [].concat(x ?? []);
|
|
110
|
+
const clip = s => (s.length > 60 ? s.slice(0, 57) + "…" : s);
|
|
111
|
+
const ISO_DATE =
|
|
112
|
+
/^\d{4}(-(0[1-9]|1[0-2])(-(0[1-9]|[12]\d|3[01])(T([01]\d|2[0-3]):[0-5]\d(:[0-5]\d(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?)?)?)?$/;
|
|
113
|
+
const NUMERIC = new Set(["Number", "Integer", "Float"]);
|
|
114
|
+
|
|
115
|
+
/** Checks one JSON-LD block. Returns issues; `pos` is the character offset in `text` they point at. */
|
|
116
|
+
export function checkJsonLd(text) {
|
|
117
|
+
// Hugo, Jekyll, Liquid, Eleventy, Django/Jinja ({{ }}, {% %}, {# #}) and ERB/EJS (<% %>): the JSON-LD only exists
|
|
118
|
+
// once built.
|
|
119
|
+
if (/\{\{|\{%|\{#|<%/.test(text))
|
|
120
|
+
return [
|
|
121
|
+
{
|
|
122
|
+
severity: "note",
|
|
123
|
+
check: "templated",
|
|
124
|
+
rule: "SCHEMA-08",
|
|
125
|
+
path: "$",
|
|
126
|
+
type: "",
|
|
127
|
+
message: "templated; check the built output",
|
|
128
|
+
pos: text.length - text.trimStart().length,
|
|
129
|
+
},
|
|
130
|
+
];
|
|
131
|
+
let data;
|
|
132
|
+
// Each object's numbers as the page wrote them, so a latitude of 53.79640 keeps its five decimal places.
|
|
133
|
+
const written = new WeakMap();
|
|
134
|
+
function keepSource(k, v, c) {
|
|
135
|
+
if (typeof v === "number" && c?.source) (written.get(this) || written.set(this, {}).get(this))[k] = c.source;
|
|
136
|
+
return v;
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
data = JSON.parse(text.replace(/^/, ""), keepSource);
|
|
140
|
+
} catch (e) {
|
|
141
|
+
const pos = Number(/at position (\d+)/.exec(e.message)?.[1] ?? text.length);
|
|
142
|
+
return [
|
|
143
|
+
{
|
|
144
|
+
severity: "error",
|
|
145
|
+
check: "json-invalid",
|
|
146
|
+
rule: "SCHEMA-01",
|
|
147
|
+
path: "$",
|
|
148
|
+
type: "",
|
|
149
|
+
message: `the JSON doesn't parse: ${e.message.replace(/ in JSON at position.*$/s, "")}`,
|
|
150
|
+
pos,
|
|
151
|
+
},
|
|
152
|
+
];
|
|
153
|
+
}
|
|
154
|
+
const issues = [];
|
|
155
|
+
// Points at the key's first occurrence in the block, not a full JSON position map: enough to find the line.
|
|
156
|
+
const add = (severity, check, path, types, message, extra = {}) => {
|
|
157
|
+
const { key, ...rest } = extra;
|
|
158
|
+
issues.push({
|
|
159
|
+
severity,
|
|
160
|
+
check,
|
|
161
|
+
rule: "SCHEMA-08",
|
|
162
|
+
path,
|
|
163
|
+
type: types.join(", "),
|
|
164
|
+
message,
|
|
165
|
+
...rest,
|
|
166
|
+
pos: key ? Math.max(0, text.indexOf(JSON.stringify(key))) : 0,
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
function shape(key, k, x, path, types) {
|
|
171
|
+
const [, range] = PROPS[k],
|
|
172
|
+
s = typeof x === "string" ? x.trim() : null;
|
|
173
|
+
if (s != null && range.includes("URL") && !range.includes("Text")) {
|
|
174
|
+
if (/^\.{0,2}\//.test(s))
|
|
175
|
+
add(
|
|
176
|
+
"warning",
|
|
177
|
+
"value-url",
|
|
178
|
+
path,
|
|
179
|
+
types,
|
|
180
|
+
`"${key}" is a relative URL ("${clip(s)}"); use the full https:// URL`,
|
|
181
|
+
{ key },
|
|
182
|
+
);
|
|
183
|
+
else if (!/^[a-z][a-z\d+.-]*:\S+$/i.test(s))
|
|
184
|
+
add("error", "value-url", path, types, `"${key}" should be a URL, not "${clip(s)}"`, { key });
|
|
185
|
+
}
|
|
186
|
+
if (
|
|
187
|
+
range.length &&
|
|
188
|
+
range.every(r => r === "Date" || r === "DateTime") &&
|
|
189
|
+
(s != null || typeof x === "number") &&
|
|
190
|
+
!ISO_DATE.test(String(s ?? x))
|
|
191
|
+
)
|
|
192
|
+
add(
|
|
193
|
+
"error",
|
|
194
|
+
"value-date",
|
|
195
|
+
path,
|
|
196
|
+
types,
|
|
197
|
+
`"${key}" should be an ISO 8601 date such as 2026-11-19 or 2026-11-19T09:30:00+01:00, not "${clip(String(x))}"`,
|
|
198
|
+
{ key },
|
|
199
|
+
);
|
|
200
|
+
if (range.length && range.every(r => NUMERIC.has(r)) && s != null && !/^-?\d+(\.\d+)?$/.test(s))
|
|
201
|
+
add("error", "value-number", path, types, `"${key}" should be a number, not "${clip(s)}"`, { key });
|
|
202
|
+
if (/^(price|lowPrice|highPrice)$/.test(k) && s != null && !/^\d+(\.\d+)?$/.test(s))
|
|
203
|
+
add(
|
|
204
|
+
"warning",
|
|
205
|
+
"value-price",
|
|
206
|
+
path,
|
|
207
|
+
types,
|
|
208
|
+
`"${key}" is "${clip(s)}"; Google wants digits with a dot for decimals, no currency symbol or thousands separator`,
|
|
209
|
+
{ key },
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// A rule applies to its type and subtypes, minus `except`, nested `under` one of its types (through `as`), and only
|
|
214
|
+
// when the item has one of the `when` properties ("*": any value, else that value in any case, so false matches
|
|
215
|
+
// "False" and "https://schema.org/ReturnShippingFees" matches "ReturnShippingFees").
|
|
216
|
+
const applies = (r, v, known, parent, via) =>
|
|
217
|
+
known.some(t => isA(t, r.type) && !list(r.except).some(e => isA(t, e))) &&
|
|
218
|
+
(!r.under || !!parent?.some(p => list(r.under).some(u => isA(p, u)))) &&
|
|
219
|
+
!(r.top && parent) &&
|
|
220
|
+
!(r.as && via !== r.as) &&
|
|
221
|
+
(!r.when ||
|
|
222
|
+
Object.entries(r.when).some(([k, want]) =>
|
|
223
|
+
want === "*"
|
|
224
|
+
? has(v, k)
|
|
225
|
+
: list(v[k]).some(x => term(String(x).trim()).toLowerCase() === String(want).toLowerCase()),
|
|
226
|
+
)) &&
|
|
227
|
+
(!r.test || r.test(v));
|
|
228
|
+
|
|
229
|
+
function google(v, path, known, parent, last, via) {
|
|
230
|
+
const seen = new Set(),
|
|
231
|
+
rules = RULES.filter(r => applies(r, v, known, parent, via));
|
|
232
|
+
const label = x =>
|
|
233
|
+
list(x)
|
|
234
|
+
.map(k => `"${k}"`)
|
|
235
|
+
.join(" or ");
|
|
236
|
+
// Every rule's required properties first, so one rule's recommended "name" never hides another's required one.
|
|
237
|
+
for (const verb of ["required", "recommended"])
|
|
238
|
+
for (const r of rules) {
|
|
239
|
+
const present = k =>
|
|
240
|
+
has(v, k) || (r.alsoOn?.[k] && list(v[r.alsoOn[k]]).some(o => o && typeof o === "object" && has(o, k)));
|
|
241
|
+
const props =
|
|
242
|
+
verb === "required"
|
|
243
|
+
? [...r.required, ...(parent && via !== r.via ? [] : r.standalone || [])].filter(
|
|
244
|
+
x => !(last && x === r.lastMayOmit),
|
|
245
|
+
)
|
|
246
|
+
: r.recommended || [];
|
|
247
|
+
for (const x of props) {
|
|
248
|
+
if (list(x).some(present) || seen.has(label(x))) continue;
|
|
249
|
+
seen.add(label(x));
|
|
250
|
+
// An item with r.elsewhere (a forum post's url) may stand for one shown in full on another page.
|
|
251
|
+
if (verb === "required" && Array.isArray(x) && r.elsewhere && has(v, r.elsewhere))
|
|
252
|
+
add(
|
|
253
|
+
"note",
|
|
254
|
+
"required-elsewhere",
|
|
255
|
+
path,
|
|
256
|
+
known,
|
|
257
|
+
`no ${label(x)}: fine if this item stands for one shown on another page, at its "${r.elsewhere}" (a category page, a thread's later pages); on its own page, ${r.feature} require one`,
|
|
258
|
+
{ doc: r.doc },
|
|
259
|
+
);
|
|
260
|
+
else
|
|
261
|
+
add(
|
|
262
|
+
verb === "required" ? "error" : "warning",
|
|
263
|
+
`${verb}-missing`,
|
|
264
|
+
path,
|
|
265
|
+
known,
|
|
266
|
+
`missing ${label(x)}, ${verb} for ${r.feature}`,
|
|
267
|
+
{ doc: r.doc },
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
// A rating can't be filled in like a missing name: it has to come from real reviews shown on the page.
|
|
271
|
+
if (
|
|
272
|
+
verb === "recommended" &&
|
|
273
|
+
r.needsRating &&
|
|
274
|
+
!r.needsRating.some(present) &&
|
|
275
|
+
!seen.has(label(r.needsRating))
|
|
276
|
+
) {
|
|
277
|
+
seen.add(label(r.needsRating));
|
|
278
|
+
add(
|
|
279
|
+
"note",
|
|
280
|
+
"rating-missing",
|
|
281
|
+
path,
|
|
282
|
+
known,
|
|
283
|
+
`no ${label(r.needsRating)}: ${r.feature} show only with one. Add it only from real reviews shown on this page (SCHEMA-04); never make one up`,
|
|
284
|
+
{ doc: r.doc },
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// Exact types only: a Recipe is a HowTo in schema.org, and still earns recipe results.
|
|
289
|
+
for (const r of RETIRED)
|
|
290
|
+
if (known.includes(r.type) && !(r.top && parent))
|
|
291
|
+
add(
|
|
292
|
+
"warning",
|
|
293
|
+
"type-retired",
|
|
294
|
+
path,
|
|
295
|
+
known,
|
|
296
|
+
`Google retired ${r.feature} in ${monthOf(r.since)}, so "${r.type}" no longer earns a Google rich result. It does no harm: remove it, or keep it knowingly`,
|
|
297
|
+
{ doc: r.source, rule: "SCHEMA-05", key: r.type },
|
|
298
|
+
);
|
|
299
|
+
// A retired type still worth keeping gets a note, not a warning.
|
|
300
|
+
for (const r of KEPT)
|
|
301
|
+
if (known.includes(r.type) && !(r.top && parent))
|
|
302
|
+
add(
|
|
303
|
+
"note",
|
|
304
|
+
"type-kept",
|
|
305
|
+
path,
|
|
306
|
+
known,
|
|
307
|
+
`Google retired ${r.feature} in ${monthOf(r.since)}, but keep "${r.type}": ${r.note}`,
|
|
308
|
+
{ doc: r.source, rule: "SCHEMA-05", key: r.type },
|
|
309
|
+
);
|
|
310
|
+
for (const t of known)
|
|
311
|
+
if (SUPERSEDED[t])
|
|
312
|
+
add(
|
|
313
|
+
"warning",
|
|
314
|
+
"type-superseded",
|
|
315
|
+
path,
|
|
316
|
+
known,
|
|
317
|
+
`schema.org has deprecated "${t}" and says to use "${SUPERSEDED[t]}" instead`,
|
|
318
|
+
{ doc: `https://schema.org/${t}`, rule: "SCHEMA-05", key: t },
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// A business's map pin and price range, as Google's local business and vacation rental docs set them.
|
|
323
|
+
function business(v, path, known) {
|
|
324
|
+
if (!known.some(isLocalBusinessType)) return;
|
|
325
|
+
const doc = docOf(known.some(t => isA(t, "VacationRental")) ? "VacationRental" : "LocalBusiness");
|
|
326
|
+
// Both docs want latitude and longitude to at least 5 decimal places.
|
|
327
|
+
for (const o of [v, ...list(v.geo)])
|
|
328
|
+
for (const k of ["latitude", "longitude"]) {
|
|
329
|
+
if (!o || typeof o !== "object" || o[k] == null) continue;
|
|
330
|
+
const s = String(written.get(o)?.[k] ?? o[k]).trim(),
|
|
331
|
+
m = /^[+-]?\d+(?:\.(\d*))?$/.exec(s),
|
|
332
|
+
n = m?.[1]?.length ?? 0;
|
|
333
|
+
if (m && n < 5)
|
|
334
|
+
add(
|
|
335
|
+
"warning",
|
|
336
|
+
"value-geo",
|
|
337
|
+
path,
|
|
338
|
+
known,
|
|
339
|
+
`"${k}" is ${s} (${n} decimal place${n === 1 ? "" : "s"}); Google wants at least 5, so the map pin lands on the right building`,
|
|
340
|
+
{ doc, key: k },
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
const range = typeof v.priceRange === "string" ? v.priceRange.trim().length : 0;
|
|
344
|
+
if (range >= 100)
|
|
345
|
+
add(
|
|
346
|
+
"warning",
|
|
347
|
+
"value-price-range",
|
|
348
|
+
path,
|
|
349
|
+
known,
|
|
350
|
+
`"priceRange" is ${range} characters; Google shows no price range at 100 characters or more ("$10-15" or "$$$" is the form it wants)`,
|
|
351
|
+
{ doc: docOf("LocalBusiness"), key: "priceRange" },
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// EU energy labels need the EPREL code; a shop serving Norway, Switzerland or the UK without codes may give
|
|
356
|
+
// certificationRating instead. Google still reads hasEnergyConsumptionDetails but recommends hasCertification.
|
|
357
|
+
function merchant(v, path, known) {
|
|
358
|
+
if (known.some(t => isA(t, "Product"))) {
|
|
359
|
+
for (const x of merchantProblems(v))
|
|
360
|
+
add(x.severity, `value-${x.check}`, path, known, x.message, { doc: MERCHANT_DOC, key: x.key });
|
|
361
|
+
if (has(v, "hasEnergyConsumptionDetails"))
|
|
362
|
+
add(
|
|
363
|
+
"note",
|
|
364
|
+
"energy-details",
|
|
365
|
+
path,
|
|
366
|
+
known,
|
|
367
|
+
`Google still reads "hasEnergyConsumptionDetails", but now recommends an energy label in "hasCertification" instead`,
|
|
368
|
+
{ doc: MERCHANT_DOC, key: "hasEnergyConsumptionDetails" },
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
if (
|
|
372
|
+
known.some(t => isA(t, "Certification")) &&
|
|
373
|
+
list(v.name).some(n => String(n).trim().toUpperCase() === "EPREL") &&
|
|
374
|
+
!has(v, "certificationIdentification")
|
|
375
|
+
)
|
|
376
|
+
add(
|
|
377
|
+
"note",
|
|
378
|
+
"eprel-code",
|
|
379
|
+
path,
|
|
380
|
+
known,
|
|
381
|
+
`an EU energy label ("EPREL") needs "certificationIdentification", the product's code in the EPREL database; only a shop serving Norway, Switzerland or the UK without one may give "certificationRating" instead`,
|
|
382
|
+
{ doc: MERCHANT_DOC, key: "name" },
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function walk(v, path, parent, last, via) {
|
|
387
|
+
if (Array.isArray(v)) return v.forEach((x, i) => walk(x, `${path}[${i}]`, parent, i === v.length - 1, via));
|
|
388
|
+
if (!v || typeof v !== "object") return;
|
|
389
|
+
const types = []
|
|
390
|
+
.concat(v["@type"] ?? [])
|
|
391
|
+
.filter(t => typeof t === "string")
|
|
392
|
+
.map(term)
|
|
393
|
+
.filter(t => !t.includes(":"));
|
|
394
|
+
const known = types.filter(t => TYPES[t]);
|
|
395
|
+
for (const t of types)
|
|
396
|
+
if (!TYPES[t])
|
|
397
|
+
add("error", "type-unknown", path, types, `"${t}" isn't a schema.org type${hint(TYPE_LC, t)}`, { key: t });
|
|
398
|
+
// An empty @type anywhere; no @type on a top-level or @graph item that has properties (via is unset only there),
|
|
399
|
+
// unless its @id names an item typed elsewhere in the block, which it adds to. Nested values may go untyped.
|
|
400
|
+
if (Object.hasOwn(v, "@type") && !list(v["@type"]).some(t => typeof t === "string" && t.trim()))
|
|
401
|
+
add("error", "type-empty", path, [], `"@type" is empty; name the type, such as "Organization" or "Article"`, {
|
|
402
|
+
key: "@type",
|
|
403
|
+
});
|
|
404
|
+
else if (
|
|
405
|
+
via === undefined &&
|
|
406
|
+
!Object.hasOwn(v, "@type") &&
|
|
407
|
+
Object.keys(v).some(k => !k.startsWith("@")) &&
|
|
408
|
+
!typedIds.has(v["@id"])
|
|
409
|
+
)
|
|
410
|
+
add(
|
|
411
|
+
"error",
|
|
412
|
+
"type-missing",
|
|
413
|
+
path,
|
|
414
|
+
[],
|
|
415
|
+
`this item has no "@type", so search engines can't tell what it describes; add one, such as "Organization" or "Article"`,
|
|
416
|
+
);
|
|
417
|
+
for (const [key, val] of Object.entries(v)) {
|
|
418
|
+
if (key === "@graph") {
|
|
419
|
+
walk(val, `${path}.@graph`, null, true);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
for (const x of list(val)) {
|
|
423
|
+
const ph = typeof x === "string" && placeholderIn(x);
|
|
424
|
+
if (ph)
|
|
425
|
+
add(
|
|
426
|
+
"error",
|
|
427
|
+
"placeholder",
|
|
428
|
+
path,
|
|
429
|
+
known,
|
|
430
|
+
`"${key}" holds the placeholder "${ph}"; fill in the real value from the site's data`,
|
|
431
|
+
{ rule: "SCHEMA-01", key },
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
const k = term(key);
|
|
435
|
+
if (key.startsWith("@") || k.includes(":")) continue; // JSON-LD keywords and other vocabularies
|
|
436
|
+
if (/-(input|output)$/.test(k) && PROPS[k.replace(/-(input|output)$/, "")]) continue; // Action annotations: "query-input"
|
|
437
|
+
if (known.length) {
|
|
438
|
+
if (!PROPS[k])
|
|
439
|
+
add("error", "prop-unknown", path, known, `"${key}" isn't a schema.org property${hint(PROP_LC, k)}`, { key });
|
|
440
|
+
else if (!known.some(t => allowed(t).has(k))) {
|
|
441
|
+
const where = PROPS[k][0];
|
|
442
|
+
add(
|
|
443
|
+
"error",
|
|
444
|
+
"prop-unknown",
|
|
445
|
+
path,
|
|
446
|
+
known,
|
|
447
|
+
`"${key}" isn't a property of ${known.join(" or ")}; schema.org defines it for ${where.slice(0, 4).join(", ")}${where.length > 4 ? " and others" : ""}`,
|
|
448
|
+
{ key },
|
|
449
|
+
);
|
|
450
|
+
} else for (const x of [].concat(val)) shape(key, k, x, path, known);
|
|
451
|
+
}
|
|
452
|
+
if (known.length && (k === "review" || k === "aggregateRating"))
|
|
453
|
+
for (const c of list(val)) {
|
|
454
|
+
const other = otherReviewed(v, c);
|
|
455
|
+
if (other)
|
|
456
|
+
add(
|
|
457
|
+
"warning",
|
|
458
|
+
"review-target-conflict",
|
|
459
|
+
`${path}.${key}`,
|
|
460
|
+
known,
|
|
461
|
+
`this nested ${k === "review" ? "review" : "rating"} names "${clip(other)}" in "itemReviewed", but Google takes the item it sits in as the one reviewed; leave "itemReviewed" out, or move the review out on its own`,
|
|
462
|
+
{ doc: REVIEW_DOC, rule: "SCHEMA-01", key: "itemReviewed" },
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
walk(val, `${path}.${key}`, known.length ? known : parent, true, k);
|
|
466
|
+
}
|
|
467
|
+
if (known.length) {
|
|
468
|
+
google(v, path, known, parent, last, via);
|
|
469
|
+
business(v, path, known);
|
|
470
|
+
merchant(v, path, known);
|
|
471
|
+
}
|
|
472
|
+
if (known.some(t => isA(t, "Event")) && onlineOnly(v))
|
|
473
|
+
add(
|
|
474
|
+
"warning",
|
|
475
|
+
"event-online",
|
|
476
|
+
path,
|
|
477
|
+
known,
|
|
478
|
+
`this event is online only; since 5 Jun 2025 Google's event results need a physical location, so it can't show there. It does no harm: keep it for other engines, or remove it`,
|
|
479
|
+
{ doc: EVENT_DOC, rule: "SCHEMA-05", key: "eventAttendanceMode" },
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const typedIds = new Set(),
|
|
484
|
+
scan = x => {
|
|
485
|
+
if (x && typeof x === "object") {
|
|
486
|
+
if (has(x, "@type") && typeof x["@id"] === "string") typedIds.add(x["@id"]);
|
|
487
|
+
Object.values(x).forEach(scan);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
scan(data);
|
|
491
|
+
|
|
492
|
+
for (const [root, path] of Array.isArray(data) ? data.map((d, i) => [d, `$[${i}]`]) : [[data, "$"]]) {
|
|
493
|
+
if (!root || typeof root !== "object") continue;
|
|
494
|
+
if (root["@context"] === undefined)
|
|
495
|
+
add("error", "context-missing", path, [], `no "@context"; add "@context": "https://schema.org"`);
|
|
496
|
+
else if (!JSON.stringify(root["@context"]).includes("schema.org")) continue; // another vocabulary, so not checked here
|
|
497
|
+
walk(root, path, null, true);
|
|
498
|
+
}
|
|
499
|
+
return issues;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const CHECKABLE = /\.(html?|jsonld|json)$/i;
|
|
503
|
+
const lineAt = (text, i) => text.slice(0, i).split("\n").length;
|
|
504
|
+
const blocksOf = html =>
|
|
505
|
+
[...html.matchAll(/(<script\b[^>]*>)([\s\S]*?)<\/script\s*>/gi)]
|
|
506
|
+
.filter(m => /\btype\s*=\s*["']?application\/ld\+json/i.test(m[1]))
|
|
507
|
+
.map(m => ({ body: m[2], start: m.index + m[1].length }));
|
|
508
|
+
|
|
509
|
+
/** Checks one file's text: JSON-LD (json = true) or HTML. Returns null when a .json file isn't JSON-LD. */
|
|
510
|
+
function checkText(name, text, json) {
|
|
511
|
+
if (json && !/\.jsonld$/i.test(name) && !/"@(context|type|graph)"/.test(text)) return null; // package.json, manifest.json…
|
|
512
|
+
const blocks = json ? [{ body: text, start: 0 }] : blocksOf(text);
|
|
513
|
+
const issues = blocks.flatMap((b, n) =>
|
|
514
|
+
checkJsonLd(b.body).map(({ pos, ...i }) => {
|
|
515
|
+
const at = b.start + pos,
|
|
516
|
+
line = lineAt(text, at);
|
|
517
|
+
return {
|
|
518
|
+
file: name,
|
|
519
|
+
line,
|
|
520
|
+
block: n + 1,
|
|
521
|
+
...i,
|
|
522
|
+
...(i.check === "json-invalid" && {
|
|
523
|
+
message: `${i.message} (line ${line}, column ${at - text.lastIndexOf("\n", at - 1)})`,
|
|
524
|
+
}),
|
|
525
|
+
};
|
|
526
|
+
}),
|
|
527
|
+
);
|
|
528
|
+
return { items: blocks.length, issues };
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function* walkDir(dir) {
|
|
532
|
+
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
533
|
+
if (e.name.startsWith(".") || e.name === "node_modules") continue;
|
|
534
|
+
const p = join(dir, e.name);
|
|
535
|
+
if (e.isDirectory()) yield* walkDir(p);
|
|
536
|
+
else if (CHECKABLE.test(e.name)) yield p;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// Redirects are followed by hand so every hop gets the same loopback test as the URL itself. 50 MB is a local cap.
|
|
541
|
+
const MAX_HOPS = 5,
|
|
542
|
+
MAX_BYTES = 50 * 1024 * 1024;
|
|
543
|
+
async function fetchPage(input, allowRemote) {
|
|
544
|
+
let u = new URL(input);
|
|
545
|
+
for (let hop = 0; hop <= MAX_HOPS; hop++) {
|
|
546
|
+
const res = await fetch(u, { redirect: "manual", signal: AbortSignal.timeout(30000) });
|
|
547
|
+
const to = res.status >= 300 && res.status < 400 && res.headers.get("location");
|
|
548
|
+
if (!to) {
|
|
549
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
550
|
+
const body = await bodyUpTo(res, MAX_BYTES);
|
|
551
|
+
if (!body) throw new Error("it's over 50 MB");
|
|
552
|
+
return {
|
|
553
|
+
u,
|
|
554
|
+
text: body.toString("utf8"),
|
|
555
|
+
json: /json/i.test(res.headers.get("content-type") || "") || /\.json(ld)?$/i.test(u.pathname),
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
const next = new URL(to, u);
|
|
559
|
+
if (!/^https?:$/.test(next.protocol) || (!allowRemote && !loopback(next.hostname)))
|
|
560
|
+
throw new Error(`it redirects to ${next.href}, which isn't on this machine (pass --allow-remote to follow it)`);
|
|
561
|
+
u = next;
|
|
562
|
+
}
|
|
563
|
+
throw new Error(`it redirects more than ${MAX_HOPS} times`);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
async function checkInputs(inputs, { allowRemote = false } = {}) {
|
|
567
|
+
const out = { files: 0, items: 0, issues: [] };
|
|
568
|
+
const take = r => {
|
|
569
|
+
if (r) {
|
|
570
|
+
out.files++;
|
|
571
|
+
out.items += r.items;
|
|
572
|
+
out.issues.push(...r.issues);
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
const show = f => {
|
|
576
|
+
const r = relative(process.cwd(), f);
|
|
577
|
+
return r && !r.startsWith("..") && !isAbsolute(r) ? r : f;
|
|
578
|
+
};
|
|
579
|
+
for (const input of inputs) {
|
|
580
|
+
if (/^https?:\/\//i.test(input)) {
|
|
581
|
+
let u;
|
|
582
|
+
try {
|
|
583
|
+
u = new URL(input);
|
|
584
|
+
} catch {
|
|
585
|
+
throw new Fail(`schema-check: ${input} isn't a web address.`);
|
|
586
|
+
}
|
|
587
|
+
if (!allowRemote && !loopback(u.hostname))
|
|
588
|
+
throw new Fail(
|
|
589
|
+
`schema-check: ${input} isn't on this machine. This is a local tool; pass --allow-remote to fetch other hosts.`,
|
|
590
|
+
);
|
|
591
|
+
try {
|
|
592
|
+
const page = await fetchPage(input, allowRemote);
|
|
593
|
+
take(checkText(input, page.text, page.json));
|
|
594
|
+
} catch (e) {
|
|
595
|
+
out.issues.push({
|
|
596
|
+
file: input,
|
|
597
|
+
line: 0,
|
|
598
|
+
block: 0,
|
|
599
|
+
severity: "error",
|
|
600
|
+
check: "fetch-failed",
|
|
601
|
+
rule: "SCHEMA-08",
|
|
602
|
+
path: "",
|
|
603
|
+
type: "",
|
|
604
|
+
message: `couldn't fetch it: ${e.cause?.code || e.message}`,
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (!existsSync(input)) throw new Fail(`schema-check: ${input} doesn't exist`);
|
|
610
|
+
const files = statSync(input).isDirectory() ? [...walkDir(input)] : [input];
|
|
611
|
+
for (const f of files) take(checkText(show(f), readFileSync(f, "utf8"), /\.json(ld)?$/i.test(f)));
|
|
612
|
+
}
|
|
613
|
+
return out;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
const plural = (n, w) => `${n} ${w}${n === 1 ? "" : "s"}`;
|
|
617
|
+
const format = i =>
|
|
618
|
+
` ${i.file}:${i.line} ${i.severity} ${i.path}${i.type ? ` (${i.type})` : ""}: ${i.message}${i.doc ? ` · ${i.doc}` : ""}`;
|
|
619
|
+
|
|
620
|
+
// Paths come from the tool-input fields only, never from the text written: Cursor's file_path, Claude Code's
|
|
621
|
+
// tool_input, Copilot's toolArgs (maybe a JSON string), and the file headers of a Codex apply_patch.
|
|
622
|
+
function pathsIn(payload) {
|
|
623
|
+
const obj = v => {
|
|
624
|
+
if (typeof v === "string")
|
|
625
|
+
try {
|
|
626
|
+
v = JSON.parse(v);
|
|
627
|
+
} catch {}
|
|
628
|
+
return v && typeof v === "object" && !Array.isArray(v) ? v : {};
|
|
629
|
+
};
|
|
630
|
+
const input = obj(payload?.tool_input),
|
|
631
|
+
out = new Set();
|
|
632
|
+
if (typeof payload?.file_path === "string") out.add(payload.file_path);
|
|
633
|
+
for (const src of [input, obj(payload?.toolArgs)])
|
|
634
|
+
for (const k of ["file_path", "filePath", "path"]) if (typeof src[k] === "string") out.add(src[k]);
|
|
635
|
+
// Copilot's apply_patch toolArgs is the patch or an object holding it; its hooks reference doesn't give the type.
|
|
636
|
+
// Other tools' text fields are written content, never a list of files.
|
|
637
|
+
const patches = [
|
|
638
|
+
...[].concat(input.command ?? []),
|
|
639
|
+
...(payload?.toolName === "apply_patch" ? [payload.toolArgs, ...Object.values(obj(payload.toolArgs))] : []),
|
|
640
|
+
];
|
|
641
|
+
for (const c of patches)
|
|
642
|
+
if (typeof c === "string" && c.includes("*** "))
|
|
643
|
+
for (const m of c.matchAll(/^\*\*\* (?:Add File|Update File|Move to): (.+)$/gm)) out.add(m[1].trim());
|
|
644
|
+
return out;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
async function hook(mode) {
|
|
648
|
+
let payload;
|
|
649
|
+
try {
|
|
650
|
+
payload = JSON.parse(readFileSync(0, "utf8"));
|
|
651
|
+
} catch {
|
|
652
|
+
return;
|
|
653
|
+
} // not a hook payload: stay silent
|
|
654
|
+
const base = typeof payload?.cwd === "string" ? payload.cwd : process.cwd();
|
|
655
|
+
const files = [...pathsIn(payload)]
|
|
656
|
+
.map(p => resolve(base, p))
|
|
657
|
+
.filter(p => CHECKABLE.test(p) && existsSync(p) && statSync(p).isFile());
|
|
658
|
+
if (!files.length) return;
|
|
659
|
+
const errors = (await checkInputs(files)).issues.filter(i => i.severity === "error");
|
|
660
|
+
if (!errors.length) return;
|
|
661
|
+
const text = `schema-check found ${plural(errors.length, "structured data error")}. Fix them in the source that renders this JSON-LD:\n${errors.map(format).join("\n")}`;
|
|
662
|
+
// Copilot and Cursor take the text as JSON context; other hosts show a hook's stderr on exit 2.
|
|
663
|
+
if (mode === "copilot") console.log(JSON.stringify({ additionalContext: text }));
|
|
664
|
+
else if (mode === "cursor")
|
|
665
|
+
console.log(JSON.stringify({ additional_context: text })); // https://cursor.com/docs/hooks
|
|
666
|
+
else {
|
|
667
|
+
console.error(text);
|
|
668
|
+
return 2;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// Every word that isn't an option is a file, folder or URL to check; after --hook, copilot or cursor is its mode.
|
|
673
|
+
async function main(argv) {
|
|
674
|
+
const USAGE =
|
|
675
|
+
"usage: node schema-check.mjs <file|folder|url ...> [--json out.json] [--quiet] [--allow-remote]\n node schema-check.mjs --hook [copilot|cursor] (reads an agent hook payload on stdin)";
|
|
676
|
+
const args = parseArgs(argv, { flags: ["json"], booleans: ["hook", "quiet", "allow-remote"], usage: USAGE }),
|
|
677
|
+
inputs = args._;
|
|
678
|
+
if (args.hook) return hook(inputs[0] === "copilot" || inputs[0] === "cursor" ? inputs[0] : true);
|
|
679
|
+
if (!inputs.length || args.json === true) throw new Fail(USAGE);
|
|
680
|
+
const opts = { json: args.json, quiet: !!args.quiet, allowRemote: !!args["allow-remote"] };
|
|
681
|
+
if (inputs.some(a => /^https?:\/\//i.test(a))) warnProxy(); // only URLs are fetched
|
|
682
|
+
const r = await checkInputs(inputs, opts);
|
|
683
|
+
const count = s => r.issues.filter(i => i.severity === s).length,
|
|
684
|
+
errors = count("error"),
|
|
685
|
+
warnings = count("warning"),
|
|
686
|
+
notes = count("note");
|
|
687
|
+
if (opts.json) {
|
|
688
|
+
mkdirSync(dirname(resolve(opts.json)), { recursive: true });
|
|
689
|
+
writeFileSync(
|
|
690
|
+
opts.json,
|
|
691
|
+
JSON.stringify(
|
|
692
|
+
{
|
|
693
|
+
tool: "schema-check",
|
|
694
|
+
vocabulary: `schema.org ${VOCAB.version}`,
|
|
695
|
+
googleRulesChecked: GOOGLE.checked,
|
|
696
|
+
files: r.files,
|
|
697
|
+
items: r.items,
|
|
698
|
+
errors,
|
|
699
|
+
warnings,
|
|
700
|
+
notes,
|
|
701
|
+
issues: r.issues,
|
|
702
|
+
},
|
|
703
|
+
null,
|
|
704
|
+
2,
|
|
705
|
+
),
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
if (!opts.quiet || errors) {
|
|
709
|
+
console.log(
|
|
710
|
+
`schema-check: ${plural(r.files, "file")}, ${plural(r.items, "item")}, ${plural(errors, "error")}, ${plural(warnings, "warning")}${notes ? `, ${plural(notes, "note")}` : ""} · schema.org ${VOCAB.version} · Google's rules as of ${GOOGLE.checked}`,
|
|
711
|
+
);
|
|
712
|
+
for (const i of r.issues) if (!opts.quiet || i.severity === "error") console.log(format(i));
|
|
713
|
+
}
|
|
714
|
+
return errors ? 1 : 0;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
if (isMain(import.meta.url)) await runMain(() => main(process.argv.slice(2)));
|