Skip to content

Commit a5d87d8

Browse files
authored
Merge pull request #23 from goude/claude/fix-broken-image-tests-Li0PW
Replace external image URLs with local assets
2 parents d896b59 + 7c9758f commit a5d87d8

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

public/placeholder.svg

Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, it, expect } from "vitest";
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
5+
const SRC_DIR = path.resolve(import.meta.dirname, "../src");
6+
7+
/** Recursively collect all files matching an extension under dir. */
8+
function collectFiles(dir, ext) {
9+
const results = [];
10+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
11+
const full = path.join(dir, entry.name);
12+
if (entry.isDirectory()) {
13+
results.push(...collectFiles(full, ext));
14+
} else if (entry.isFile() && entry.name.endsWith(ext)) {
15+
results.push(full);
16+
}
17+
}
18+
return results;
19+
}
20+
21+
/** Match <img ...src="https://..."> across tag boundaries (multiline). */
22+
const IMG_EXTERNAL_SRC = /<img\b[^>]*\bsrc=["']https?:\/\/[^"']+["'][^>]*>/gis;
23+
24+
describe("external image references in source files", () => {
25+
it("no .astro file uses an external http(s) URL as <img src>", () => {
26+
const astroFiles = collectFiles(SRC_DIR, ".astro");
27+
const violations = [];
28+
29+
for (const file of astroFiles) {
30+
const content = fs.readFileSync(file, "utf8");
31+
const matches = [...content.matchAll(IMG_EXTERNAL_SRC)];
32+
for (const m of matches) {
33+
const lineNumber = content.slice(0, m.index).split("\n").length;
34+
violations.push(`${path.relative(SRC_DIR, file)}:${lineNumber}: ${m[0].slice(0, 80)}…`);
35+
}
36+
}
37+
38+
expect(violations, `External <img src> URLs found:\n${violations.join("\n")}`).toEqual([]);
39+
});
40+
});

src/pages/ai-generated/markup.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ import Layout from "@/layouts/Layout.astro";
147147

148148
<figure>
149149
<picture>
150-
<source srcset="https://via.placeholder.com/400x200" />
150+
<source srcset="/placeholder.svg" />
151151
<img
152-
src="https://via.placeholder.com/400x200"
152+
src="/placeholder.svg"
153153
alt="Placeholder"
154154
width="400"
155155
height="200"

src/pages/ai-generated/wall-of-sound/_Origins.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<figure class="article-figure">
2424
<img
25-
src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Gold_Star_Studios.jpg/1024px-Gold_Star_Studios.jpg"
25+
src="/ai-generated/wall-of-sound/gold-star-studios.jpg"
2626
alt="The exterior of Gold Star Studios on Santa Monica Boulevard, Los Angeles"
2727
loading="lazy"
2828
decoding="async"

0 commit comments

Comments
 (0)