@uxfront/site-checks 0.0.0 → 0.1.1
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 +254 -1
- package/bin/site-checks.js +8 -0
- package/index.d.ts +362 -0
- package/index.js +10 -0
- package/lib/check.js +260 -0
- package/lib/cli.js +80 -0
- package/lib/html.js +288 -0
- package/lib/rules.js +1301 -0
- package/lib/site.js +601 -0
- package/lib/validate.js +211 -0
- package/lighthouse.d.ts +45 -0
- package/lighthouse.js +88 -0
- package/package.json +48 -4
package/lib/cli.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
import { checkSeo, formatReport } from "./check.js";
|
|
6
|
+
|
|
7
|
+
const USAGE = `Usage: site-checks seo [dist] [--config <file>]
|
|
8
|
+
|
|
9
|
+
Checks a built site for search and AI search: its tags, structured data, sitemap,
|
|
10
|
+
robots.txt and llms.txt. dist defaults to ./dist, and the config to
|
|
11
|
+
./site-checks.config.mjs, whose default export holds the options.
|
|
12
|
+
|
|
13
|
+
Exits 0 when every rule passes, 1 on a problem, and 2 when it can't run.`;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Runs the CLI and returns its exit code.
|
|
17
|
+
* @param {string[]} args
|
|
18
|
+
* @param {{ out: (text: string) => void, err: (text: string) => void, cwd: string }} io
|
|
19
|
+
*/
|
|
20
|
+
export async function main(args, { out, err, cwd }) {
|
|
21
|
+
const [command, ...rest] = args;
|
|
22
|
+
if (command === "--help" || command === "-h" || command === "help") {
|
|
23
|
+
out(`${USAGE}\n`);
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
if (command !== "seo") {
|
|
27
|
+
err(`${USAGE}\n`);
|
|
28
|
+
return 2;
|
|
29
|
+
}
|
|
30
|
+
let dist;
|
|
31
|
+
let config = "site-checks.config.mjs";
|
|
32
|
+
for (let index = 0; index < rest.length; index++) {
|
|
33
|
+
const arg = rest[index] ?? "";
|
|
34
|
+
if (arg === "--config" || arg === "-c") {
|
|
35
|
+
const next = rest[++index];
|
|
36
|
+
if (!next) {
|
|
37
|
+
err("--config needs a file\n");
|
|
38
|
+
return 2;
|
|
39
|
+
}
|
|
40
|
+
config = next;
|
|
41
|
+
} else if (arg.startsWith("--config=")) {
|
|
42
|
+
config = arg.slice("--config=".length);
|
|
43
|
+
} else if (arg.startsWith("-") || dist !== undefined) {
|
|
44
|
+
err(`Unexpected argument ${arg}\n\n${USAGE}\n`);
|
|
45
|
+
return 2;
|
|
46
|
+
} else {
|
|
47
|
+
dist = arg;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const folder = resolve(cwd, dist ?? "dist");
|
|
51
|
+
if (!existsSync(join(folder, "index.html"))) {
|
|
52
|
+
err(`No build in ${folder}: there's no index.html. Build the site first.\n`);
|
|
53
|
+
return 2;
|
|
54
|
+
}
|
|
55
|
+
const file = resolve(cwd, config);
|
|
56
|
+
if (!existsSync(file)) {
|
|
57
|
+
err(`No config at ${file}. Pass one with --config <file>.\n`);
|
|
58
|
+
return 2;
|
|
59
|
+
}
|
|
60
|
+
let options;
|
|
61
|
+
try {
|
|
62
|
+
options = (await import(pathToFileURL(file).href)).default;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
err(`Couldn't load ${config}: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
65
|
+
return 2;
|
|
66
|
+
}
|
|
67
|
+
if (!options || typeof options !== "object") {
|
|
68
|
+
err(`${config} has no default export of options\n`);
|
|
69
|
+
return 2;
|
|
70
|
+
}
|
|
71
|
+
let report;
|
|
72
|
+
try {
|
|
73
|
+
report = checkSeo({ ...options, dist: folder });
|
|
74
|
+
} catch (error) {
|
|
75
|
+
err(`${config}: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
76
|
+
return 2;
|
|
77
|
+
}
|
|
78
|
+
out(`${formatReport(report)}\n`);
|
|
79
|
+
return report.problems ? 1 : 0;
|
|
80
|
+
}
|
package/lib/html.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/** @import { Entity, ParseProfile, Scope } from "../index.js" */
|
|
2
|
+
|
|
3
|
+
/** @typedef {Required<ParseProfile>} Profile */
|
|
4
|
+
|
|
5
|
+
/** @type {Readonly<Record<string, string>>} */
|
|
6
|
+
const NAMED = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The character a numeric reference like `#x27` or `#39` stands for. A reference past the last
|
|
10
|
+
* code point throws, as `String.fromCodePoint` does, so the page's rule reports it.
|
|
11
|
+
* @param {string} code
|
|
12
|
+
*/
|
|
13
|
+
const numeric = (code) =>
|
|
14
|
+
String.fromCodePoint(
|
|
15
|
+
code[1]?.toLowerCase() === "x"
|
|
16
|
+
? Number.parseInt(code.slice(2), 16)
|
|
17
|
+
: Number.parseInt(code.slice(1), 10),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
/** @param {string} text */
|
|
21
|
+
const escapeRegExp = (text) => text.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A decoder for the profile's entities. `one-pass` decodes each reference once, so `&lt;`
|
|
25
|
+
* stays `<`. `numeric-first` decodes numeric references before named ones, so `&lt;`
|
|
26
|
+
* becomes `<`.
|
|
27
|
+
* @param {Pick<Profile, "decode" | "decodeOrder" | "decodeIgnoreCase">} profile
|
|
28
|
+
* @returns {(text: string) => string}
|
|
29
|
+
*/
|
|
30
|
+
export function decoder({ decode: entities, decodeOrder, decodeIgnoreCase }) {
|
|
31
|
+
const named = entities.filter((entity) => entity !== "numeric");
|
|
32
|
+
const flags = decodeIgnoreCase ? "gi" : "g";
|
|
33
|
+
const NUMERIC = String.raw`#[xX][\da-fA-F]+|#\d+`;
|
|
34
|
+
/** @param {string} code */
|
|
35
|
+
const value = (code) =>
|
|
36
|
+
code.startsWith("#") && entities.includes("numeric")
|
|
37
|
+
? numeric(code)
|
|
38
|
+
: (NAMED[decodeIgnoreCase ? code.toLowerCase() : code] ??
|
|
39
|
+
(code.startsWith("#") ? numeric(code) : `&${code};`));
|
|
40
|
+
/** @param {string[]} alternatives */
|
|
41
|
+
const replacer = (alternatives) => {
|
|
42
|
+
if (!alternatives.length) {
|
|
43
|
+
return (/** @type {string} */ text) => text;
|
|
44
|
+
}
|
|
45
|
+
const pattern = new RegExp(`&(${alternatives.join("|")});`, flags);
|
|
46
|
+
return (/** @type {string} */ text) =>
|
|
47
|
+
text.replace(pattern, (_, /** @type {string} */ code) => value(code));
|
|
48
|
+
};
|
|
49
|
+
const numerics = entities.includes("numeric") ? [NUMERIC] : [];
|
|
50
|
+
const names = named.map(escapeRegExp);
|
|
51
|
+
if (decodeOrder === "numeric-first") {
|
|
52
|
+
const first = replacer(numerics);
|
|
53
|
+
const second = replacer(names);
|
|
54
|
+
return (text) => second(first(text));
|
|
55
|
+
}
|
|
56
|
+
return replacer([...numerics, ...names]);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The part of a page that a scope reads.
|
|
61
|
+
* @param {string} html
|
|
62
|
+
* @param {Scope} scope
|
|
63
|
+
*/
|
|
64
|
+
export function scoped(html, scope) {
|
|
65
|
+
if (scope === "document") {
|
|
66
|
+
return html;
|
|
67
|
+
}
|
|
68
|
+
if (scope === "markup") {
|
|
69
|
+
return html.replaceAll(/<script\b[\s\S]*?<\/script>|<!--[\s\S]*?-->/gi, "");
|
|
70
|
+
}
|
|
71
|
+
if (scope === "head-element") {
|
|
72
|
+
return html.match(/<head[\s>][\s\S]*?<\/head>/i)?.[0] ?? "";
|
|
73
|
+
}
|
|
74
|
+
const end = html.indexOf("</head>");
|
|
75
|
+
return end > 0 ? html.slice(0, end) : html;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// A tag's attributes, where a quoted value may hold a `>`, as in `content="<component is>"`.
|
|
79
|
+
const ATTRS = `(?:[^>"']|"[^"]*"|'[^']*')*`;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The regular expression that finds a tag by name, as the profile reads tags.
|
|
83
|
+
* @param {string} name
|
|
84
|
+
* @param {Pick<Profile, "quotes" | "caseSensitiveTags" | "tagEnd">} profile
|
|
85
|
+
* @param {string} [after] What follows the tag, like its body and end tag.
|
|
86
|
+
*/
|
|
87
|
+
export function tagPattern(name, { quotes, caseSensitiveTags, tagEnd }, after = "") {
|
|
88
|
+
const end = tagEnd === "word" ? String.raw`\b` : String.raw`(?=[\s/>])`;
|
|
89
|
+
const attributes = quotes === "double" ? "[^>]*" : ATTRS;
|
|
90
|
+
return new RegExp(`<${name}${end}(${attributes})>${after}`, caseSensitiveTags ? "g" : "gi");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The attributes in a tag's source, with lower-case names and decoded values. With `double`, only
|
|
95
|
+
* double-quoted values are read.
|
|
96
|
+
* @param {string} source
|
|
97
|
+
* @param {Profile["quotes"]} quotes
|
|
98
|
+
* @param {(text: string) => string} decode
|
|
99
|
+
*/
|
|
100
|
+
export function attributes(source, quotes, decode) {
|
|
101
|
+
/** @type {Record<string, string>} */
|
|
102
|
+
const found = {};
|
|
103
|
+
if (quotes === "double") {
|
|
104
|
+
for (const [, key = "", value = ""] of source.matchAll(/([\w:-]+)="([^"]*)"/g)) {
|
|
105
|
+
found[key.toLowerCase()] = decode(value);
|
|
106
|
+
}
|
|
107
|
+
return found;
|
|
108
|
+
}
|
|
109
|
+
for (const [, key = "", double, single, bare] of source.matchAll(
|
|
110
|
+
/([^\s=/>"']+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>"']+)))?/g,
|
|
111
|
+
)) {
|
|
112
|
+
found[key.toLowerCase()] = decode(double ?? single ?? bare ?? "");
|
|
113
|
+
}
|
|
114
|
+
return found;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The attributes of every `<name …>` tag, as the profile reads them.
|
|
119
|
+
* @param {string} html
|
|
120
|
+
* @param {string} name
|
|
121
|
+
* @param {Profile} profile
|
|
122
|
+
* @param {(text: string) => string} decode
|
|
123
|
+
* @returns {Record<string, string>[]}
|
|
124
|
+
*/
|
|
125
|
+
export function tags(html, name, profile, decode) {
|
|
126
|
+
return [...html.matchAll(tagPattern(name, profile))].map(([, source = ""]) =>
|
|
127
|
+
attributes(source, profile.quotes, decode),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The text of each `<title>`, as the profile's `titleTag` finds them: `any` takes attributes and
|
|
133
|
+
* any text, `plain-text` attributes and text without a `<`, and `bare` neither.
|
|
134
|
+
* @param {string} html
|
|
135
|
+
* @param {Profile} profile
|
|
136
|
+
*/
|
|
137
|
+
export function titleTexts(html, { titleTag, caseSensitiveTags }) {
|
|
138
|
+
const source =
|
|
139
|
+
titleTag === "any"
|
|
140
|
+
? String.raw`<title\b[^>]*>([\s\S]*?)</title>`
|
|
141
|
+
: titleTag === "plain-text"
|
|
142
|
+
? "<title[^>]*>([^<]*)</title>"
|
|
143
|
+
: "<title>([^<]*)</title>";
|
|
144
|
+
return [...html.matchAll(new RegExp(source, caseSensitiveTags ? "g" : "gi"))].map(
|
|
145
|
+
([, text = ""]) => text,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The number of `<h1>` elements where the profile counts them.
|
|
151
|
+
* @param {string} html
|
|
152
|
+
* @param {Pick<Profile, "h1" | "caseSensitiveTags">} profile
|
|
153
|
+
*/
|
|
154
|
+
export function countH1(html, { h1: where, caseSensitiveTags }) {
|
|
155
|
+
if (where === "markup") {
|
|
156
|
+
return scoped(html, "markup").match(/<h1\b/gi)?.length ?? 0;
|
|
157
|
+
}
|
|
158
|
+
const text =
|
|
159
|
+
where === "rendered"
|
|
160
|
+
? html.replaceAll(/<(script|style|template)\b[\s\S]*?<\/\1>|<!--[\s\S]*?-->/gi, "")
|
|
161
|
+
: html;
|
|
162
|
+
return text.match(new RegExp(String.raw`<h1[\s>]`, caseSensitiveTags ? "g" : "gi"))?.length ?? 0;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The body of each JSON-LD `<script>`. `exact` takes a `type` of exactly `application/ld+json`,
|
|
167
|
+
* and `prefix-any-case` one that starts with it, in any case.
|
|
168
|
+
* @param {string} html
|
|
169
|
+
* @param {Profile} profile
|
|
170
|
+
* @param {(text: string) => string} decode
|
|
171
|
+
*/
|
|
172
|
+
export function jsonLdBodies(html, profile, decode) {
|
|
173
|
+
if (profile.jsonLdType === "prefix-any-case") {
|
|
174
|
+
return [
|
|
175
|
+
...html.matchAll(
|
|
176
|
+
/<script\b[^>]*type=["']?application\/ld\+json["']?[^>]*>([\s\S]*?)<\/script>/gi,
|
|
177
|
+
),
|
|
178
|
+
].map(([, body = ""]) => body);
|
|
179
|
+
}
|
|
180
|
+
return [...html.matchAll(tagPattern("script", profile, String.raw`([\s\S]*?)</script>`))].flatMap(
|
|
181
|
+
([, source = "", body = ""]) =>
|
|
182
|
+
attributes(source, profile.quotes, decode).type === "application/ld+json" ? [body] : [],
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Whether a parsed JSON value is an object or an array, so its keys can be read.
|
|
188
|
+
* @param {unknown} value
|
|
189
|
+
* @returns {value is Record<string, unknown>}
|
|
190
|
+
*/
|
|
191
|
+
export const isObject = (value) => Boolean(value) && typeof value === "object";
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The nodes of the JSON-LD blocks of a page, as the profile reads them.
|
|
195
|
+
* @param {unknown[]} blocks
|
|
196
|
+
* @param {Profile["nodes"]} mode
|
|
197
|
+
* @returns {Record<string, unknown>[]}
|
|
198
|
+
*/
|
|
199
|
+
export function jsonLdNodes(blocks, mode) {
|
|
200
|
+
/** @param {unknown} value @returns {Record<string, unknown>[]} */
|
|
201
|
+
const all = (value) => {
|
|
202
|
+
if (Array.isArray(value)) {
|
|
203
|
+
return value.flatMap(all);
|
|
204
|
+
}
|
|
205
|
+
if (!isObject(value)) {
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
const graph = value["@graph"];
|
|
209
|
+
return [value, ...(Array.isArray(graph) ? graph.flatMap(all) : [])];
|
|
210
|
+
};
|
|
211
|
+
/** @param {unknown} value @returns {Record<string, unknown>[]} */
|
|
212
|
+
const deep = (value) => {
|
|
213
|
+
if (Array.isArray(value)) {
|
|
214
|
+
return value.flatMap(deep);
|
|
215
|
+
}
|
|
216
|
+
if (!isObject(value)) {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
return value["@graph"] ? deep(value["@graph"]) : [value];
|
|
220
|
+
};
|
|
221
|
+
return blocks.flatMap((block) => {
|
|
222
|
+
if (mode === "all") {
|
|
223
|
+
return all(block);
|
|
224
|
+
}
|
|
225
|
+
if (mode === "graph-deep") {
|
|
226
|
+
return deep(block);
|
|
227
|
+
}
|
|
228
|
+
if (!isObject(block)) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
const graph = block["@graph"];
|
|
232
|
+
const members = Array.isArray(graph) ? graph.filter(isObject) : [];
|
|
233
|
+
if (mode === "block-and-graph") {
|
|
234
|
+
return [block, ...members];
|
|
235
|
+
}
|
|
236
|
+
return Array.isArray(graph) ? members : [block];
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Whether a JSON-LD node has this `@type`. With `typeArrays`, an array matches any member.
|
|
242
|
+
* @param {Record<string, unknown>} node
|
|
243
|
+
* @param {string} type
|
|
244
|
+
* @param {boolean} typeArrays
|
|
245
|
+
*/
|
|
246
|
+
export const isType = (node, type, typeArrays) =>
|
|
247
|
+
typeArrays ? [node["@type"]].flat().includes(type) : node["@type"] === type;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The width, height and type of a PNG or JPEG, from its header. With `name`, a PNG is any file
|
|
251
|
+
* whose bytes 1 to 3 spell `PNG`, and with `signature` one that starts with the whole signature.
|
|
252
|
+
* @param {Uint8Array} bytes
|
|
253
|
+
* @param {"signature" | "name"} [png]
|
|
254
|
+
* @returns {{ type: "png" | "jpeg", width: number, height: number } | undefined}
|
|
255
|
+
*/
|
|
256
|
+
export function imageSize(bytes, png = "signature") {
|
|
257
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
258
|
+
const SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
259
|
+
const isPng =
|
|
260
|
+
png === "name"
|
|
261
|
+
? bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47
|
|
262
|
+
: SIGNATURE.every((byte, index) => bytes[index] === byte);
|
|
263
|
+
if (isPng) {
|
|
264
|
+
return bytes.length >= 24
|
|
265
|
+
? { type: "png", width: view.getUint32(16), height: view.getUint32(20) }
|
|
266
|
+
: undefined;
|
|
267
|
+
}
|
|
268
|
+
if (bytes[0] === 0xff && bytes[1] === 0xd8) {
|
|
269
|
+
for (let offset = 2; offset + 9 < bytes.length;) {
|
|
270
|
+
if (bytes[offset] !== 0xff) {
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
const marker = bytes[offset + 1] ?? 0;
|
|
274
|
+
// Start-of-frame markers hold the size. C4, C8 and CC are other segments.
|
|
275
|
+
if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)) {
|
|
276
|
+
return {
|
|
277
|
+
type: "jpeg",
|
|
278
|
+
height: view.getUint16(offset + 5),
|
|
279
|
+
width: view.getUint16(offset + 7),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
offset += 2 + view.getUint16(offset + 2);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export { escapeRegExp };
|