more
All checks were successful
Deploy to Pages / build (pull_request) Successful in 1m36s

This commit is contained in:
Andras Schmelczer 2026-05-25 12:52:37 +01:00
parent 84769f9ce4
commit fd4bb61b5f
30 changed files with 355 additions and 156 deletions

View file

@ -56,7 +56,7 @@ try {
}
const files = await walk(dist);
const htmlAndXml = files.filter((file) => /\.(html|xml)$/.test(file));
const checkedFiles = files.filter((file) => /\.(html|xml|css|webmanifest)$/.test(file));
function pagePathname(file) {
const rel = path.relative(dist, file).replaceAll(path.sep, '/');
@ -65,14 +65,52 @@ function pagePathname(file) {
return `/${rel}`;
}
for (const file of htmlAndXml) {
function collectUrlReferences(body, rel) {
const urls = [];
for (const match of body.matchAll(/\b(?:href|src|poster)=["']([^"']+)["']/g)) {
urls.push(match[1]);
}
for (const match of body.matchAll(/\bsrcset=["']([^"']+)["']/g)) {
for (const candidate of match[1].split(',')) {
const url = candidate.trim().split(/\s+/)[0];
if (url) urls.push(url);
}
}
if (rel.endsWith('.css')) {
for (const match of body.matchAll(/url\(\s*['"]?([^'")]+)['"]?\s*\)/g)) {
urls.push(match[1]);
}
}
if (rel.endsWith('.webmanifest')) {
try {
const manifest = JSON.parse(body);
for (const key of ['start_url', 'scope']) {
if (typeof manifest[key] === 'string') urls.push(manifest[key]);
}
for (const icon of manifest.icons ?? []) {
if (typeof icon?.src === 'string') urls.push(icon.src);
}
for (const screenshot of manifest.screenshots ?? []) {
if (typeof screenshot?.src === 'string') urls.push(screenshot.src);
}
} catch {
failures.push(`${rel}: invalid web manifest JSON`);
}
}
return urls;
}
for (const file of checkedFiles) {
const body = await readFile(file, 'utf8');
const rel = path.relative(dist, file);
const baseUrl = new URL(pagePathname(file), 'https://schmelczer.dev');
const matches = body.matchAll(/\b(?:href|src)=["']([^"'#?]+)(?:[?#][^"']*)?["']/g);
for (const match of matches) {
const raw = match[1];
for (const raw of collectUrlReferences(body, rel)) {
if (/^(mailto:|tel:|data:)/i.test(raw)) continue;
let parsed;